spruce/version.go
Dan Anglin b9dbdb2c61
refactor: embed *flag.FlagSet
Embed *flag.FlagSet directly into the subcommand types.
2023-08-11 19:26:24 +01:00

47 lines
761 B
Go

package main
import (
"flag"
"fmt"
)
type VersionCommand struct {
*flag.FlagSet
fullVersion bool
}
var (
binaryVersion string
buildTime string
goVersion string
gitCommit string
)
func NewVersionCommand() *VersionCommand {
vc := VersionCommand{
FlagSet: flag.NewFlagSet("version", flag.ExitOnError),
}
vc.BoolVar(&vc.fullVersion, "full", false, "prints the full version")
return &vc
}
func (v *VersionCommand) Run() error {
var version string
if v.fullVersion {
fullVersionFmt := `Spruce
Version: %s
Git commit: %s
Go version: %s
Build date: %s
`
version = fmt.Sprintf(fullVersionFmt, binaryVersion, gitCommit, goVersion, buildTime)
} else {
version = binaryVersion + "\n"
}
fmt.Print(version)
return nil
}