root.go

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