-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from mateimicu/add-version
- Loading branch information
Showing
8 changed files
with
120 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Package cmd offers CLI functionality | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
goversion "go.hein.dev/go-version" | ||
) | ||
|
||
var ( | ||
shortened bool = false | ||
output string = "json" | ||
) | ||
|
||
func newVersionCommand(version, commit, date string) *cobra.Command { | ||
versionCmd := &cobra.Command{ | ||
Use: "version", | ||
Short: "Version will output the current build information", | ||
Long: "", | ||
|
||
RunE: func(cmd *cobra.Command, args []string) error { | ||
resp := goversion.FuncWithOutput(shortened, version, commit, date, output) | ||
cmd.Print(resp) | ||
return nil | ||
}, | ||
} | ||
|
||
versionCmd.Flags().BoolVarP(&shortened, "short", "s", false, "Print just the version number.") | ||
versionCmd.Flags().StringVarP(&output, "output", "o", "json", "Output format. One of 'yaml' or 'json'.") | ||
|
||
return versionCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Package cmd offers CLI functionality | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_Version(t *testing.T) { | ||
cases := []struct { | ||
Cmd []string | ||
}{ | ||
{Cmd: []string{"version"}}, | ||
{Cmd: []string{"version", "-o", "json"}}, | ||
{Cmd: []string{"version", "-o", "yaml"}}, | ||
} | ||
for _, tt := range cases { | ||
testname := fmt.Sprintf("command %v", tt.Cmd) | ||
t.Run(testname, func(t *testing.T) { | ||
dir, err := ioutil.TempDir("", ".kube") | ||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
defer os.RemoveAll(dir) | ||
|
||
kubeconfigPath := filepath.Join(dir, "kubeconfig") | ||
version := "mock-version" | ||
commit := "mock-commit" | ||
date := "mock-date" | ||
cmd := NewRootCommand(version, commit, date) | ||
buf := new(strings.Builder) | ||
cmd.SetOut(buf) | ||
cmd.SetErr(buf) | ||
|
||
completCmd := append(tt.Cmd, "--kubeconfig-path") | ||
completCmd = append(completCmd, kubeconfigPath) | ||
|
||
cmd.SetArgs(completCmd) | ||
err = cmd.Execute() | ||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
out := buf.String() | ||
assert.Contains(t, out, version) | ||
assert.Contains(t, out, date) | ||
assert.Contains(t, out, date) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters