From 2cd8e0af5035d8a594e7724d76bce913fcd29953 Mon Sep 17 00:00:00 2001 From: Arctic Ice Studio Date: Fri, 18 Oct 2019 14:07:07 +0200 Subject: [PATCH] Refactor `info` command and rename to `version` Previously the `info` command printed more detailed application information compared to the compact and machine-friendly `--version` global flag. The name of command was not optimal as it could give the impression that it provides more "info"rmation about one or more snowblocks that might be passed as argument(s). Therefore the command has been renamed to `version` which also matches the naming of many other Go CLI apps like Kubernetes [1] `kubectl` [2]. To also enhance the provided information the command now prints more application version details using the function implemented in GH-93. [1]: https://kubernetes.io [2]: https://github.com/kubernetes/kubernetes/tree/master/pkg/kubectl Epic GH-33 Relates to GH-93 GH-94 --- cmd/snowsaw/info/info.go | 44 ---------------------------------- cmd/snowsaw/snowsaw.go | 6 ++--- cmd/snowsaw/version/version.go | 39 ++++++++++++++++++++++++++++++ magefile.go | 28 ++++++++++++++-------- pkg/config/constants.go | 15 +++++++----- 5 files changed, 69 insertions(+), 63 deletions(-) delete mode 100644 cmd/snowsaw/info/info.go create mode 100644 cmd/snowsaw/version/version.go diff --git a/cmd/snowsaw/info/info.go b/cmd/snowsaw/info/info.go deleted file mode 100644 index b9212d2..0000000 --- a/cmd/snowsaw/info/info.go +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (C) 2017-present Arctic Ice Studio -// Copyright (C) 2017-present Sven Greb -// -// Project: snowsaw -// Repository: https://github.com/arcticicestudio/snowsaw -// License: MIT - -// Author: Arctic Ice Studio -// Author: Sven Greb -// Since: 0.4.0 - -// Package info provides the info command to print more detailed application information. -package info - -import ( - "fmt" - - "github.com/fatih/color" - "github.com/spf13/cobra" - - "github.com/arcticicestudio/snowsaw/pkg/config" -) - -// NewInfoCmd creates and configures a new `info` command. -func NewInfoCmd() *cobra.Command { - infoCmd := &cobra.Command{ - Use: "info", - Short: "Prints more detailed application information", - Run: func(cmd *cobra.Command, args []string) { - if config.BuildDateTime != "" { - fmt.Println(fmt.Sprintf("%s %s (build %s)", - color.CyanString(config.ProjectName), - color.BlueString(config.Version), - color.GreenString(config.BuildDateTime))) - } else { - fmt.Println(fmt.Sprintf("%s %s", - color.CyanString(config.ProjectName), - color.BlueString(config.Version))) - } - }, - } - - return infoCmd -} diff --git a/cmd/snowsaw/snowsaw.go b/cmd/snowsaw/snowsaw.go index 05206a5..daee728 100644 --- a/cmd/snowsaw/snowsaw.go +++ b/cmd/snowsaw/snowsaw.go @@ -20,7 +20,7 @@ import ( "github.com/spf13/cobra" "github.com/arcticicestudio/snowsaw/cmd/snowsaw/bootstrap" - "github.com/arcticicestudio/snowsaw/cmd/snowsaw/info" + info "github.com/arcticicestudio/snowsaw/cmd/snowsaw/version" "github.com/arcticicestudio/snowsaw/pkg/config" "github.com/arcticicestudio/snowsaw/pkg/config/builder" "github.com/arcticicestudio/snowsaw/pkg/config/source/file" @@ -76,12 +76,12 @@ func init() { "comma-separated paths to snowblock base directories") // Set the app version information for the automatically generated `version` flag. - rootCmd.Version = color.CyanString(config.Version) + rootCmd.Version = color.CyanString(config.AppVersion) rootCmd.SetVersionTemplate(`{{printf "%s\n" .Version}}`) // Create and register all subcommands. - rootCmd.AddCommand(info.NewInfoCmd()) rootCmd.AddCommand(bootstrap.NewBootstrapCmd()) + rootCmd.AddCommand(info.NewVersionCmd()) } // initConfig searches and loads either the default application configuration file paths or the explicit file at the diff --git a/cmd/snowsaw/version/version.go b/cmd/snowsaw/version/version.go new file mode 100644 index 0000000..bfadc1f --- /dev/null +++ b/cmd/snowsaw/version/version.go @@ -0,0 +1,39 @@ +// Copyright (C) 2017-present Arctic Ice Studio +// Copyright (C) 2017-present Sven Greb +// +// Project: snowsaw +// Repository: https://github.com/arcticicestudio/snowsaw +// License: MIT + +// Author: Arctic Ice Studio +// Author: Sven Greb +// Since: 0.4.0 + +// Package version provides the version command to print more detailed application version information. +package version + +import ( + "fmt" + + "github.com/fatih/color" + "github.com/spf13/cobra" + + "github.com/arcticicestudio/snowsaw/pkg/config" +) + +// NewVersionCmd creates and configures a new `version` command. +func NewVersionCmd() *cobra.Command { + versionCmd := &cobra.Command{ + Use: "version", + Short: "Prints more detailed application version information", + Run: func(cmd *cobra.Command, args []string) { + fmt.Println(fmt.Sprintf("%s %s build at %s with %s", + color.CyanString(config.ProjectName), + color.BlueString(config.AppVersion), + color.GreenString(config.AppVersionBuildDateTime), + color.BlueString(config.AppVersionGoRuntime))) + }, + } + + return versionCmd +} diff --git a/magefile.go b/magefile.go index 63c4c4f..7919749 100644 --- a/magefile.go +++ b/magefile.go @@ -25,6 +25,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strconv" "strings" "time" @@ -143,8 +144,9 @@ var ( goPath string // Arguments for the `-ldflags` flag to pass on each `go tool link` invocation. - ldFlags = "-X $PACKAGE_NAME/pkg/config.BuildDateTime=$BUILD_DATE_TIME" + - " -X $PACKAGE_NAME/pkg/config.Version=$VERSION" + ldFlags = "-X $PACKAGE_NAME/pkg/config.AppVersion=$APP_VERSION" + + " -X $PACKAGE_NAME/pkg/config.AppVersionBuildDateTime=$APP_VERSION_BUILD_DATE_TIME" + + " -X $PACKAGE_NAME/pkg/config.AppVersionGoRuntime=$APP_VERSION_GO_RUNTIME" // The tool used to lint all Go source files. // This is the same tool used by the https://golangci.com service that is also integrated in snowsaw's CI/CD pipeline. @@ -500,13 +502,13 @@ func getAppVersionFromGit() (*appVersion, error) { } // Use the version from the application configuration by default or... - semVersion, semVerErr := semver.NewVersion(config.Version) + semVersion, semVerErr := semver.NewVersion(config.AppVersion) version := &appVersion{Version: semVersion} if semVerErr != nil { return nil, fmt.Errorf("failed to parse default version from application configuration: %s", semVerErr) } if len(tagCandidates) == 0 { - prt.Infof("No Git tag found, using defined version %s as fallback", color.CyanString(config.Version)) + prt.Infof("No Git tag found, using defined version %s as fallback", color.CyanString(config.AppVersion)) // ...the latest Git tag from the current branch if at least one has been found. } else { semVersion, semVerErr = semver.NewVersion(tagCandidates[0].ref.Name().Short()) @@ -568,8 +570,12 @@ func getEnvFlags() map[string]string { prt.Infof( "Injecting %s:\n"+ " Build Date: %s\n"+ - " Version: %s", - color.BlueString("LDFLAGS"), color.CyanString(buildDate), color.CyanString(version.String())) + " Version: %s\n"+ + " Go Runtime: %s", + color.BlueString("LDFLAGS"), + color.CyanString(buildDate), + color.CyanString(version.String()), + color.CyanString(runtime.Version())) prt.Infof( "Injecting %s:\n"+ @@ -582,10 +588,12 @@ func getEnvFlags() map[string]string { color.BlueString("GCFLAGS"), color.CyanString(pwd)) return map[string]string{ - "BUILD_DATE_TIME": buildDate, - "PACKAGE_NAME": config.PackageName, - "PROJECT_ROOT": pwd, - "VERSION": version.String()} + "APP_VERSION": version.String(), + "APP_VERSION_BUILD_DATE_TIME": buildDate, + "APP_VERSION_GO_RUNTIME": runtime.Version(), + "PACKAGE_NAME": config.PackageName, + "PROJECT_ROOT": pwd, + } } // getExecutablePath returns the path to the executable for the given package/module. diff --git a/pkg/config/constants.go b/pkg/config/constants.go index 5c99024..7e31897 100644 --- a/pkg/config/constants.go +++ b/pkg/config/constants.go @@ -41,18 +41,21 @@ var ( // AppConfigPaths is the default paths the application will search for configuration files. AppConfigPaths []*file.File + // AppVersion is the application version. + AppVersion = "0.0.0" + + // AppVersionBuildDateTime is the date and time when this application version was built. + AppVersionBuildDateTime string + + // AppVersionGoRuntime is the Go runtime version with which this application was built. + AppVersionGoRuntime string + availableTaskRunner = []snowblock.TaskRunner{ &clean.Clean{}, &link.Link{}, &shell.Shell{}, } - // BuildDateTime is the date and time this application was build. - BuildDateTime string - // SnowblockTaskRunnerRegistry is the application-wide registry for snowblock task runner. SnowblockTaskRunnerRegistry = task.NewRegistry() - - // Version is the application version. - Version = "0.0.0" )