Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor info command and rename to version #95

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <development@arcticicestudio.com>
// Copyright (C) 2017-present Sven Greb <development@svengreb.de>
//
// Project: snowsaw
// Repository: https://github.com/arcticicestudio/snowsaw
// License: MIT

// Author: Arctic Ice Studio <development@arcticicestudio.com>
// Author: Sven Greb <development@svengreb.de>
// 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"
)