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
24By default, git-bug manages bugs and users in the Git repository containing
25the current working directory.  This behavior can be changed using the --git-dir
26flag described below, or by setting the GIT-DIR environment variable, which
27takes precedence over the CLI flag.
28`,
29
30		PersistentPreRun: func(cmd *cobra.Command, args []string) {
31			root := cmd.Root()
32			root.Version = version
33		},
34
35		// For the root command, force the execution of the PreRun
36		// even if we just display the help. This is to make sure that we check
37		// the repository and give the user early feedback.
38		Run: func(cmd *cobra.Command, args []string) {
39			if err := cmd.Help(); err != nil {
40				os.Exit(1)
41			}
42		},
43
44		SilenceUsage:      true,
45		DisableAutoGenTag: true,
46	}
47
48	const entityGroup = "entity"
49	const uiGroup = "ui"
50	const remoteGroup = "remote"
51
52	cmd.AddGroup(&cobra.Group{ID: entityGroup, Title: "Entities"})
53	cmd.AddGroup(&cobra.Group{ID: uiGroup, Title: "Interactive interfaces"})
54	cmd.AddGroup(&cobra.Group{ID: remoteGroup, Title: "Interaction with the outside world"})
55
56	addCmdWithGroup := func(child *cobra.Command, groupID string) {
57		cmd.AddCommand(child)
58		child.GroupID = groupID
59	}
60
61	env := execenv.NewEnv()
62	cmd.PersistentFlags().StringVar(&env.GitDir, "git-dir", "", `run as if git-bug was started in <path>`)
63
64	addCmdWithGroup(bugcmd.NewBugCommand(env), entityGroup)
65	addCmdWithGroup(usercmd.NewUserCommand(env), entityGroup)
66	addCmdWithGroup(newLabelCommand(env), entityGroup)
67
68	addCmdWithGroup(newTermUICommand(env), uiGroup)
69	addCmdWithGroup(newWebUICommand(env), uiGroup)
70
71	addCmdWithGroup(newPullCommand(env), remoteGroup)
72	addCmdWithGroup(newPushCommand(env), remoteGroup)
73	addCmdWithGroup(bridgecmd.NewBridgeCommand(env), remoteGroup)
74
75	cmd.AddCommand(newVersionCommand(env))
76	cmd.AddCommand(newWipeCommand(env))
77
78	return cmd
79}