diff options
Diffstat (limited to 'commands/root.go')
-rw-r--r-- | commands/root.go | 133 |
1 files changed, 87 insertions, 46 deletions
diff --git a/commands/root.go b/commands/root.go index 2ea95d4b..42859d19 100644 --- a/commands/root.go +++ b/commands/root.go @@ -14,14 +14,16 @@ import ( const rootCommandName = "git-bug" -// package scoped var to hold the repo after the PreRun execution -var repo repository.ClockedRepo +// These variables are initialized externally during the build. See the Makefile. +var GitCommit string +var GitLastTag string +var GitExactTag string -// RootCmd represents the base command when called without any subcommands -var RootCmd = &cobra.Command{ - Use: rootCommandName, - Short: "A bug tracker embedded in Git.", - Long: `git-bug is a bug tracker embedded in git. +func NewRootCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: rootCommandName, + Short: "A bug tracker embedded in Git.", + Long: `git-bug is a bug tracker embedded in git. git-bug use git objects to store the bug tracking separated from the files history. As bugs are regular git objects, they can be pushed and pulled from/to @@ -29,65 +31,104 @@ the same git remote you are already using to collaborate with other people. `, - // For the root command, force the execution of the PreRun - // even if we just display the help. This is to make sure that we check - // the repository and give the user early feedback. - Run: func(cmd *cobra.Command, args []string) { - if err := cmd.Help(); err != nil { - os.Exit(1) - } - }, - - SilenceUsage: true, - DisableAutoGenTag: true, - - // Custom bash code to connect the git completion for "git bug" to the - // git-bug completion for "git-bug" - BashCompletionFunction: ` + PersistentPreRun: func(cmd *cobra.Command, args []string) { + root := cmd.Root() + + if GitExactTag == "undefined" { + GitExactTag = "" + } + root.Version = GitLastTag + if GitExactTag == "" { + root.Version = fmt.Sprintf("%s-dev-%.10s", root.Version, GitCommit) + } + }, + + // For the root command, force the execution of the PreRun + // even if we just display the help. This is to make sure that we check + // the repository and give the user early feedback. + Run: func(cmd *cobra.Command, args []string) { + if err := cmd.Help(); err != nil { + os.Exit(1) + } + }, + + SilenceUsage: true, + DisableAutoGenTag: true, + + // Custom bash code to connect the git completion for "git bug" to the + // git-bug completion for "git-bug" + BashCompletionFunction: ` _git_bug() { __start_git-bug "$@" } `, + } + + cmd.AddCommand(newAddCommand()) + cmd.AddCommand(newBridgeCommand()) + cmd.AddCommand(newCommandsCommand()) + cmd.AddCommand(newCommentCommand()) + cmd.AddCommand(newDeselectCommand()) + cmd.AddCommand(newLabelCommand()) + cmd.AddCommand(newLsCommand()) + cmd.AddCommand(newLsIdCommand()) + cmd.AddCommand(newLsLabelCommand()) + cmd.AddCommand(newPullCommand()) + cmd.AddCommand(newPushCommand()) + cmd.AddCommand(newSelectCommand()) + cmd.AddCommand(newShowCommand()) + cmd.AddCommand(newStatusCommand()) + cmd.AddCommand(newTermUICommand()) + cmd.AddCommand(newTitleCommand()) + cmd.AddCommand(newUserCommand()) + cmd.AddCommand(newVersionCommand()) + cmd.AddCommand(newWebUICommand()) + + return cmd } func Execute() { - if err := RootCmd.Execute(); err != nil { + if err := NewRootCommand().Execute(); err != nil { os.Exit(1) } } // loadRepo is a pre-run function that load the repository for use in a command -func loadRepo(cmd *cobra.Command, args []string) error { - cwd, err := os.Getwd() - if err != nil { - return fmt.Errorf("unable to get the current working directory: %q", err) - } +func loadRepo(env *Env) func(*cobra.Command, []string) error { + return func(cmd *cobra.Command, args []string) error { + cwd, err := os.Getwd() + if err != nil { + return fmt.Errorf("unable to get the current working directory: %q", err) + } - repo, err = repository.NewGitRepo(cwd, []repository.ClockLoader{bug.ClockLoader}) - if err == repository.ErrNotARepo { - return fmt.Errorf("%s must be run from within a git repo", rootCommandName) - } + env.repo, err = repository.NewGitRepo(cwd, []repository.ClockLoader{bug.ClockLoader}) + if err == repository.ErrNotARepo { + return fmt.Errorf("%s must be run from within a git repo", rootCommandName) + } - if err != nil { - return err - } + if err != nil { + return err + } - return nil + return nil + } } // loadRepoEnsureUser is the same as loadRepo, but also ensure that the user has configured // an identity. Use this pre-run function when an error after using the configured user won't // do. -func loadRepoEnsureUser(cmd *cobra.Command, args []string) error { - err := loadRepo(cmd, args) - if err != nil { - return err - } +func loadRepoEnsureUser(env *Env) func(*cobra.Command, []string) error { + return func(cmd *cobra.Command, args []string) error { + err := loadRepo(env)(cmd, args) + if err != nil { + return err + } - _, err = identity.GetUserIdentity(repo) - if err != nil { - return err - } + _, err = identity.GetUserIdentity(env.repo) + if err != nil { + return err + } - return nil + return nil + } } |