1package commands
 2
 3import (
 4	"os"
 5
 6	"github.com/spf13/cobra"
 7
 8	"github.com/git-bug/git-bug/commands/bridge"
 9	"github.com/git-bug/git-bug/commands/bug"
10	"github.com/git-bug/git-bug/commands/execenv"
11	"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
59	addCmdWithGroup(bugcmd.NewBugCommand(env), entityGroup)
60	addCmdWithGroup(usercmd.NewUserCommand(env), entityGroup)
61	addCmdWithGroup(newLabelCommand(env), entityGroup)
62
63	addCmdWithGroup(newTermUICommand(env), uiGroup)
64	addCmdWithGroup(newWebUICommand(env), uiGroup)
65
66	addCmdWithGroup(newPullCommand(env), remoteGroup)
67	addCmdWithGroup(newPushCommand(env), remoteGroup)
68	addCmdWithGroup(bridgecmd.NewBridgeCommand(env), remoteGroup)
69
70	cmd.AddCommand(newVersionCommand(env))
71	cmd.AddCommand(newWipeCommand(env))
72
73	return cmd
74}