diff options
Diffstat (limited to 'commands')
-rw-r--r-- | commands/commands.go | 80 | ||||
-rw-r--r-- | commands/root.go | 80 | ||||
-rw-r--r-- | commands/version.go | 87 | ||||
-rw-r--r-- | commands/wipe.go | 1 |
4 files changed, 51 insertions, 197 deletions
diff --git a/commands/commands.go b/commands/commands.go deleted file mode 100644 index 00c7b2dc7..000000000 --- a/commands/commands.go +++ /dev/null @@ -1,80 +0,0 @@ -package commands - -import ( - "sort" - - "github.com/spf13/cobra" - - "github.com/git-bug/git-bug/commands/execenv" -) - -type commandOptions struct { - desc bool -} - -func newCommandsCommand(env *execenv.Env) *cobra.Command { - options := commandOptions{} - - cmd := &cobra.Command{ - Use: "commands", - Short: "Display available commands.", - RunE: func(cmd *cobra.Command, args []string) error { - return runCommands(env, options) - }, - } - - flags := cmd.Flags() - flags.SortFlags = false - - flags.BoolVarP(&options.desc, "pretty", "p", false, - "Output the command description as well as Markdown compatible comment", - ) - - return cmd -} - -func runCommands(env *execenv.Env, opts commandOptions) error { - first := true - - var allCmds []*cobra.Command - queue := []*cobra.Command{NewRootCommand()} - - for len(queue) > 0 { - cmd := queue[0] - queue = queue[1:] - allCmds = append(allCmds, cmd) - queue = append(queue, cmd.Commands()...) - } - - sort.Sort(commandSorterByName(allCmds)) - - for _, cmd := range allCmds { - if !first { - env.Out.Println() - } - - first = false - - if opts.desc { - env.Out.Printf("# %s\n", cmd.Short) - } - - env.Out.Print(cmd.UseLine()) - - if opts.desc { - env.Out.Println() - } - } - - if !opts.desc { - env.Out.Println() - } - - return nil -} - -type commandSorterByName []*cobra.Command - -func (c commandSorterByName) Len() int { return len(c) } -func (c commandSorterByName) Swap(i, j int) { c[i], c[j] = c[j], c[i] } -func (c commandSorterByName) Less(i, j int) bool { return c[i].CommandPath() < c[j].CommandPath() } diff --git a/commands/root.go b/commands/root.go index e966751c7..1b64b5090 100644 --- a/commands/root.go +++ b/commands/root.go @@ -1,25 +1,17 @@ -// Package commands contains the CLI commands package commands import ( - "fmt" "os" - "runtime/debug" "github.com/spf13/cobra" - bridgecmd "github.com/git-bug/git-bug/commands/bridge" - bugcmd "github.com/git-bug/git-bug/commands/bug" + "github.com/git-bug/git-bug/commands/bridge" + "github.com/git-bug/git-bug/commands/bug" "github.com/git-bug/git-bug/commands/execenv" - usercmd "github.com/git-bug/git-bug/commands/user" + "github.com/git-bug/git-bug/commands/user" ) -// These variables are initialized externally during the build. See the Makefile. -var GitCommit string -var GitLastTag string -var GitExactTag string - -func NewRootCommand() *cobra.Command { +func NewRootCommand(version string) *cobra.Command { cmd := &cobra.Command{ Use: execenv.RootCommandName, Short: "A bug tracker embedded in Git", @@ -33,7 +25,7 @@ the same git remote you are already using to collaborate with other people. PersistentPreRun: func(cmd *cobra.Command, args []string) { root := cmd.Root() - root.Version = getVersion() + root.Version = version }, // For the root command, force the execution of the PreRun @@ -75,70 +67,8 @@ the same git remote you are already using to collaborate with other people. addCmdWithGroup(newPushCommand(env), remoteGroup) addCmdWithGroup(bridgecmd.NewBridgeCommand(env), remoteGroup) - cmd.AddCommand(newCommandsCommand(env)) cmd.AddCommand(newVersionCommand(env)) cmd.AddCommand(newWipeCommand(env)) return cmd } - -func Execute() { - if err := NewRootCommand().Execute(); err != nil { - os.Exit(1) - } -} - -func getVersion() string { - if GitExactTag == "undefined" { - GitExactTag = "" - } - - if GitExactTag != "" { - // we are exactly on a tag --> release version - return GitLastTag - } - - if GitLastTag != "" { - // not exactly on a tag --> dev version - return fmt.Sprintf("%s-dev-%.10s", GitLastTag, GitCommit) - } - - // we don't have commit information, try golang build info - if commit, dirty, err := getCommitAndDirty(); err == nil { - if dirty { - return fmt.Sprintf("dev-%.10s-dirty", commit) - } - return fmt.Sprintf("dev-%.10s", commit) - } - - return "dev-unknown" -} - -func getCommitAndDirty() (commit string, dirty bool, err error) { - info, ok := debug.ReadBuildInfo() - if !ok { - return "", false, fmt.Errorf("unable to read build info") - } - - var commitFound bool - - // get the commit and modified status - // (that is the flag for repository dirty or not) - for _, kv := range info.Settings { - switch kv.Key { - case "vcs.revision": - commit = kv.Value - commitFound = true - case "vcs.modified": - if kv.Value == "true" { - dirty = true - } - } - } - - if !commitFound { - return "", false, fmt.Errorf("no commit found") - } - - return commit, dirty, nil -} diff --git a/commands/version.go b/commands/version.go index 189a2e350..ffa4a180a 100644 --- a/commands/version.go +++ b/commands/version.go @@ -1,63 +1,66 @@ package commands import ( - "runtime" + "log/slog" "github.com/spf13/cobra" "github.com/git-bug/git-bug/commands/execenv" ) -type versionOptions struct { - number bool - commit bool - all bool -} +// TODO: 0.12.0: remove deprecated build vars +var ( + GitCommit string + GitLastTag string + GitExactTag string +) func newVersionCommand(env *execenv.Env) *cobra.Command { - options := versionOptions{} + return &cobra.Command{ + Use: "version", + Short: "Print version information", + Example: "git bug version", + Long: ` +Print version information. - cmd := &cobra.Command{ - Use: "version", - Short: "Show git-bug version information", - Run: func(cmd *cobra.Command, args []string) { - runVersion(env, options, cmd.Root()) - }, - } +Format: + git-bug <version> [commit[/dirty]] <compiler version> <platform> <arch> - flags := cmd.Flags() - flags.SortFlags = false +Format Description: + <version> may be one of: + - A semantic version string, prefixed with a "v", e.g. v1.2.3 + - "undefined" (if not provided, or built with an invalid version string) - flags.BoolVarP(&options.number, "number", "n", false, - "Only show the version number", - ) - flags.BoolVarP(&options.commit, "commit", "c", false, - "Only show the commit hash", - ) - flags.BoolVarP(&options.all, "all", "a", false, - "Show all version information", - ) + [commit], if present, is the commit hash that was checked out during the + build. This may be suffixed with '/dirty' if there were local file + modifications. This is indicative of your build being patched, or modified in + some way from the commit. - return cmd -} + <compiler version> is the version of the go compiler used for the build. -func runVersion(env *execenv.Env, opts versionOptions, root *cobra.Command) { - if opts.all { - env.Out.Printf("%s version: %s\n", execenv.RootCommandName, root.Version) - env.Out.Printf("System version: %s/%s\n", runtime.GOARCH, runtime.GOOS) - env.Out.Printf("Golang version: %s\n", runtime.Version()) - return - } + <platform> is the target platform (GOOS). - if opts.number { - env.Out.Println(root.Version) - return + <arch> is the target architecture (GOARCH). +`, + Run: func(cmd *cobra.Command, args []string) { + defer warnDeprecated() + env.Out.Printf("%s %s", execenv.RootCommandName, cmd.Root().Version) + }, } +} - if opts.commit { - env.Out.Println(GitCommit) - return +// warnDeprecated warns about deprecated build variables +// TODO: 0.12.0: remove support for old build tags +func warnDeprecated() { + msg := "please contact your package maintainer" + reason := "deprecated build variable" + if GitLastTag != "" { + slog.Warn(msg, "reason", reason, "name", "GitLastTag", "value", GitLastTag) + } + if GitExactTag != "" { + slog.Warn(msg, "reason", reason, "name", "GitExactTag", "value", GitExactTag) + } + if GitCommit != "" { + slog.Warn(msg, "reason", reason, "name", "GitCommit", "value", GitCommit) } - - env.Out.Printf("%s version: %s\n", execenv.RootCommandName, root.Version) } diff --git a/commands/wipe.go b/commands/wipe.go index 2f378e902..d9dab42ad 100644 --- a/commands/wipe.go +++ b/commands/wipe.go @@ -33,6 +33,7 @@ func runWipe(env *execenv.Env) error { _ = env.Backend.Close() return err } + err = env.Backend.LocalConfig().RemoveAll("git-bug") if err != nil { _ = env.Backend.Close() |