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