-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add version command to slsa-provenance cli
- Loading branch information
1 parent
b9a038f
commit 1de67f4
Showing
3 changed files
with
127 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"runtime" | ||
"strings" | ||
"text/tabwriter" | ||
|
||
"github.com/peterbourgon/ff/v3/ffcli" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// Base version information. | ||
// | ||
// This is the fallback data used when version information from git is not | ||
// provided via go ldflags (e.g. via Makefile). | ||
var ( | ||
// Output of "git describe". The prerequisite is that the branch should be | ||
// tagged using the correct versioning strategy. | ||
GitVersion string = "devel" | ||
// SHA1 from git, output of $(git rev-parse HEAD) | ||
gitCommit = "unknown" | ||
// State of git tree, either "clean" or "dirty" | ||
gitTreeState = "unknown" | ||
// Build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ') | ||
buildDate = "unknown" | ||
) | ||
|
||
func Version() *ffcli.Command { | ||
var ( | ||
flagset = flag.NewFlagSet("slsa-provenance version", flag.ExitOnError) | ||
outJSON = flagset.Bool("json", false, "print JSON instead of text") | ||
) | ||
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 errors.Wrap(err, "unable to generate JSON from version info") | ||
} | ||
res = j | ||
} | ||
|
||
fmt.Println(res) | ||
return nil | ||
}, | ||
} | ||
} | ||
|
||
type Info struct { | ||
GitVersion string `json:"git_version,omitempty"` | ||
GitCommit string `json:"git_commit,omitempty"` | ||
GitTreeState string `json:"git_tree_state,omitempty"` | ||
BuildDate string `json:"build_date,omitempty"` | ||
GoVersion string `json:"go_version,omitempty"` | ||
Compiler string `json:"compiler,omitempty"` | ||
Platform string `json:"platform,omitempty"` | ||
} | ||
|
||
func VersionInfo() Info { | ||
return Info{ | ||
GitVersion: GitVersion, | ||
GitCommit: gitCommit, | ||
GitTreeState: gitTreeState, | ||
BuildDate: buildDate, | ||
GoVersion: runtime.Version(), | ||
Compiler: runtime.Compiler, | ||
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), | ||
} | ||
} | ||
|
||
// String returns the string representation of the version info | ||
func (i *Info) String() string { | ||
b := strings.Builder{} | ||
w := tabwriter.NewWriter(&b, 0, 0, 2, ' ', 0) | ||
|
||
fmt.Fprintf(w, "GitVersion:\t%s\n", i.GitVersion) | ||
fmt.Fprintf(w, "GitCommit:\t%s\n", i.GitCommit) | ||
fmt.Fprintf(w, "GitTreeState:\t%s\n", i.GitTreeState) | ||
fmt.Fprintf(w, "BuildDate:\t%s\n", i.BuildDate) | ||
fmt.Fprintf(w, "GoVersion:\t%s\n", i.GoVersion) | ||
fmt.Fprintf(w, "Compiler:\t%s\n", i.Compiler) | ||
fmt.Fprintf(w, "Platform:\t%s\n", i.Platform) | ||
|
||
w.Flush() | ||
return b.String() | ||
} | ||
|
||
// JSONString returns the JSON representation of the version info | ||
func (i *Info) JSONString() (string, error) { | ||
b, err := json.MarshalIndent(i, "", " ") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(b), nil | ||
} |
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