From 7f5922f905831a85ffee4c9226b65715899ba758 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Thu, 19 Jul 2018 12:30:25 +0200 Subject: [PATCH] rework all the commands to use cobra as a parser --- Gopkg.lock | 20 +- bug/bug.go | 4 +- commands/close.go | 16 +- commands/command.go | 59 - commands/commands.go | 60 +- commands/comment.go | 45 +- commands/label.go | 33 +- commands/ls.go | 16 +- commands/new.go | 52 +- commands/open.go | 16 +- commands/pull.go | 16 +- commands/push.go | 16 +- commands/root.go | 59 + commands/show.go | 16 +- commands/webui.go | 17 +- git-bug.go | 80 +- .../inconshreveable/mousetrap/LICENSE | 13 + .../inconshreveable/mousetrap/README.md | 23 + .../inconshreveable/mousetrap/trap_others.go | 15 + .../inconshreveable/mousetrap/trap_windows.go | 98 ++ .../mousetrap/trap_windows_1.4.go | 46 + vendor/github.com/spf13/cobra/.gitignore | 36 + vendor/github.com/spf13/cobra/.mailmap | 3 + vendor/github.com/spf13/cobra/.travis.yml | 21 + vendor/github.com/spf13/cobra/LICENSE.txt | 174 ++ vendor/github.com/spf13/cobra/README.md | 736 ++++++++ vendor/github.com/spf13/cobra/args.go | 89 + .../spf13/cobra/bash_completions.go | 584 +++++++ .../spf13/cobra/bash_completions.md | 221 +++ vendor/github.com/spf13/cobra/cobra.go | 200 +++ .../spf13/cobra/cobra/cmd/license_agpl.go | 683 ++++++++ .../spf13/cobra/cobra/cmd/license_apache_2.go | 238 +++ .../cobra/cobra/cmd/license_bsd_clause_2.go | 71 + .../cobra/cobra/cmd/license_bsd_clause_3.go | 78 + .../spf13/cobra/cobra/cmd/license_gpl_2.go | 376 ++++ .../spf13/cobra/cobra/cmd/license_gpl_3.go | 711 ++++++++ .../spf13/cobra/cobra/cmd/license_lgpl.go | 186 ++ .../spf13/cobra/cobra/cmd/license_mit.go | 63 + .../spf13/cobra/cobra/cmd/licenses.go | 118 ++ .../cobra/cobra/cmd/testdata/LICENSE.golden | 202 +++ vendor/github.com/spf13/cobra/command.go | 1517 +++++++++++++++++ .../github.com/spf13/cobra/command_notwin.go | 5 + vendor/github.com/spf13/cobra/command_win.go | 20 + .../github.com/spf13/cobra/zsh_completions.go | 126 ++ vendor/github.com/spf13/pflag/.gitignore | 2 + vendor/github.com/spf13/pflag/.travis.yml | 21 + vendor/github.com/spf13/pflag/LICENSE | 28 + vendor/github.com/spf13/pflag/README.md | 296 ++++ vendor/github.com/spf13/pflag/bool.go | 94 + vendor/github.com/spf13/pflag/bool_slice.go | 147 ++ vendor/github.com/spf13/pflag/bytes.go | 105 ++ vendor/github.com/spf13/pflag/count.go | 96 ++ vendor/github.com/spf13/pflag/duration.go | 86 + .../github.com/spf13/pflag/duration_slice.go | 128 ++ vendor/github.com/spf13/pflag/flag.go | 1223 +++++++++++++ vendor/github.com/spf13/pflag/float32.go | 88 + vendor/github.com/spf13/pflag/float64.go | 84 + vendor/github.com/spf13/pflag/golangflag.go | 105 ++ vendor/github.com/spf13/pflag/int.go | 84 + vendor/github.com/spf13/pflag/int16.go | 88 + vendor/github.com/spf13/pflag/int32.go | 88 + vendor/github.com/spf13/pflag/int64.go | 84 + vendor/github.com/spf13/pflag/int8.go | 88 + vendor/github.com/spf13/pflag/int_slice.go | 128 ++ vendor/github.com/spf13/pflag/ip.go | 94 + vendor/github.com/spf13/pflag/ip_slice.go | 148 ++ vendor/github.com/spf13/pflag/ipmask.go | 122 ++ vendor/github.com/spf13/pflag/ipnet.go | 98 ++ vendor/github.com/spf13/pflag/string.go | 80 + vendor/github.com/spf13/pflag/string_array.go | 103 ++ vendor/github.com/spf13/pflag/string_slice.go | 149 ++ vendor/github.com/spf13/pflag/uint.go | 88 + vendor/github.com/spf13/pflag/uint16.go | 88 + vendor/github.com/spf13/pflag/uint32.go | 88 + vendor/github.com/spf13/pflag/uint64.go | 88 + vendor/github.com/spf13/pflag/uint8.go | 88 + vendor/github.com/spf13/pflag/uint_slice.go | 126 ++ 77 files changed, 11328 insertions(+), 273 deletions(-) delete mode 100644 commands/command.go create mode 100644 commands/root.go create mode 100644 vendor/github.com/inconshreveable/mousetrap/LICENSE create mode 100644 vendor/github.com/inconshreveable/mousetrap/README.md create mode 100644 vendor/github.com/inconshreveable/mousetrap/trap_others.go create mode 100644 vendor/github.com/inconshreveable/mousetrap/trap_windows.go create mode 100644 vendor/github.com/inconshreveable/mousetrap/trap_windows_1.4.go create mode 100644 vendor/github.com/spf13/cobra/.gitignore create mode 100644 vendor/github.com/spf13/cobra/.mailmap create mode 100644 vendor/github.com/spf13/cobra/.travis.yml create mode 100644 vendor/github.com/spf13/cobra/LICENSE.txt create mode 100644 vendor/github.com/spf13/cobra/README.md create mode 100644 vendor/github.com/spf13/cobra/args.go create mode 100644 vendor/github.com/spf13/cobra/bash_completions.go create mode 100644 vendor/github.com/spf13/cobra/bash_completions.md create mode 100644 vendor/github.com/spf13/cobra/cobra.go create mode 100644 vendor/github.com/spf13/cobra/cobra/cmd/license_agpl.go create mode 100644 vendor/github.com/spf13/cobra/cobra/cmd/license_apache_2.go create mode 100644 vendor/github.com/spf13/cobra/cobra/cmd/license_bsd_clause_2.go create mode 100644 vendor/github.com/spf13/cobra/cobra/cmd/license_bsd_clause_3.go create mode 100644 vendor/github.com/spf13/cobra/cobra/cmd/license_gpl_2.go create mode 100644 vendor/github.com/spf13/cobra/cobra/cmd/license_gpl_3.go create mode 100644 vendor/github.com/spf13/cobra/cobra/cmd/license_lgpl.go create mode 100644 vendor/github.com/spf13/cobra/cobra/cmd/license_mit.go create mode 100644 vendor/github.com/spf13/cobra/cobra/cmd/licenses.go create mode 100644 vendor/github.com/spf13/cobra/cobra/cmd/testdata/LICENSE.golden create mode 100644 vendor/github.com/spf13/cobra/command.go create mode 100644 vendor/github.com/spf13/cobra/command_notwin.go create mode 100644 vendor/github.com/spf13/cobra/command_win.go create mode 100644 vendor/github.com/spf13/cobra/zsh_completions.go create mode 100644 vendor/github.com/spf13/pflag/.gitignore create mode 100644 vendor/github.com/spf13/pflag/.travis.yml create mode 100644 vendor/github.com/spf13/pflag/LICENSE create mode 100644 vendor/github.com/spf13/pflag/README.md create mode 100644 vendor/github.com/spf13/pflag/bool.go create mode 100644 vendor/github.com/spf13/pflag/bool_slice.go create mode 100644 vendor/github.com/spf13/pflag/bytes.go create mode 100644 vendor/github.com/spf13/pflag/count.go create mode 100644 vendor/github.com/spf13/pflag/duration.go create mode 100644 vendor/github.com/spf13/pflag/duration_slice.go create mode 100644 vendor/github.com/spf13/pflag/flag.go create mode 100644 vendor/github.com/spf13/pflag/float32.go create mode 100644 vendor/github.com/spf13/pflag/float64.go create mode 100644 vendor/github.com/spf13/pflag/golangflag.go create mode 100644 vendor/github.com/spf13/pflag/int.go create mode 100644 vendor/github.com/spf13/pflag/int16.go create mode 100644 vendor/github.com/spf13/pflag/int32.go create mode 100644 vendor/github.com/spf13/pflag/int64.go create mode 100644 vendor/github.com/spf13/pflag/int8.go create mode 100644 vendor/github.com/spf13/pflag/int_slice.go create mode 100644 vendor/github.com/spf13/pflag/ip.go create mode 100644 vendor/github.com/spf13/pflag/ip_slice.go create mode 100644 vendor/github.com/spf13/pflag/ipmask.go create mode 100644 vendor/github.com/spf13/pflag/ipnet.go create mode 100644 vendor/github.com/spf13/pflag/string.go create mode 100644 vendor/github.com/spf13/pflag/string_array.go create mode 100644 vendor/github.com/spf13/pflag/string_slice.go create mode 100644 vendor/github.com/spf13/pflag/uint.go create mode 100644 vendor/github.com/spf13/pflag/uint16.go create mode 100644 vendor/github.com/spf13/pflag/uint32.go create mode 100644 vendor/github.com/spf13/pflag/uint64.go create mode 100644 vendor/github.com/spf13/pflag/uint8.go create mode 100644 vendor/github.com/spf13/pflag/uint_slice.go diff --git a/Gopkg.lock b/Gopkg.lock index d413c93042a6d424bf7b2b3f2131dbe1c1febf0f..bf166eb625ecf80ee495e3ec0123a6cd327651e6 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -19,6 +19,12 @@ revision = "e3702bed27f0d39777b0b37b664b6280e8ef8fbf" version = "v1.6.2" +[[projects]] + name = "github.com/inconshreveable/mousetrap" + packages = ["."] + revision = "76626ae9c91c4f2a10f34cad8ce83ea42c93bb75" + version = "v1.0" + [[projects]] name = "github.com/kevinburke/go.uuid" packages = ["."] @@ -67,6 +73,18 @@ packages = ["open"] revision = "75fb7ed4208cf72d323d7d02fd1a5964a7a9073c" +[[projects]] + name = "github.com/spf13/cobra" + packages = ["."] + revision = "ef82de70bb3f60c65fb8eebacbb2d122ef517385" + version = "v0.0.3" + +[[projects]] + name = "github.com/spf13/pflag" + packages = ["."] + revision = "583c0c0531f06d5278b7d917446061adc344b5cd" + version = "v1.0.1" + [[projects]] branch = "master" name = "golang.org/x/sys" @@ -76,6 +94,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "c72c9ba592c5dae56b0ca48216a393c9664d7d275fd67304bf85003ceaac4c0b" + inputs-digest = "e0a170d986fb6962f504dbab544ba4dd2b61efc0762b2aff042c47110339fd80" solver-name = "gps-cdcl" solver-version = 1 diff --git a/bug/bug.go b/bug/bug.go index 398bdb9f8f0f2ba043b1c30415950f5a4718686e..88a0ff8b90c33c007c58b8c558fe44e977522e3c 100644 --- a/bug/bug.go +++ b/bug/bug.go @@ -14,6 +14,7 @@ const BugsRefPattern = "refs/bugs/" const BugsRemoteRefPattern = "refs/remote/%s/bugs/" const OpsEntryName = "ops" const RootEntryName = "root" +const HumanIdLength = 7 // Bug hold the data of a bug thread, organized in a way close to // how it will be persisted inside Git. This is the datastructure @@ -347,7 +348,8 @@ func (bug *Bug) Id() string { // Return the Bug identifier truncated for human consumption func (bug *Bug) HumanId() string { - return fmt.Sprintf("%.8s", bug.id) + format := fmt.Sprintf("%%.%ds", HumanIdLength) + return fmt.Sprintf(format, bug.id) } // Lookup for the very first operation of the bug. diff --git a/commands/close.go b/commands/close.go index 15f50926924bfdeb87c3c447d611ac8a00350f7d..66c2b84d0fc48f54b53f1ab2fcb501f71de150b6 100644 --- a/commands/close.go +++ b/commands/close.go @@ -4,10 +4,10 @@ import ( "errors" "github.com/MichaelMure/git-bug/bug" "github.com/MichaelMure/git-bug/bug/operations" - "github.com/MichaelMure/git-bug/repository" + "github.com/spf13/cobra" ) -func runCloseBug(repo repository.Repo, args []string) error { +func runCloseBug(cmd *cobra.Command, args []string) error { if len(args) > 1 { return errors.New("Only closing one bug at a time is supported") } @@ -37,8 +37,12 @@ func runCloseBug(repo repository.Repo, args []string) error { return err } -var closeCmd = &Command{ - Description: "Mark the bug as closed", - Usage: "", - RunMethod: runCloseBug, +var closeCmd = &cobra.Command{ + Use: "close ", + Short: "Mark the bug as closed", + RunE: runCloseBug, +} + +func init() { + rootCmd.AddCommand(closeCmd) } diff --git a/commands/command.go b/commands/command.go deleted file mode 100644 index 89420b46da5dd9f2e0d941a4228083a6145e2e94..0000000000000000000000000000000000000000 --- a/commands/command.go +++ /dev/null @@ -1,59 +0,0 @@ -// Package commands contains the assorted sub commands supported by the git-bug tool. -package commands - -import ( - "flag" - "fmt" - "github.com/MichaelMure/git-bug/repository" -) - -const messageFilename = "BUG_MESSAGE_EDITMSG" - -// Command represents the definition of a single command. -type Command struct { - // Short description of the command - Description string - // Command line usage - Usage string - // Flag set of the command - flagSet *flag.FlagSet - // Execute the command - RunMethod func(repository.Repo, []string) error -} - -// Run executes a command, given its arguments. -// -// The args parameter is all of the command line args that followed the -// subcommand. -func (cmd *Command) Run(repo repository.Repo, args []string) error { - return cmd.RunMethod(repo, args) -} - -func (cmd *Command) PrintUsage(rootCommand string, cmdName string) { - fmt.Printf("Usage: %s %s %s\n", rootCommand, cmdName, cmd.Usage) - - if cmd.flagSet != nil { - fmt.Printf("\nOptions:\n") - cmd.flagSet.PrintDefaults() - } -} - -// CommandMap defines all of the available (sub)commands. -var CommandMap map[string]*Command - -// We use init() to avoid a cycle in the data initialization because of the "commands" command -func init() { - CommandMap = map[string]*Command{ - "close": closeCmd, - "commands": commandsCmd, - "comment": commentCmd, - "label": labelCmd, - "ls": lsCmd, - "new": newCmd, - "open": openCmd, - "pull": pullCmd, - "push": pushCmd, - "show": showCmd, - "webui": webUICmd, - } -} diff --git a/commands/commands.go b/commands/commands.go index 1e8e9e19e47bf48d8a77db6f00eed5310bff61a4..f06d1ce8769642abab31f4c9485194cc0a8b2051 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -1,55 +1,55 @@ package commands import ( - "flag" "fmt" - "github.com/MichaelMure/git-bug/repository" - "sort" + "github.com/spf13/cobra" ) -var commandsFlagSet = flag.NewFlagSet("commands", flag.ExitOnError) - -var ( - commandsDesc = commandsFlagSet.Bool("pretty", false, "Output the command description as well as Markdown compatible comment") -) - -func runCommands(repo repository.Repo, args []string) error { - commandsFlagSet.Parse(args) - args = commandsFlagSet.Args() +var commandsDesc bool +func runCommands(cmd *cobra.Command, args []string) error { first := true - keys := make([]string, 0, len(CommandMap)) + allCmds := cmd.Root().Commands() - for key := range CommandMap { - keys = append(keys, key) - } - - sort.Strings(keys) - - for _, key := range keys { + for _, cmd := range allCmds { if !first { fmt.Println() } first = false - cmd := CommandMap[key] + if commandsDesc { + fmt.Printf("# %s\n", cmd.Short) + } - if *commandsDesc { - fmt.Printf("# %s\n", cmd.Description) + fmt.Printf("%s %s", + rootCommandName, + cmd.Use, + ) + + if commandsDesc { + fmt.Println() } + } - // TODO: the root name command ("git bug") should be passed from git-bug.go but well ... - fmt.Printf("%s %s %s\n", "git bug", key, cmd.Usage) + if !commandsDesc { + fmt.Println() } return nil } -var commandsCmd = &Command{ - Description: "Display available commands", - Usage: "[