1// Package commands contains the CLI commands
  2package commands
  3
  4import (
  5	"fmt"
  6	"os"
  7	"runtime/debug"
  8
  9	"github.com/spf13/cobra"
 10
 11	bridgecmd "github.com/git-bug/git-bug/commands/bridge"
 12	bugcmd "github.com/git-bug/git-bug/commands/bug"
 13	"github.com/git-bug/git-bug/commands/execenv"
 14	usercmd "github.com/git-bug/git-bug/commands/user"
 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			root.Version = getVersion()
 37		},
 38
 39		// For the root command, force the execution of the PreRun
 40		// even if we just display the help. This is to make sure that we check
 41		// the repository and give the user early feedback.
 42		Run: func(cmd *cobra.Command, args []string) {
 43			if err := cmd.Help(); err != nil {
 44				os.Exit(1)
 45			}
 46		},
 47
 48		SilenceUsage:      true,
 49		DisableAutoGenTag: true,
 50	}
 51
 52	const entityGroup = "entity"
 53	const uiGroup = "ui"
 54	const remoteGroup = "remote"
 55
 56	cmd.AddGroup(&cobra.Group{ID: entityGroup, Title: "Entities"})
 57	cmd.AddGroup(&cobra.Group{ID: uiGroup, Title: "Interactive interfaces"})
 58	cmd.AddGroup(&cobra.Group{ID: remoteGroup, Title: "Interaction with the outside world"})
 59
 60	addCmdWithGroup := func(child *cobra.Command, groupID string) {
 61		cmd.AddCommand(child)
 62		child.GroupID = groupID
 63	}
 64
 65	env := execenv.NewEnv()
 66
 67	addCmdWithGroup(bugcmd.NewBugCommand(env), entityGroup)
 68	addCmdWithGroup(usercmd.NewUserCommand(env), entityGroup)
 69	addCmdWithGroup(newLabelCommand(env), entityGroup)
 70
 71	addCmdWithGroup(newTermUICommand(env), uiGroup)
 72	addCmdWithGroup(newWebUICommand(env), uiGroup)
 73
 74	addCmdWithGroup(newPullCommand(env), remoteGroup)
 75	addCmdWithGroup(newPushCommand(env), remoteGroup)
 76	addCmdWithGroup(bridgecmd.NewBridgeCommand(env), remoteGroup)
 77
 78	cmd.AddCommand(newVersionCommand(env))
 79	cmd.AddCommand(newWipeCommand(env))
 80
 81	return cmd
 82}
 83
 84func Execute() {
 85	if err := NewRootCommand().Execute(); err != nil {
 86		os.Exit(1)
 87	}
 88}
 89
 90func getVersion() string {
 91	if GitExactTag == "undefined" {
 92		GitExactTag = ""
 93	}
 94
 95	if GitExactTag != "" {
 96		// we are exactly on a tag --> release version
 97		return GitLastTag
 98	}
 99
100	if GitLastTag != "" {
101		// not exactly on a tag --> dev version
102		return fmt.Sprintf("%s-dev-%.10s", GitLastTag, GitCommit)
103	}
104
105	// we don't have commit information, try golang build info
106	if commit, dirty, err := getCommitAndDirty(); err == nil {
107		if dirty {
108			return fmt.Sprintf("dev-%.10s-dirty", commit)
109		}
110		return fmt.Sprintf("dev-%.10s", commit)
111	}
112
113	return "dev-unknown"
114}
115
116func getCommitAndDirty() (commit string, dirty bool, err error) {
117	info, ok := debug.ReadBuildInfo()
118	if !ok {
119		return "", false, fmt.Errorf("unable to read build info")
120	}
121
122	var commitFound bool
123
124	// get the commit and modified status
125	// (that is the flag for repository dirty or not)
126	for _, kv := range info.Settings {
127		switch kv.Key {
128		case "vcs.revision":
129			commit = kv.Value
130			commitFound = true
131		case "vcs.modified":
132			if kv.Value == "true" {
133				dirty = true
134			}
135		}
136	}
137
138	if !commitFound {
139		return "", false, fmt.Errorf("no commit found")
140	}
141
142	return commit, dirty, nil
143}