Skip to content

Commit

Permalink
Add version command to slsa-provenance cli
Browse files Browse the repository at this point in the history
  • Loading branch information
marcofranssen committed Oct 6, 2021
1 parent b9a038f commit 1de67f4
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 1 deletion.
19 changes: 18 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
GIT_TAG ?= dirty-tag
GIT_VERSION ?= $(shell git describe --tags --always --dirty)
GIT_HASH ?= $(shell git rev-parse HEAD)
DATE_FMT = +'%Y-%m-%dT%H:%M:%SZ'
SOURCE_DATE_EPOCH ?= $(shell git log -1 --pretty=%ct)
ifdef SOURCE_DATE_EPOCH
BUILD_DATE ?= $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null || date -u "$(DATE_FMT)")
else
BUILD_DATE ?= $(shell date "$(DATE_FMT)")
endif
GIT_TREESTATE = "clean"
DIFF = $(shell git diff --quiet >/dev/null 2>&1; if [ $$? -eq 1 ]; then echo "1"; fi)
ifeq ($(DIFF), 1)
GIT_TREESTATE = "dirty"
endif

PKG=github.com/philips-labs/slsa-provenance-action/cmd/slsa-provenance/cli
LDFLAGS="-X $(PKG).GitVersion=$(GIT_VERSION) -X $(PKG).gitCommit=$(GIT_HASH) -X $(PKG).gitTreeState=$(GIT_TREESTATE) -X $(PKG).buildDate=$(BUILD_DATE)"

GO_BUILD_FLAGS := -trimpath
GO_BUILD_FLAGS := -trimpath -ldflags $(LDFLAGS)
COMMANDS := slsa-provenance

.PHONY: help
Expand Down
106 changes: 106 additions & 0 deletions cmd/slsa-provenance/cli/version.go
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
}
3 changes: 3 additions & 0 deletions cmd/slsa-provenance/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ func main() {
FlagSet: rootFlagSet,
Subcommands: []*ffcli.Command{
cli.Generate(),
cli.Version(),
},
Exec: func(ctx context.Context, args []string) error {
fmt.Println("slsa-provenance")
fmt.Println()

return cli.Version().Exec(ctx, args)
},
}

Expand Down

0 comments on commit 1de67f4

Please sign in to comment.