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
Provenance
- Commit
463be14- Tree
cce9beb- Author
Daniel Martí <mvdan@mvdan.cc>- Committer
Daniel Martí <mvdan@mvdan.cc>- Parent
c6399cd
1 file changed+52 added−51 removed
modified:bash_completions.go+52 added-51 removed
1 unmodified line2 3 import (4 "bytes"5 "fmt"6+ "io"7 "os"8 "sort"9 "strings"10 5 unmodified lines16 BashCompOneRequiredFlag = "cobra_annotation_bash_completion_one_required_flag"17 BashCompSubdirsInDir = "cobra_annotation_bash_completion_subdirs_in_dir"18 )19 19-func preamble(out *bytes.Buffer) {20+func preamble(out io.Writer) {21 fmt.Fprintf(out, `#!/bin/bash22 23 __debug()24 {182 unmodified lines207 208 `)209 }210 210-func postscript(out *bytes.Buffer, name string) {211+func 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 || return15 unmodified lines234 __handle_word235 }236 237 `, 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 %s240 else241 complete -o default -o nospace -F __start_%s %s242 fi243 244 `, 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 }247 247-func writeCommands(cmd *Command, out *bytes.Buffer) {248- fmt.Fprintf(out, " commands=()\n")248+func 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 }258 258-func writeFlagHandler(name string, annotations map[string][]string, out *bytes.Buffer) {259+func 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)264 265 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)274 275 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 }285 285-func writeShortFlag(flag *pflag.Flag, out *bytes.Buffer) {286+func 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 }297 297-func writeFlag(flag *pflag.Flag, out *bytes.Buffer) {298+func 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 }309 309-func writeFlags(cmd *Command, out *bytes.Buffer) {310- fmt.Fprintf(out, ` flags=()310+func writeFlags(cmd *Command, w io.Writer) {311+ fmt.Fprintf(w, ` flags=()312 two_word_flags=()313 flags_with_completion=()314 flags_completion=()315 316 `)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 })329 329- fmt.Fprintf(out, "\n")330+ fmt.Fprintf(w, "\n")331 }332 332-func writeRequiredFlag(cmd *Command, out *bytes.Buffer) {333- fmt.Fprintf(out, " must_have_one_flag=()\n")333+func 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 lines342 if !b {343 format += "="344 }345 format += "\")\n"345- fmt.Fprintf(out, format, flag.Name)346+ fmt.Fprintf(w, format, flag.Name)347 348 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 }355 355-func writeRequiredNoun(cmd *Command, out *bytes.Buffer) {356- fmt.Fprintf(out, " must_have_one_noun=()\n")356+func 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 }363 363-func gen(cmd *Command, out *bytes.Buffer) {364+func 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 }382 382-func (cmd *Command) GenBashCompletion(out *bytes.Buffer) {383- preamble(out)383+func (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 }391 392 func (cmd *Command) GenBashCompletionFile(filename string) error {393 out := new(bytes.Buffer)48 unmodified lines