2023-08-12 10:26:23 +01:00
|
|
|
package cmd
|
2023-08-11 18:33:26 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2023-08-21 03:07:06 +01:00
|
|
|
"os"
|
2023-08-13 17:45:33 +01:00
|
|
|
"strings"
|
2023-08-11 18:33:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type VersionCommand struct {
|
2023-08-11 19:26:24 +01:00
|
|
|
*flag.FlagSet
|
2023-08-13 17:45:33 +01:00
|
|
|
summary string
|
2023-08-12 10:26:23 +01:00
|
|
|
fullVersion bool
|
2023-08-11 18:33:26 +01:00
|
|
|
binaryVersion string
|
|
|
|
buildTime string
|
|
|
|
goVersion string
|
|
|
|
gitCommit string
|
2023-08-12 10:26:23 +01:00
|
|
|
}
|
2023-08-11 18:33:26 +01:00
|
|
|
|
2023-08-20 06:14:10 +01:00
|
|
|
func NewVersionCommand(binaryVersion, buildTime, goVersion, gitCommit, name, summary string) *VersionCommand {
|
|
|
|
command := VersionCommand{
|
|
|
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
2023-08-12 10:26:23 +01:00
|
|
|
binaryVersion: binaryVersion,
|
|
|
|
buildTime: buildTime,
|
|
|
|
goVersion: goVersion,
|
|
|
|
gitCommit: gitCommit,
|
2023-08-20 06:14:10 +01:00
|
|
|
summary: summary,
|
2023-08-11 18:33:26 +01:00
|
|
|
}
|
|
|
|
|
2023-08-20 06:14:10 +01:00
|
|
|
command.BoolVar(&command.fullVersion, "full", false, "prints the full build information")
|
2023-08-13 17:45:33 +01:00
|
|
|
|
2023-08-20 06:14:10 +01:00
|
|
|
command.Usage = usageFunc(command.Name(), command.summary, command.FlagSet)
|
2023-08-11 18:33:26 +01:00
|
|
|
|
2023-08-20 06:14:10 +01:00
|
|
|
return &command
|
2023-08-11 18:33:26 +01:00
|
|
|
}
|
|
|
|
|
2023-08-20 06:14:10 +01:00
|
|
|
func (c *VersionCommand) Run() error {
|
2023-08-21 03:07:06 +01:00
|
|
|
var builder strings.Builder
|
|
|
|
|
2023-08-20 06:14:10 +01:00
|
|
|
if c.fullVersion {
|
2023-08-21 03:07:06 +01:00
|
|
|
fmt.Fprintf(
|
|
|
|
&builder,
|
|
|
|
"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,
|
|
|
|
)
|
2023-08-11 18:33:26 +01:00
|
|
|
} else {
|
2023-08-21 03:07:06 +01:00
|
|
|
fmt.Fprintln(&builder, c.binaryVersion)
|
2023-08-11 18:33:26 +01:00
|
|
|
}
|
|
|
|
|
2023-08-21 03:07:06 +01:00
|
|
|
fmt.Fprint(os.Stdout, builder.String())
|
|
|
|
|
2023-08-11 18:33:26 +01:00
|
|
|
return nil
|
|
|
|
}
|