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/MichaelMure/git-bug/commands/bridge"
 12	bugcmd "github.com/MichaelMure/git-bug/commands/bug"
 13	"github.com/MichaelMure/git-bug/commands/execenv"
 14	usercmd "github.com/MichaelMure/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(newCommandsCommand(env))
 79	cmd.AddCommand(newVersionCommand(env))
 80	cmd.AddCommand(newWipeCommand(env))
 81
 82	return cmd
 83}
 84
 85func Execute() {
 86	if err := NewRootCommand().Execute(); err != nil {
 87		os.Exit(1)
 88	}
 89}
 90
 91func getVersion() string {
 92	if GitExactTag == "undefined" {
 93		GitExactTag = ""
 94	}
 95
 96	if GitExactTag != "" {
 97		// we are exactly on a tag --> release version
 98		return GitLastTag
 99	}
100
101	if GitLastTag != "" {
102		// not exactly on a tag --> dev version
103		return fmt.Sprintf("%s-dev-%.10s", GitLastTag, GitCommit)
104	}
105
106	// we don't have commit information, try golang build info
107	if commit, dirty, err := getCommitAndDirty(); err == nil {
108		if dirty {
109			return fmt.Sprintf("dev-%.10s-dirty", commit)
110		}
111		return fmt.Sprintf("dev-%.10s", commit)
112	}
113
114	return "dev-unknown"
115}
116
117func getCommitAndDirty() (commit string, dirty bool, err error) {
118	info, ok := debug.ReadBuildInfo()
119	if !ok {
120		return "", false, fmt.Errorf("unable to read build info")
121	}
122
123	var commitFound bool
124
125	// get the commit and modified status
126	// (that is the flag for repository dirty or not)
127	for _, kv := range info.Settings {
128		switch kv.Key {
129		case "vcs.revision":
130			commit = kv.Value
131			commitFound = true
132		case "vcs.modified":
133			if kv.Value == "true" {
134				dirty = true
135			}
136		}
137	}
138
139	if !commitFound {
140		return "", false, fmt.Errorf("no commit found")
141	}
142
143	return commit, dirty, nil
144}