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
55	cmd.AddCommand(newAddCommand())
56	cmd.AddCommand(newBridgeCommand())
57	cmd.AddCommand(newCommandsCommand())
58	cmd.AddCommand(newCommentCommand())
59	cmd.AddCommand(newDeselectCommand())
60	cmd.AddCommand(newLabelCommand())
61	cmd.AddCommand(newLsCommand())
62	cmd.AddCommand(newLsIdCommand())
63	cmd.AddCommand(newLsLabelCommand())
64	cmd.AddCommand(newPullCommand())
65	cmd.AddCommand(newPushCommand())
66	cmd.AddCommand(newRmCommand())
67	cmd.AddCommand(newSelectCommand())
68	cmd.AddCommand(newShowCommand())
69	cmd.AddCommand(newStatusCommand())
70	cmd.AddCommand(newTermUICommand())
71	cmd.AddCommand(newTitleCommand())
72	cmd.AddCommand(newUserCommand())
73	cmd.AddCommand(newVersionCommand())
74	cmd.AddCommand(newWebUICommand())
75
76	return cmd
77}
78
79func Execute() {
80	if err := NewRootCommand().Execute(); err != nil {
81		os.Exit(1)
82	}
83}