root.go

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