From 09bec115064d5bd52c5e1f6ea87903dfa4934b2a Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sat, 2 Mar 2024 23:15:56 +0000 Subject: [PATCH] jpictl: add --version and version command from `git describe` using go:generate and embed Signed-off-by: Alejandro Mery --- .gitignore | 1 + cmd/jpictl/main.go | 7 +++++-- cmd/jpictl/version.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 cmd/jpictl/version.go diff --git a/.gitignore b/.gitignore index d36977d..ea6e957 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .tmp +.version diff --git a/cmd/jpictl/main.go b/cmd/jpictl/main.go index 5199ca6..fe10deb 100644 --- a/cmd/jpictl/main.go +++ b/cmd/jpictl/main.go @@ -2,6 +2,8 @@ package main import ( + _ "embed" + "github.com/spf13/cobra" ) @@ -12,8 +14,9 @@ const ( var ( rootCmd = &cobra.Command{ - Use: CmdName, - Short: "control tool for jpi.cloud", + Use: CmdName, + Short: "control tool for jpi.cloud", + Version: version, } ) diff --git a/cmd/jpictl/version.go b/cmd/jpictl/version.go new file mode 100644 index 0000000..7eefacf --- /dev/null +++ b/cmd/jpictl/version.go @@ -0,0 +1,31 @@ +package main + +import ( + _ "embed" + + "fmt" + "os" + + "github.com/spf13/cobra" +) + +//go:generate sh -c "git describe | tr -d '\r\n' > .version" +//go:embed .version +var version string + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Returns jpictl's version", + Args: cobra.NoArgs, + Run: func(_ *cobra.Command, _ []string) { + _, _ = fmt.Fprintf(os.Stdout, "%s\n", version) + }, +} + +func init() { + if version == "" { + version = "undetermined" + } + + rootCmd.AddCommand(versionCmd) +}