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: "[