Skip to content
Myr

Replace *bytes.Buffer with io.Writer

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Also adds support for generating bash completions on writers other than just buffers. Found via github.com/mvdan/interfacer.

authored by Daniel Martí · committed by Daniel Martí · parent c6399cd · Browse files · 1 file+52 added−51 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+52 added−51 removed

  • other:+52 added−51 removed

1 file+52 added−51 removed

modified:bash_completions.go+52 added-51 removed
1 unmodified line
23import (4 "bytes"5 "fmt"6 "io"7 "os"8 "sort"9 "strings"10
5 unmodified lines
16 BashCompOneRequiredFlag = "cobra_annotation_bash_completion_one_required_flag"17 BashCompSubdirsInDir = "cobra_annotation_bash_completion_subdirs_in_dir"18)1919func preamble(out *bytes.Buffer) {20func preamble(out io.Writer) {21 fmt.Fprintf(out, `#!/bin/bash2223__debug()24{
182 unmodified lines
207208`)209}210210func postscript(out *bytes.Buffer, name string) {211func postscript(w io.Writer, name string) {212 name = strings.Replace(name, ":", "__", -1)212 fmt.Fprintf(out, "__start_%s()\n", name)213 fmt.Fprintf(out, `{213 fmt.Fprintf(w, "__start_%s()\n", name)214 fmt.Fprintf(w, `{215 local cur prev words cword216 declare -A flaghash217 if declare -F _init_completion >/dev/null 2>&1; then218 _init_completion -s || return
15 unmodified lines
234 __handle_word235}236237`, name)237 fmt.Fprintf(out, `if [[ $(type -t compopt) = "builtin" ]]; then238 fmt.Fprintf(w, `if [[ $(type -t compopt) = "builtin" ]]; then239 complete -o default -F __start_%s %s240else241 complete -o default -o nospace -F __start_%s %s242fi243244`, name, name, name, name)244 fmt.Fprintf(out, "# ex: ts=4 sw=4 et filetype=sh\n")245 fmt.Fprintf(w, "# ex: ts=4 sw=4 et filetype=sh\n")246}247247func writeCommands(cmd *Command, out *bytes.Buffer) {248 fmt.Fprintf(out, " commands=()\n")248func writeCommands(cmd *Command, w io.Writer) {249 fmt.Fprintf(w, " commands=()\n")250 for _, c := range cmd.Commands() {251 if !c.IsAvailableCommand() || c == cmd.helpCommand {252 continue253 }253 fmt.Fprintf(out, " commands+=(%q)\n", c.Name())254 fmt.Fprintf(w, " commands+=(%q)\n", c.Name())255 }255 fmt.Fprintf(out, "\n")256 fmt.Fprintf(w, "\n")257}258258func writeFlagHandler(name string, annotations map[string][]string, out *bytes.Buffer) {259func writeFlagHandler(name string, annotations map[string][]string, w io.Writer) {260 for key, value := range annotations {261 switch key {262 case BashCompFilenameExt:262 fmt.Fprintf(out, " flags_with_completion+=(%q)\n", name)263 fmt.Fprintf(w, " flags_with_completion+=(%q)\n", name)264265 if len(value) > 0 {266 ext := "__handle_filename_extension_flag " + strings.Join(value, "|")266 fmt.Fprintf(out, " flags_completion+=(%q)\n", ext)267 fmt.Fprintf(w, " flags_completion+=(%q)\n", ext)268 } else {269 ext := "_filedir"269 fmt.Fprintf(out, " flags_completion+=(%q)\n", ext)270 fmt.Fprintf(w, " flags_completion+=(%q)\n", ext)271 }272 case BashCompSubdirsInDir:272 fmt.Fprintf(out, " flags_with_completion+=(%q)\n", name)273 fmt.Fprintf(w, " flags_with_completion+=(%q)\n", name)274275 if len(value) == 1 {276 ext := "__handle_subdirs_in_dir_flag " + value[0]276 fmt.Fprintf(out, " flags_completion+=(%q)\n", ext)277 fmt.Fprintf(w, " flags_completion+=(%q)\n", ext)278 } else {279 ext := "_filedir -d"279 fmt.Fprintf(out, " flags_completion+=(%q)\n", ext)280 fmt.Fprintf(w, " flags_completion+=(%q)\n", ext)281 }282 }283 }284}285285func writeShortFlag(flag *pflag.Flag, out *bytes.Buffer) {286func writeShortFlag(flag *pflag.Flag, w io.Writer) {287 b := (flag.Value.Type() == "bool")288 name := flag.Shorthand289 format := " "290 if !b {291 format += "two_word_"292 }293 format += "flags+=(\"-%s\")\n"293 fmt.Fprintf(out, format, name)294 writeFlagHandler("-"+name, flag.Annotations, out)294 fmt.Fprintf(w, format, name)295 writeFlagHandler("-"+name, flag.Annotations, w)296}297297func writeFlag(flag *pflag.Flag, out *bytes.Buffer) {298func writeFlag(flag *pflag.Flag, w io.Writer) {299 b := (flag.Value.Type() == "bool")300 name := flag.Name301 format := " flags+=(\"--%s"302 if !b {303 format += "="304 }305 format += "\")\n"305 fmt.Fprintf(out, format, name)306 writeFlagHandler("--"+name, flag.Annotations, out)306 fmt.Fprintf(w, format, name)307 writeFlagHandler("--"+name, flag.Annotations, w)308}309309func writeFlags(cmd *Command, out *bytes.Buffer) {310 fmt.Fprintf(out, ` flags=()310func writeFlags(cmd *Command, w io.Writer) {311 fmt.Fprintf(w, ` flags=()312 two_word_flags=()313 flags_with_completion=()314 flags_completion=()315316`)317 cmd.NonInheritedFlags().VisitAll(func(flag *pflag.Flag) {317 writeFlag(flag, out)318 writeFlag(flag, w)319 if len(flag.Shorthand) > 0 {319 writeShortFlag(flag, out)320 writeShortFlag(flag, w)321 }322 })323 cmd.InheritedFlags().VisitAll(func(flag *pflag.Flag) {323 writeFlag(flag, out)324 writeFlag(flag, w)325 if len(flag.Shorthand) > 0 {325 writeShortFlag(flag, out)326 writeShortFlag(flag, w)327 }328 })329329 fmt.Fprintf(out, "\n")330 fmt.Fprintf(w, "\n")331}332332func writeRequiredFlag(cmd *Command, out *bytes.Buffer) {333 fmt.Fprintf(out, " must_have_one_flag=()\n")333func writeRequiredFlag(cmd *Command, w io.Writer) {334 fmt.Fprintf(w, " must_have_one_flag=()\n")335 flags := cmd.NonInheritedFlags()336 flags.VisitAll(func(flag *pflag.Flag) {337 for key := range flag.Annotations {338 switch key {
3 unmodified lines
342 if !b {343 format += "="344 }345 format += "\")\n"345 fmt.Fprintf(out, format, flag.Name)346 fmt.Fprintf(w, format, flag.Name)347348 if len(flag.Shorthand) > 0 {348 fmt.Fprintf(out, " must_have_one_flag+=(\"-%s\")\n", flag.Shorthand)349 fmt.Fprintf(w, " must_have_one_flag+=(\"-%s\")\n", flag.Shorthand)350 }351 }352 }353 })354}355355func writeRequiredNoun(cmd *Command, out *bytes.Buffer) {356 fmt.Fprintf(out, " must_have_one_noun=()\n")356func writeRequiredNoun(cmd *Command, w io.Writer) {357 fmt.Fprintf(w, " must_have_one_noun=()\n")358 sort.Sort(sort.StringSlice(cmd.ValidArgs))359 for _, value := range cmd.ValidArgs {359 fmt.Fprintf(out, " must_have_one_noun+=(%q)\n", value)360 fmt.Fprintf(w, " must_have_one_noun+=(%q)\n", value)361 }362}363363func gen(cmd *Command, out *bytes.Buffer) {364func gen(cmd *Command, w io.Writer) {365 for _, c := range cmd.Commands() {366 if !c.IsAvailableCommand() || c == cmd.helpCommand {367 continue368 }368 gen(c, out)369 gen(c, w)370 }371 commandName := cmd.CommandPath()372 commandName = strings.Replace(commandName, " ", "_", -1)373 commandName = strings.Replace(commandName, ":", "__", -1)373 fmt.Fprintf(out, "_%s()\n{\n", commandName)374 fmt.Fprintf(out, " last_command=%q\n", commandName)375 writeCommands(cmd, out)376 writeFlags(cmd, out)377 writeRequiredFlag(cmd, out)378 writeRequiredNoun(cmd, out)379 fmt.Fprintf(out, "}\n\n")374 fmt.Fprintf(w, "_%s()\n{\n", commandName)375 fmt.Fprintf(w, " last_command=%q\n", commandName)376 writeCommands(cmd, w)377 writeFlags(cmd, w)378 writeRequiredFlag(cmd, w)379 writeRequiredNoun(cmd, w)380 fmt.Fprintf(w, "}\n\n")381}382382func (cmd *Command) GenBashCompletion(out *bytes.Buffer) {383 preamble(out)383func (cmd *Command) GenBashCompletion(w io.Writer) {384 preamble(w)385 if len(cmd.BashCompletionFunction) > 0 {385 fmt.Fprintf(out, "%s\n", cmd.BashCompletionFunction)386 fmt.Fprintf(w, "%s\n", cmd.BashCompletionFunction)387 }387 gen(cmd, out)388 postscript(out, cmd.Name())388 gen(cmd, w)389 postscript(w, cmd.Name())390}391392func (cmd *Command) GenBashCompletionFile(filename string) error {393 out := new(bytes.Buffer)
48 unmodified lines