From 7a30dd99dc7c05827ba11050505c476799bb2932 Mon Sep 17 00:00:00 2001 From: Balazs Nadasdi Date: Wed, 3 Nov 2021 13:43:02 +0100 Subject: [PATCH] feat: add version subcommand (#197) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `version` subcommand without any flags prints out a general version information, good to check what version we have. ❯ ./bin/flintlockd_amd64 version flintlock v0.1.0-alpha.1-11-af7a1a9 The `version` subcommand with `--short` flag prints out only the version, useful for scripting. ❯ ./bin/flintlockd_amd64 version --short v0.1.0-alpha.1-11-af7a1a9 The `version` subcommand with `--long` flag prints out a detailed version information, useful for bug reports. ❯ ./bin/flintlockd_amd64 version --long flintlock Version: v0.1.0-alpha.1-12-gaf7a1a9 CommitHash: af7a1a9 BuildDate: 2021-11-03T11:38:43Z == Why a subcommand and not a `-v` or `-V` flag? Because it can be confusing. Some applications are using `-v` for version, some are using `-V` for version because `-v` is the verbose flag. Go, kubectl, and Helm follows the same logic, they have a `version` subcommand. == Different flags * I find it annoying to parse version information from `version` subcommands all the time when I want to automate something, that's the `--short`. * I find it useful to print out everything we have for debugging or filing bug reports, that's the `--long` flag. * I find it useful to print out the package/application name, so even if the binary was renamed I can see what is the name of the application, but without extra information like commit hash or build date. That's the no-flag option. == Moved the log.Info call In general, it's useful to print out the info log on `run` and `gw`, but not for `version`. Fixes #163 --- internal/command/gw/gw.go | 9 ++++++ internal/command/root.go | 55 ++++++++++++++++++++++++++++++++++--- internal/command/run/run.go | 9 ++++++ internal/version/version.go | 4 +++ 4 files changed, 73 insertions(+), 4 deletions(-) diff --git a/internal/command/gw/gw.go b/internal/command/gw/gw.go index 6491eeae..cc5a70da 100644 --- a/internal/command/gw/gw.go +++ b/internal/command/gw/gw.go @@ -15,6 +15,7 @@ import ( mvmv1 "github.com/weaveworks/flintlock/api/services/microvm/v1alpha1" cmdflags "github.com/weaveworks/flintlock/internal/command/flags" "github.com/weaveworks/flintlock/internal/config" + "github.com/weaveworks/flintlock/internal/version" "github.com/weaveworks/flintlock/pkg/flags" "github.com/weaveworks/flintlock/pkg/log" ) @@ -27,6 +28,14 @@ func NewCommand(cfg *config.Config) *cobra.Command { PreRunE: func(c *cobra.Command, _ []string) error { flags.BindCommandToViper(c) + logger := log.GetLogger(c.Context()) + logger.Infof( + "flintlockd, version=%s, built_on=%s, commit=%s", + version.Version, + version.BuildDate, + version.CommitHash, + ) + return nil }, RunE: func(c *cobra.Command, _ []string) error { diff --git a/internal/command/root.go b/internal/command/root.go index 58bdb486..40bfbe20 100644 --- a/internal/command/root.go +++ b/internal/command/root.go @@ -29,9 +29,6 @@ func NewRootCommand() (*cobra.Command, error) { return fmt.Errorf("configuring logging: %w", err) } - logger := log.GetLogger(cmd.Context()) - logger.Infof("flintlockd, version=%s, built_on=%s, commit=%s", version.Version, version.BuildDate, version.CommitHash) - return nil }, RunE: func(c *cobra.Command, _ []string) error { @@ -63,7 +60,7 @@ func initCobra() { viper.AddConfigPath("$HOME/.config/flintlockd/") } - viper.ReadInConfig() //nolint: errcheck + _ = viper.ReadInConfig() } func addRootSubCommands(cmd *cobra.Command, cfg *config.Config) error { @@ -73,9 +70,59 @@ func addRootSubCommands(cmd *cobra.Command, cfg *config.Config) error { } cmd.AddCommand(runCmd) + cmd.AddCommand(versionCommand()) gwCmd := gw.NewCommand(cfg) cmd.AddCommand(gwCmd) return nil } + +func versionCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "version", + Short: "Print the version number of flintlock", + RunE: func(cmd *cobra.Command, args []string) error { + var ( + long, short bool + err error + ) + + if long, err = cmd.Flags().GetBool("long"); err != nil { + return nil + } + + if short, err = cmd.Flags().GetBool("short"); err != nil { + return nil + } + + if short { + fmt.Fprintln(cmd.OutOrStdout(), version.Version) + + return nil + } + + if long { + fmt.Fprintf( + cmd.OutOrStdout(), + "%s\n Version: %s\n CommitHash: %s\n BuildDate: %s\n", + version.PackageName, + version.Version, + version.CommitHash, + version.BuildDate, + ) + + return nil + } + + fmt.Fprintf(cmd.OutOrStdout(), "%s %s\n", version.PackageName, version.Version) + + return nil + }, + } + + _ = cmd.Flags().Bool("long", false, "Print long version information") + _ = cmd.Flags().Bool("short", false, "Print short version information") + + return cmd +} diff --git a/internal/command/run/run.go b/internal/command/run/run.go index 3156c6ba..0ff3cca7 100644 --- a/internal/command/run/run.go +++ b/internal/command/run/run.go @@ -19,6 +19,7 @@ import ( cmdflags "github.com/weaveworks/flintlock/internal/command/flags" "github.com/weaveworks/flintlock/internal/config" "github.com/weaveworks/flintlock/internal/inject" + "github.com/weaveworks/flintlock/internal/version" "github.com/weaveworks/flintlock/pkg/defaults" "github.com/weaveworks/flintlock/pkg/flags" "github.com/weaveworks/flintlock/pkg/log" @@ -32,6 +33,14 @@ func NewCommand(cfg *config.Config) (*cobra.Command, error) { PreRunE: func(c *cobra.Command, _ []string) error { flags.BindCommandToViper(c) + logger := log.GetLogger(c.Context()) + logger.Infof( + "flintlockd, version=%s, built_on=%s, commit=%s", + version.Version, + version.BuildDate, + version.CommitHash, + ) + return nil }, RunE: func(c *cobra.Command, _ []string) error { diff --git a/internal/version/version.go b/internal/version/version.go index 6c833291..f5d1aaa4 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -1,5 +1,9 @@ package version +// PackageName is the name of the package, not just FlintlockD, +// all commands fall under this name. +const PackageName = "flintlock" + var ( Version = "undefined" // Specifies the app version BuildDate = "undefined" // Specifies the build date