Skip to content

Commit

Permalink
Refactor version command to cobra lib
Browse files Browse the repository at this point in the history
Signed-off-by: Marco Franssen <marco.franssen@philips.com>
  • Loading branch information
marcofranssen committed Nov 30, 2021
1 parent d5263fa commit 95c63d2
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 47 deletions.
64 changes: 29 additions & 35 deletions cmd/slsa-provenance/cli/version.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
package cli

import (
"context"
"encoding/json"
"flag"
"fmt"
"io"
"runtime"
"strings"
"text/tabwriter"

"github.com/peterbourgon/ff/v3/ffcli"
"github.com/spf13/cobra"
)

// Version creates an instance of *cobra.Command to print version info
func Version() *cobra.Command {
var outputJSON bool

cmd := &cobra.Command{
Use: "version",
Short: fmt.Sprintf("Prints the %s version", CLIName),
Long: fmt.Sprintf("Prints the %s version", CLIName),
RunE: func(cmd *cobra.Command, args []string) error {
v := VersionInfo()
res := v.String()
if outputJSON {
j, err := v.JSONString()
if err != nil {
return fmt.Errorf("unable to generate JSON from version info: %w", err)
}
res = j
}
fmt.Fprintln(cmd.OutOrStdout(), res)
return nil
},
}

cmd.Flags().BoolVar(&outputJSON, "json", false, "print version information as JSON")

return cmd
}

// Base version information.
//
// This is the fallback data used when version information from git is not
Expand All @@ -29,37 +54,6 @@ var (
buildDate = "unknown"
)

// Version creates an instance of *ffcli.Command to print version info
func Version(w io.Writer) *ffcli.Command {
var (
flagset = flag.NewFlagSet("slsa-provenance version", flag.ExitOnError)
outJSON = flagset.Bool("json", false, "print JSON instead of text")
)

flagset.SetOutput(w)

return &ffcli.Command{
Name: "version",
ShortUsage: "slsa-provenance version",
ShortHelp: "Prints the slsa-provenance version",
FlagSet: flagset,
Exec: func(ctx context.Context, args []string) error {
v := VersionInfo()
res := v.String()
if *outJSON {
j, err := v.JSONString()
if err != nil {
return fmt.Errorf("unable to generate JSON from version info: %w", err)
}
res = j
}

fmt.Fprintln(w, res)
return nil
},
}
}

// Info holds the version information of the binary
type Info struct {
GitVersion string `json:"git_version,omitempty"`
Expand Down
16 changes: 4 additions & 12 deletions cmd/slsa-provenance/cli/version_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package cli_test

import (
"context"
"fmt"
"runtime"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -15,9 +13,6 @@ import (
func TestVersionCliText(t *testing.T) {
assert := assert.New(t)

sb := strings.Builder{}
cli := cli.Version(&sb)

expected := fmt.Sprintf(`GitVersion: devel
GitCommit: unknown
GitTreeState: unknown
Expand All @@ -28,17 +23,14 @@ Platform: %s/%s
`, runtime.Version(), runtime.Compiler, runtime.GOOS, runtime.GOARCH)

err := cli.ParseAndRun(context.Background(), nil)
output, err := executeCommand(cli.Version())
assert.NoError(err)
assert.Equal(expected, sb.String())
assert.Equal(expected, output)
}

func TestVersionCliJSON(t *testing.T) {
assert := assert.New(t)

sb := strings.Builder{}
cli := cli.Version(&sb)

expected := fmt.Sprintf(`{
"git_version": "devel",
"git_commit": "unknown",
Expand All @@ -50,7 +42,7 @@ func TestVersionCliJSON(t *testing.T) {
}
`, runtime.Version(), runtime.Compiler, runtime.GOOS, runtime.GOARCH)

err := cli.ParseAndRun(context.Background(), []string{"-json"})
output, err := executeCommand(cli.Version(), "--json")
assert.NoError(err)
assert.Equal(expected, sb.String())
assert.Equal(expected, output)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/cobra v1.2.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa // indirect
golang.org/x/net v0.0.0-20211108170745-6635138e15ea // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
Loading

0 comments on commit 95c63d2

Please sign in to comment.