1// Package commands contains the CLI commands
2package commands
3
4import (
5 "fmt"
6 "os"
7
8 "github.com/spf13/cobra"
9
10 bridgecmd "github.com/MichaelMure/git-bug/commands/bridge"
11 bugcmd "github.com/MichaelMure/git-bug/commands/bug"
12 "github.com/MichaelMure/git-bug/commands/execenv"
13 usercmd "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 env := execenv.NewEnv()
72
73 addCmdWithGroup(bugcmd.NewBugCommand(env), entityGroup)
74 addCmdWithGroup(usercmd.NewUserCommand(env), entityGroup)
75 addCmdWithGroup(newLabelCommand(env), entityGroup)
76
77 addCmdWithGroup(newTermUICommand(env), uiGroup)
78 addCmdWithGroup(newWebUICommand(env), uiGroup)
79
80 addCmdWithGroup(newPullCommand(env), remoteGroup)
81 addCmdWithGroup(newPushCommand(env), remoteGroup)
82 addCmdWithGroup(bridgecmd.NewBridgeCommand(env), remoteGroup)
83
84 cmd.AddCommand(newCommandsCommand(env))
85 cmd.AddCommand(newVersionCommand(env))
86 cmd.AddCommand(newWipeCommand(env))
87
88 return cmd
89}
90
91func Execute() {
92 if err := NewRootCommand().Execute(); err != nil {
93 os.Exit(1)
94 }
95}