Skip to content

Commit 64a432e

Browse files
Refactor info command and rename to version (#95)
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 Resolves GH-94
1 parent 532e680 commit 64a432e

File tree

5 files changed

+69
-63
lines changed

5 files changed

+69
-63
lines changed

cmd/snowsaw/info/info.go

Lines changed: 0 additions & 44 deletions
This file was deleted.

cmd/snowsaw/snowsaw.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"github.com/spf13/cobra"
2121

2222
"github.com/arcticicestudio/snowsaw/cmd/snowsaw/bootstrap"
23-
"github.com/arcticicestudio/snowsaw/cmd/snowsaw/info"
23+
info "github.com/arcticicestudio/snowsaw/cmd/snowsaw/version"
2424
"github.com/arcticicestudio/snowsaw/pkg/config"
2525
"github.com/arcticicestudio/snowsaw/pkg/config/builder"
2626
"github.com/arcticicestudio/snowsaw/pkg/config/source/file"
@@ -76,12 +76,12 @@ func init() {
7676
"comma-separated paths to snowblock base directories")
7777

7878
// Set the app version information for the automatically generated `version` flag.
79-
rootCmd.Version = color.CyanString(config.Version)
79+
rootCmd.Version = color.CyanString(config.AppVersion)
8080
rootCmd.SetVersionTemplate(`{{printf "%s\n" .Version}}`)
8181

8282
// Create and register all subcommands.
83-
rootCmd.AddCommand(info.NewInfoCmd())
8483
rootCmd.AddCommand(bootstrap.NewBootstrapCmd())
84+
rootCmd.AddCommand(info.NewVersionCmd())
8585
}
8686

8787
// initConfig searches and loads either the default application configuration file paths or the explicit file at the

cmd/snowsaw/version/version.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (C) 2017-present Arctic Ice Studio <[email protected]>
2+
// Copyright (C) 2017-present Sven Greb <[email protected]>
3+
//
4+
// Project: snowsaw
5+
// Repository: https://github.com/arcticicestudio/snowsaw
6+
// License: MIT
7+
8+
// Author: Arctic Ice Studio <[email protected]>
9+
// Author: Sven Greb <[email protected]>
10+
// Since: 0.4.0
11+
12+
// Package version provides the version command to print more detailed application version information.
13+
package version
14+
15+
import (
16+
"fmt"
17+
18+
"github.com/fatih/color"
19+
"github.com/spf13/cobra"
20+
21+
"github.com/arcticicestudio/snowsaw/pkg/config"
22+
)
23+
24+
// NewVersionCmd creates and configures a new `version` command.
25+
func NewVersionCmd() *cobra.Command {
26+
versionCmd := &cobra.Command{
27+
Use: "version",
28+
Short: "Prints more detailed application version information",
29+
Run: func(cmd *cobra.Command, args []string) {
30+
fmt.Println(fmt.Sprintf("%s %s build at %s with %s",
31+
color.CyanString(config.ProjectName),
32+
color.BlueString(config.AppVersion),
33+
color.GreenString(config.AppVersionBuildDateTime),
34+
color.BlueString(config.AppVersionGoRuntime)))
35+
},
36+
}
37+
38+
return versionCmd
39+
}

magefile.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"os"
2626
"os/exec"
2727
"path/filepath"
28+
"runtime"
2829
"strconv"
2930
"strings"
3031
"time"
@@ -143,8 +144,9 @@ var (
143144
goPath string
144145

145146
// Arguments for the `-ldflags` flag to pass on each `go tool link` invocation.
146-
ldFlags = "-X $PACKAGE_NAME/pkg/config.BuildDateTime=$BUILD_DATE_TIME" +
147-
" -X $PACKAGE_NAME/pkg/config.Version=$VERSION"
147+
ldFlags = "-X $PACKAGE_NAME/pkg/config.AppVersion=$APP_VERSION" +
148+
" -X $PACKAGE_NAME/pkg/config.AppVersionBuildDateTime=$APP_VERSION_BUILD_DATE_TIME" +
149+
" -X $PACKAGE_NAME/pkg/config.AppVersionGoRuntime=$APP_VERSION_GO_RUNTIME"
148150

149151
// The tool used to lint all Go source files.
150152
// 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) {
500502
}
501503

502504
// Use the version from the application configuration by default or...
503-
semVersion, semVerErr := semver.NewVersion(config.Version)
505+
semVersion, semVerErr := semver.NewVersion(config.AppVersion)
504506
version := &appVersion{Version: semVersion}
505507
if semVerErr != nil {
506508
return nil, fmt.Errorf("failed to parse default version from application configuration: %s", semVerErr)
507509
}
508510
if len(tagCandidates) == 0 {
509-
prt.Infof("No Git tag found, using defined version %s as fallback", color.CyanString(config.Version))
511+
prt.Infof("No Git tag found, using defined version %s as fallback", color.CyanString(config.AppVersion))
510512
// ...the latest Git tag from the current branch if at least one has been found.
511513
} else {
512514
semVersion, semVerErr = semver.NewVersion(tagCandidates[0].ref.Name().Short())
@@ -568,8 +570,12 @@ func getEnvFlags() map[string]string {
568570
prt.Infof(
569571
"Injecting %s:\n"+
570572
" Build Date: %s\n"+
571-
" Version: %s",
572-
color.BlueString("LDFLAGS"), color.CyanString(buildDate), color.CyanString(version.String()))
573+
" Version: %s\n"+
574+
" Go Runtime: %s",
575+
color.BlueString("LDFLAGS"),
576+
color.CyanString(buildDate),
577+
color.CyanString(version.String()),
578+
color.CyanString(runtime.Version()))
573579

574580
prt.Infof(
575581
"Injecting %s:\n"+
@@ -582,10 +588,12 @@ func getEnvFlags() map[string]string {
582588
color.BlueString("GCFLAGS"), color.CyanString(pwd))
583589

584590
return map[string]string{
585-
"BUILD_DATE_TIME": buildDate,
586-
"PACKAGE_NAME": config.PackageName,
587-
"PROJECT_ROOT": pwd,
588-
"VERSION": version.String()}
591+
"APP_VERSION": version.String(),
592+
"APP_VERSION_BUILD_DATE_TIME": buildDate,
593+
"APP_VERSION_GO_RUNTIME": runtime.Version(),
594+
"PACKAGE_NAME": config.PackageName,
595+
"PROJECT_ROOT": pwd,
596+
}
589597
}
590598

591599
// getExecutablePath returns the path to the executable for the given package/module.

pkg/config/constants.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,21 @@ var (
4141
// AppConfigPaths is the default paths the application will search for configuration files.
4242
AppConfigPaths []*file.File
4343

44+
// AppVersion is the application version.
45+
AppVersion = "0.0.0"
46+
47+
// AppVersionBuildDateTime is the date and time when this application version was built.
48+
AppVersionBuildDateTime string
49+
50+
// AppVersionGoRuntime is the Go runtime version with which this application was built.
51+
AppVersionGoRuntime string
52+
4453
availableTaskRunner = []snowblock.TaskRunner{
4554
&clean.Clean{},
4655
&link.Link{},
4756
&shell.Shell{},
4857
}
4958

50-
// BuildDateTime is the date and time this application was build.
51-
BuildDateTime string
52-
5359
// SnowblockTaskRunnerRegistry is the application-wide registry for snowblock task runner.
5460
SnowblockTaskRunnerRegistry = task.NewRegistry()
55-
56-
// Version is the application version.
57-
Version = "0.0.0"
5861
)

0 commit comments

Comments
 (0)