root.go

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