git-bug/commands/root.go

94 lines
2.5 KiB
Go
Raw Normal View History

2018-09-16 14:50:53 +03:00
// Package commands contains the CLI commands
package commands
import (
"fmt"
2018-08-13 19:32:11 +03:00
"os"
"github.com/spf13/cobra"
"github.com/MichaelMure/git-bug/commands/bridge"
usercmd "github.com/MichaelMure/git-bug/commands/user"
"github.com/MichaelMure/git-bug/commands/bug"
"github.com/MichaelMure/git-bug/commands/execenv"
)
2020-06-28 19:26:29 +03:00
// These variables are initialized externally during the build. See the Makefile.
var GitCommit string
var GitLastTag string
var GitExactTag string
2020-06-28 19:26:29 +03:00
func NewRootCommand() *cobra.Command {
cmd := &cobra.Command{
Use: execenv.RootCommandName,
Short: "A bug tracker embedded in Git",
2020-06-28 19:26:29 +03:00
Long: `git-bug is a bug tracker embedded in git.
git-bug use git objects to store the bug tracking separated from the files
history. As bugs are regular git objects, they can be pushed and pulled from/to
2020-04-30 23:24:03 +03:00
the same git remote you are already using to collaborate with other people.
`,
2020-06-28 19:26:29 +03:00
PersistentPreRun: func(cmd *cobra.Command, args []string) {
root := cmd.Root()
if GitExactTag == "undefined" {
GitExactTag = ""
}
root.Version = GitLastTag
if GitExactTag == "" {
root.Version = fmt.Sprintf("%s-dev-%.10s", root.Version, GitCommit)
}
},
// For the root command, force the execution of the PreRun
// even if we just display the help. This is to make sure that we check
// the repository and give the user early feedback.
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Help(); err != nil {
os.Exit(1)
}
},
SilenceUsage: true,
DisableAutoGenTag: true,
}
const entityGroup = "entity"
const uiGroup = "ui"
const remoteGroup = "remote"
cmd.AddGroup(&cobra.Group{ID: entityGroup, Title: "Entities"})
2022-12-23 01:19:31 +03:00
cmd.AddGroup(&cobra.Group{ID: uiGroup, Title: "Interactive interfaces"})
cmd.AddGroup(&cobra.Group{ID: remoteGroup, Title: "Interaction with the outside world"})
addCmdWithGroup := func(child *cobra.Command, groupID string) {
cmd.AddCommand(child)
child.GroupID = groupID
}
addCmdWithGroup(bugcmd.NewBugCommand(), entityGroup)
addCmdWithGroup(usercmd.NewUserCommand(), entityGroup)
addCmdWithGroup(newLabelCommand(), entityGroup)
addCmdWithGroup(newTermUICommand(), uiGroup)
addCmdWithGroup(newWebUICommand(), uiGroup)
addCmdWithGroup(newPullCommand(), remoteGroup)
addCmdWithGroup(newPushCommand(), remoteGroup)
addCmdWithGroup(bridgecmd.NewBridgeCommand(), remoteGroup)
2020-06-28 19:26:29 +03:00
cmd.AddCommand(newCommandsCommand())
cmd.AddCommand(newVersionCommand())
return cmd
}
func Execute() {
2020-06-28 19:26:29 +03:00
if err := NewRootCommand().Execute(); err != nil {
os.Exit(1)
}
}