Skip to content

Commit

Permalink
add version
Browse files Browse the repository at this point in the history
  • Loading branch information
liujianping committed Jun 25, 2019
1 parent dfd87dc commit b1fb616
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/*
7 changes: 6 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"time"

"github.com/liujianping/ts/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -28,6 +29,10 @@ func RootCmd() *cobra.Command {
(timezone) $: ts -f "2019/06/25 23:30:10" -z "Asia/Shanghai"
`,
Run: func(cmd *cobra.Command, args []string) {
if viper.GetBool("version") {
fmt.Println(version.String())
os.Exit(0)
}
//pipe stdin
if len(args) == 0 {
info, err := os.Stdin.Stat()
Expand All @@ -42,7 +47,7 @@ func RootCmd() *cobra.Command {
exitForErr(Main(cmd, args))
},
}

cmd.Flags().BoolP("version", "v", false, "current version")
cmd.Flags().StringP("after", "a", "", "after compare")
cmd.Flags().StringP("before", "b", "", "before compare")
cmd.Flags().StringP("format", "f", "", "time format")
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.2.2
github.com/x-mod/errors v0.1.5
)
12 changes: 11 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@

package main

import "github.com/liujianping/ts/cmd"
import (
"github.com/liujianping/ts/cmd"
ver "github.com/liujianping/ts/version"
)

var (
version = "dev"
commit = "none"
date = "unknown"
)

func main() {
ver.Info(version, commit, date)
cmd.Execute()
}
21 changes: 21 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package version

import "fmt"

var (
version = "dev"
commit = "none"
date = "unknown"
)

//String version
func String() string {
return fmt.Sprintf("%v, commit %v, built at %v", version, commit, date)
}

//Info build info
func Info(v, m, d string) {
version = v
commit = m
date = d
}
28 changes: 28 additions & 0 deletions version/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package version

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestVersion(t *testing.T) {
tests := []struct {
name string
want string
}{
{
"default",
"dev, commit none, built at unknown",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
})
}
Info("v0.0.1", "now", "timestamp")
assert.Equal(t, "v0.0.1, commit now, built at timestamp", String())
}

0 comments on commit b1fb616

Please sign in to comment.