Skip to content

Commit

Permalink
Refactor info command and rename to version
Browse files Browse the repository at this point in the history
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
  • Loading branch information
arcticicestudio committed Oct 18, 2019
1 parent 532e680 commit 2cd8e0a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 63 deletions.
44 changes: 0 additions & 44 deletions cmd/snowsaw/info/info.go

This file was deleted.

6 changes: 3 additions & 3 deletions cmd/snowsaw/snowsaw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions cmd/snowsaw/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (C) 2017-present Arctic Ice Studio <[email protected]>
// Copyright (C) 2017-present Sven Greb <[email protected]>
//
// Project: snowsaw
// Repository: https://github.com/arcticicestudio/snowsaw
// License: MIT

// Author: Arctic Ice Studio <[email protected]>
// Author: Sven Greb <[email protected]>
// 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
}
28 changes: 18 additions & 10 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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"+
Expand All @@ -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.
Expand Down
15 changes: 9 additions & 6 deletions pkg/config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

0 comments on commit 2cd8e0a

Please sign in to comment.