Feature: Adds support for --version

Resolves: #24
This commit is contained in:
kenjones 2017-09-23 00:45:31 -04:00
parent 6980be3800
commit 72bd88cfa5
7 changed files with 137 additions and 3 deletions

View File

@ -5,8 +5,8 @@ export GIT_COMMIT = $(shell git rev-parse --short HEAD)
export GIT_DIRTY = $(shell test -n "$$(git status --porcelain)" && echo "+CHANGES" || true)
export GIT_DESCRIBE = $(shell git describe --tags --always)
LDFLAGS :=
# LDFLAGS += -X ${IMPORT_PATH}/${PROJECT}.Commit=${GIT_COMMIT}
# LDFLAGS += -X ${IMPORT_PATH}/${PROJECT}.Version=${GIT_DESCRIBE}
LDFLAGS += -X main.GitCommit=${GIT_COMMIT}${GIT_DIRTY}
LDFLAGS += -X main.GitDescribe=${GIT_DESCRIBE}
# Windows environment?
CYG_CHECK := $(shell hash cygpath 2>/dev/null && echo 1)
@ -28,6 +28,7 @@ endif
DEV_IMAGE := ${PROJECT}_dev
DOCKRUN := docker run --rm \
-e LDFLAGS="${LDFLAGS}" \
-v ${ROOT}/vendor:/go/src \
-v ${ROOT}:/${PROJECT}/src/${IMPORT_PATH} \
-w /${PROJECT}/src/${IMPORT_PATH} \

View File

@ -29,6 +29,7 @@ Check out the [documentation](http://mikefarah.github.io/yaml/) for more detaile
```
Usage:
yaml [flags]
yaml [command]
Available Commands:
@ -43,6 +44,7 @@ Flags:
-j, --tojson output as json
-t, --trim trim yaml output (default true)
-v, --verbose verbose mode
-V, --version Print version information and quit
Use "yaml [command] --help" for more information about a command.
```

View File

@ -97,6 +97,28 @@ func TestRootCmd_ToJsonShort(t *testing.T) {
}
}
func TestRootCmd_VersionShort(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "-V")
if result.Error != nil {
t.Error(result.Error)
}
if !strings.Contains(result.Output, "yaml version") {
t.Error("expected version message to be printed out, but the message was not found.")
}
}
func TestRootCmd_VersionLong(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "--version")
if result.Error != nil {
t.Error(result.Error)
}
if !strings.Contains(result.Output, "yaml version") {
t.Error("expected version message to be printed out, but the message was not found.")
}
}
func TestReadCmd(t *testing.T) {
cmd := getRootCommand()
result := runCmd(cmd, "read examples/sample.yaml b.c")

View File

@ -3,5 +3,5 @@
# This assumes that gonative and gox is installed as per the 'one time setup' instructions
# at https://github.com/inconshreveable/gonative
gox -output="build/{{.Dir}}_{{.OS}}_{{.Arch}}"
gox -ldflags "${LDFLAGS}" -output="build/{{.Dir}}_{{.OS}}_{{.Arch}}"

52
version.go Normal file
View File

@ -0,0 +1,52 @@
package main
import (
"fmt"
"strings"
)
// The git commit that was compiled. This will be filled in by the compiler.
var (
GitCommit string
GitDescribe string
// Version is main version number that is being run at the moment.
Version = "1.13.0"
// VersionPrerelease is a pre-release marker for the version. If this is "" (empty string)
// then it means that it is a final release. Otherwise, this is a pre-release
// such as "dev" (in development), "beta", "rc1", etc.
VersionPrerelease = "dev"
)
// ProductName is the name of the product
const ProductName = "yaml"
// GetVersionDisplay composes the parts of the version in a way that's suitable
// for displaying to humans.
func GetVersionDisplay() string {
return fmt.Sprintf("%s version %s\n", ProductName, getHumanVersion())
}
func getHumanVersion() string {
version := Version
if GitDescribe != "" {
version = GitDescribe
}
release := VersionPrerelease
if GitDescribe == "" && release == "" {
release = "dev"
}
if release != "" {
if !strings.Contains(version, release) {
version += fmt.Sprintf("-%s", release)
}
if GitCommit != "" {
version += fmt.Sprintf(" (%s)", GitCommit)
}
}
// Strip off any single quotes added by the git information.
return strings.Replace(version, "'", "", -1)
}

46
version_test.go Normal file
View File

@ -0,0 +1,46 @@
package main
import "testing"
func TestGetVersionDisplay(t *testing.T) {
tests := []struct {
name string
want string
}{
{
name: "Display Version",
want: ProductName + " version " + Version + "-dev\n",
},
}
for _, tt := range tests {
if got := GetVersionDisplay(); got != tt.want {
t.Errorf("%q. GetVersionDisplay() = %v, want %v", tt.name, got, tt.want)
}
}
}
func Test_getHumanVersion(t *testing.T) {
GitDescribe = "e42813d"
GitCommit = "e42813d+CHANGES"
var wanted string
if VersionPrerelease == "" {
wanted = GitDescribe
} else {
wanted = "e42813d-" + VersionPrerelease + " (e42813d+CHANGES)"
}
tests := []struct {
name string
want string
}{
{
name: "Git Variables defined",
want: wanted,
},
}
for _, tt := range tests {
if got := getHumanVersion(); got != tt.want {
t.Errorf("%q. getHumanVersion() = %v, want %v", tt.name, got, tt.want)
}
}
}

11
yaml.go
View File

@ -19,6 +19,7 @@ var writeScript = ""
var outputToJSON = false
var overwriteFlag = false
var verbose = false
var version = false
var log = logging.MustGetLogger("yaml")
func main() {
@ -32,6 +33,15 @@ func main() {
func newCommandCLI() *cobra.Command {
var rootCmd = &cobra.Command{
Use: "yaml",
RunE: func(cmd *cobra.Command, args []string) error {
if version {
cmd.Print(GetVersionDisplay())
return nil
}
cmd.Println(cmd.UsageString())
return nil
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
var format = logging.MustStringFormatter(
`%{color}%{time:15:04:05} %{shortfunc} [%{level:.4s}]%{color:reset} %{message}`,
@ -52,6 +62,7 @@ func newCommandCLI() *cobra.Command {
rootCmd.PersistentFlags().BoolVarP(&trimOutput, "trim", "t", true, "trim yaml output")
rootCmd.PersistentFlags().BoolVarP(&outputToJSON, "tojson", "j", false, "output as json")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose mode")
rootCmd.Flags().BoolVarP(&version, "version", "V", false, "Print version information and quit")
rootCmd.AddCommand(createReadCmd(), createWriteCmd(), createNewCmd(), createMergeCmd())
rootCmd.SetOutput(os.Stdout)