spruce/internal/cmd/version.go
Dan Anglin 4f90a4c8bb
fix: use default FlagSet for spruce help message
Use the default FlagSet to parse all the arguments and to set the
default help message for spruce. Arguments set after the subcommand are
still parsed by the subcommand's FlagSet.

The summaries for all subcommand are defined in one place in the main
function for consistency.

The internal/cmd.SpruceUsage function is replaced with the
spruceUsageFunc function in the main package which returns the usage
function which is set as the default usage function.

The format of the help message for spruce and the subcommands have been
updated with the inspiration of the help message from gopass.
2023-08-20 06:14:10 +01:00

46 lines
1.1 KiB
Go

package cmd
import (
"flag"
"fmt"
"strings"
)
type VersionCommand struct {
*flag.FlagSet
summary string
fullVersion bool
binaryVersion string
buildTime string
goVersion string
gitCommit string
}
func NewVersionCommand(binaryVersion, buildTime, goVersion, gitCommit, name, summary string) *VersionCommand {
command := VersionCommand{
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
binaryVersion: binaryVersion,
buildTime: buildTime,
goVersion: goVersion,
gitCommit: gitCommit,
summary: summary,
}
command.BoolVar(&command.fullVersion, "full", false, "prints the full build information")
command.Usage = usageFunc(command.Name(), command.summary, command.FlagSet)
return &command
}
func (c *VersionCommand) Run() error {
var b strings.Builder
if c.fullVersion {
fmt.Fprintf(&b, "Spruce\n Version: %s\n Git commit: %s\n Go version: %s\n Build date: %s\n", c.binaryVersion, c.gitCommit, c.goVersion, c.buildTime)
} else {
fmt.Fprintln(&b, c.binaryVersion)
}
fmt.Print(b.String())
return nil
}