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