root.go

 1package commands
 2
 3import (
 4	"os"
 5
 6	"github.com/spf13/cobra"
 7
 8	bridgecmd "github.com/git-bug/git-bug/commands/bridge"
 9	bugcmd "github.com/git-bug/git-bug/commands/bug"
10	"github.com/git-bug/git-bug/commands/execenv"
11	usercmd "github.com/git-bug/git-bug/commands/user"
12)
13
14func NewRootCommand(version string) *cobra.Command {
15	cmd := &cobra.Command{
16		Use:   execenv.RootCommandName,
17		Short: "A bug tracker embedded in Git",
18		Long: `git-bug is a bug tracker embedded in git.
19
20git-bug use git objects to store the bug tracking separated from the files
21history. As bugs are regular git objects, they can be pushed and pulled from/to
22the same git remote you are already using to collaborate with other people.
23
24`,
25
26		PersistentPreRun: func(cmd *cobra.Command, args []string) {
27			root := cmd.Root()
28			root.Version = version
29		},
30
31		// For the root command, force the execution of the PreRun
32		// even if we just display the help. This is to make sure that we check
33		// the repository and give the user early feedback.
34		Run: func(cmd *cobra.Command, args []string) {
35			if err := cmd.Help(); err != nil {
36				os.Exit(1)
37			}
38		},
39
40		SilenceUsage:      true,
41		DisableAutoGenTag: true,
42	}
43
44	const entityGroup = "entity"
45	const uiGroup = "ui"
46	const remoteGroup = "remote"
47
48	cmd.AddGroup(&cobra.Group{ID: entityGroup, Title: "Entities"})
49	cmd.AddGroup(&cobra.Group{ID: uiGroup, Title: "Interactive interfaces"})
50	cmd.AddGroup(&cobra.Group{ID: remoteGroup, Title: "Interaction with the outside world"})
51
52	addCmdWithGroup := func(child *cobra.Command, groupID string) {
53		cmd.AddCommand(child)
54		child.GroupID = groupID
55	}
56
57	env := execenv.NewEnv()
58	cmd.PersistentFlags().StringArrayVarP(&env.RepoPath, "repo-path", "C", []string{}, "Path to the git repository")
59
60	addCmdWithGroup(bugcmd.NewBugCommand(env), entityGroup)
61	addCmdWithGroup(usercmd.NewUserCommand(env), entityGroup)
62	addCmdWithGroup(newLabelCommand(env), entityGroup)
63
64	addCmdWithGroup(newTermUICommand(env), uiGroup)
65	addCmdWithGroup(newWebUICommand(env), uiGroup)
66
67	addCmdWithGroup(newPullCommand(env), remoteGroup)
68	addCmdWithGroup(newPushCommand(env), remoteGroup)
69	addCmdWithGroup(bridgecmd.NewBridgeCommand(env), remoteGroup)
70
71	cmd.AddCommand(newVersionCommand(env))
72	cmd.AddCommand(newWipeCommand(env))
73
74	return cmd
75}