1// Package commands contains the CLI commands
 2package commands
 3
 4import (
 5	"fmt"
 6	"os"
 7
 8	"github.com/spf13/cobra"
 9)
10
11const rootCommandName = "git-bug"
12
13// These variables are initialized externally during the build. See the Makefile.
14var GitCommit string
15var GitLastTag string
16var GitExactTag string
17
18func NewRootCommand() *cobra.Command {
19	cmd := &cobra.Command{
20		Use:   rootCommandName,
21		Short: "A bug tracker embedded in Git.",
22		Long: `git-bug is a bug tracker embedded in git.
23
24git-bug use git objects to store the bug tracking separated from the files
25history. As bugs are regular git objects, they can be pushed and pulled from/to
26the same git remote you are already using to collaborate with other people.
27
28`,
29
30		PersistentPreRun: func(cmd *cobra.Command, args []string) {
31			root := cmd.Root()
32
33			if GitExactTag == "undefined" {
34				GitExactTag = ""
35			}
36			root.Version = GitLastTag
37			if GitExactTag == "" {
38				root.Version = fmt.Sprintf("%s-dev-%.10s", root.Version, GitCommit)
39			}
40		},
41
42		// For the root command, force the execution of the PreRun
43		// even if we just display the help. This is to make sure that we check
44		// the repository and give the user early feedback.
45		Run: func(cmd *cobra.Command, args []string) {
46			if err := cmd.Help(); err != nil {
47				os.Exit(1)
48			}
49		},
50
51		SilenceUsage:      true,
52		DisableAutoGenTag: true,
53
54		// Custom bash code to connect the git completion for "git bug" to the
55		// git-bug completion for "git-bug"
56		BashCompletionFunction: `
57_git_bug() {
58    __start_git-bug "$@"
59}
60`,
61	}
62
63	cmd.AddCommand(newAddCommand())
64	cmd.AddCommand(newBridgeCommand())
65	cmd.AddCommand(newCommandsCommand())
66	cmd.AddCommand(newCommentCommand())
67	cmd.AddCommand(newDeselectCommand())
68	cmd.AddCommand(newLabelCommand())
69	cmd.AddCommand(newLsCommand())
70	cmd.AddCommand(newLsIdCommand())
71	cmd.AddCommand(newLsLabelCommand())
72	cmd.AddCommand(newPullCommand())
73	cmd.AddCommand(newPushCommand())
74	cmd.AddCommand(newRmCommand())
75	cmd.AddCommand(newSelectCommand())
76	cmd.AddCommand(newShowCommand())
77	cmd.AddCommand(newStatusCommand())
78	cmd.AddCommand(newTermUICommand())
79	cmd.AddCommand(newTitleCommand())
80	cmd.AddCommand(newUserCommand())
81	cmd.AddCommand(newVersionCommand())
82	cmd.AddCommand(newWebUICommand())
83
84	return cmd
85}
86
87func Execute() {
88	if err := NewRootCommand().Execute(); err != nil {
89		os.Exit(1)
90	}
91}