spruce/version.go
Dan Anglin cee274318d
feat: add FlagSets to create new subcommands
Create new FlagSets to create new subcommands.

- The version subcommand prints the version and build info.
- The generate subcommand generates the CV PDF documentation.
2023-08-11 18:33:26 +01:00

55 lines
917 B
Go

package main
import (
"flag"
"fmt"
)
type VersionCommand struct {
fs *flag.FlagSet
fullVersion bool
}
var (
binaryVersion string
buildTime string
goVersion string
gitCommit string
)
func NewVersionCommand() *VersionCommand {
vc := VersionCommand{
fs: flag.NewFlagSet("version", flag.ExitOnError),
}
vc.fs.BoolVar(&vc.fullVersion, "full", false, "prints the full version")
return &vc
}
func (v *VersionCommand) Parse(args []string) error {
return v.fs.Parse(args)
}
func (v *VersionCommand) Name() string {
return v.fs.Name()
}
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
}