Merge pull request #90 from eparis/deprecated-subcommands
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Deprecated subcommands
authored by Eric Paris · committed by Eric Paris · parents 7c7837e, c3e48f9 · Browse files · 6 files·+48 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+48 added−5 removed
- other:+48 added−5 removed
Provenance
- Commit
9cf0f37- Tree
8a81109- Author
Eric Paris <eparis@redhat.com>- Committer
Eric Paris <eparis@redhat.com>- Parent 1
7c7837e- Parent 2
c3e48f9
6 files changed+48 added−5 removed
modified:bash_completions_test.go+9 added-1 removed
9 unmodified lines10 11 var _ = fmt.Println12 var _ = os.Stderr13 14+func checkOmit(t *testing.T, found, unexpected string) {15+ if strings.Contains(found, unexpected) {16+ t.Errorf("Unexpected response.\nGot: %q\nBut should not have!\n", unexpected)17+ }18+}19+20 func check(t *testing.T, found, expected string) {21 if !strings.Contains(found, expected) {22 t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)23 }9 unmodified lines33 34 func TestBashCompletions(t *testing.T) {35 c := initializeWithRootCmd()36 cmdEcho.AddCommand(cmdTimes)31- c.AddCommand(cmdEcho, cmdPrint)37+ c.AddCommand(cmdEcho, cmdPrint, cmdDeprecated)38 39 // custom completion function40 c.BashCompletionFunction = bash_completion_func41 34 unmodified lines76 // check for required nouns77 check(t, str, `must_have_one_noun+=("pods")`)78 // check for filename extention flags79 check(t, str, `flags_completion+=("_filedir '@(json|yaml|yml)'")`)80+81+ checkOmit(t, str, cmdDeprecated.Name())82 }modified:bash_completions.go+6 added
197 unmodified lines198 199 func writeCommands(cmd *Command, out *bytes.Buffer) {200 fmt.Fprintf(out, " commands=()\n")201 for _, c := range cmd.Commands() {202+ if len(c.Deprecated) > 0 {203+ continue204+ }205 fmt.Fprintf(out, " commands+=(%q)\n", c.Name())206 }207 fmt.Fprintf(out, "\n")208 }84 unmodified lines293 }294 295 func gen(cmd *Command, out *bytes.Buffer) {296 for _, c := range cmd.Commands() {297+ if len(c.Deprecated) > 0 {298+ continue299+ }300 gen(c, out)301 }302 commandName := cmd.CommandPath()303 commandName = strings.Replace(commandName, " ", "_", -1)48 unmodified linesmodified:cobra_test.go+16 added-1 removed
49 unmodified lines50 Run: func(cmd *Command, args []string) {51 },52 }53 54+var cmdDeprecated = &Command{55+ Use: "deprecated [can't do anything here]",56+ Short: "A command which is deprecated",57+ Long: `an absolutely utterly useless command for testing deprecation!.`,58+ Deprecated: "Please use echo instead",59+ Run: func(cmd *Command, args []string) {60+ },61+}62+63 var cmdTimes = &Command{64 Use: "times [# times] [string to echo]",65 Short: "Echo anything to the screen more times",66 Long: `a slightly useless command for testing.`,146 unmodified lines213 buf := new(bytes.Buffer)214 // Testing flag with invalid input215 c.SetOutput(buf)216 cmdEcho.AddCommand(cmdTimes)208- c.AddCommand(cmdPrint, cmdEcho, cmdSubNoRun)217+ c.AddCommand(cmdPrint, cmdEcho, cmdSubNoRun, cmdDeprecated)218 c.SetArgs(strings.Split(input, " "))219 220 err := c.Execute()221 output := buf.String()598 unmodified lines820 if versionUsed != 2 {821 t.Errorf("Replacing command should have been called but didn't\n")822 }823 }824+825+func TestDeprecatedSub(t *testing.T) {826+ c := fullSetupTest("deprecated")827+828+ checkResultContains(t, c, cmdDeprecated.Deprecated)829+}modified:command.go+8 added-2 removed
46 unmodified lines47 // List of all valid non-flag arguments, used for bash completions *TODO* actually validate these48 ValidArgs []string49 // Custom functions used by the bash autocompletion generator50 BashCompletionFunction string51+ // Is this command deprecated and should print this string when used?52+ Deprecated string53 // Full set of flags54 flags *flag.FlagSet55 // Set of flags childrens of this command will inherit56 pflags *flag.FlagSet175 unmodified lines232 Examples:233 {{ .Example }}234 {{end}}{{ if .HasRunnableSubCommands}}235 234-Available Commands: {{range .Commands}}{{if .Runnable}}236+Available Commands: {{range .Commands}}{{if and (.Runnable) (not .Deprecated)}}237 {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}238 {{end}}239 {{ if .HasLocalFlags}}Flags:240 {{.LocalFlags.FlagUsages}}{{end}}241 {{ if .HasInheritedFlags}}Global Flags:242 {{.InheritedFlags.FlagUsages}}{{end}}{{if or (.HasHelpSubCommands) (.HasRunnableSiblings)}}243 Additional help topics:242-{{if .HasHelpSubCommands}}{{range .Commands}}{{if not .Runnable}} {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasRunnableSiblings }}{{range .Parent.Commands}}{{if .Runnable}}{{if not (eq .Name $cmd.Name) }}244+{{if .HasHelpSubCommands}}{{range .Commands}}{{if and (not .Runnable) (not .Deprecated)}} {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasRunnableSiblings }}{{range .Parent.Commands}}{{if and (not .Runnable) (not .Deprecated)}}{{if not (eq .Name $cmd.Name) }}245 {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{end}}246 {{end}}{{ if .HasSubCommands }}247 Use "{{.Root.Name}} help [command]" for more information about a command.248 {{end}}`166 unmodified lines415 if c == nil {416 return fmt.Errorf("Called Execute() on a nil Command")417 }418 419+ if len(c.Deprecated) > 0 {420+ c.Printf("Command %q is deprecated, %s\n", c.Name(), c.Deprecated)421+ }422+423 err = c.ParseFlags(a)424 if err == flag.ErrHelp {425 c.Help()426 return nil539 unmodified linesmodified:md_docs_test.go+6 added-1 removed
12 unmodified lines13 14 func TestGenMdDoc(t *testing.T) {15 c := initializeWithRootCmd()16 // Need two commands to run the command alphabetical sort17- cmdEcho.AddCommand(cmdTimes, cmdEchoSub)17+ cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)18 c.AddCommand(cmdPrint, cmdEcho)19 cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)20 21 out := new(bytes.Buffer)36 unmodified lines58 expected = cmdEchoSub.Short59 if !strings.Contains(found, expected) {60 t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)61 }62+63+ unexpected := cmdDeprecated.Short64+ if strings.Contains(found, unexpected) {65+ t.Errorf("Unexpected response.\nFound: %v\nBut should not have!!\n", unexpected)66+ }67 }modified:md_docs.go+3 added
83 unmodified lines84 children := cmd.Commands()85 sort.Sort(byName(children))86 87 for _, child := range children {88+ if len(child.Deprecated) > 0 {89+ continue90+ }91 cname := name + " " + child.Name()92 link := cname + ".md"93 link = strings.Replace(link, " ", "_", -1)94 fmt.Fprintf(out, "* [%s](%s)\t - %s\n", cname, link, child.Short)30 unmodified lines