Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: improved version output #1817

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ BIN_WINDOWS ?= $(BIN)_windows_amd64.exe
HASH := $(shell git rev-parse --short HEAD 2>/dev/null)
VTAG := $(shell git tag --points-at HEAD | head -1)
VTAG := $(shell [ -z $(VTAG) ] && echo $(ETAG) || echo $(VTAG))
VERS ?= $(shell [ -z $(VTAG) ] && echo 'tip' || echo $(VTAG) )
LDFLAGS := "-X main.vers=$(VERS) -X main.hash=$(HASH)"
VERS ?= $(shell git describe --tags --match 'v*')
KVER ?= $(shell git describe --tags --match 'knative-*')
LDFLAGS := "-X main.date=$(DATE) -X main.vers=$(VERS) -X main.kver=$(KVER) -X main.hash=$(HASH)"

# All Code prerequisites, including generated files, etc.
CODE := $(shell find . -name '*.go') generate/zz_filesystem_generated.go go.mod schema/func_yaml-schema.json
Expand Down
3 changes: 2 additions & 1 deletion cmd/func/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

// Statically-populated build metadata set by `make build`.
var vers, hash string
var vers, kver, hash string

func main() {
ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -34,6 +34,7 @@ func main() {
Name: "func",
Version: cmd.Version{
Vers: vers,
Kver: kver,
Hash: hash,
}}

Expand Down
52 changes: 24 additions & 28 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"strings"

"github.com/Masterminds/semver"
"github.com/ory/viper"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -317,55 +318,50 @@ func cwd() (cwd string) {
return cwd
}

// Version information populated on build.
type Version struct {
// Version tag of the git commit, or 'tip' if no tag.
Vers string
// Kver is the version of knative in which func was most recently
// If the build is not tagged as being released with a specific Knative
// build, this is the most recent version of knative along with a suffix
// consisting of the number of commits which have been added since it was
// included in Knative.
Kver string
// Hash of the currently active git commit on build.
Hash string
// Verbose printing enabled for the string representation.
Verbose bool
}

// Return the stringification of the Version struct, which takes into account
// the verbosity setting.
// Return the stringification of the Version struct.
func (v Version) String() string {
if v.Verbose {
return v.StringVerbose()
}

// Ensure that the value returned is parseable as a semver, with the special
// value v0.0.0 as the default indicating there is no version information
// available.
if strings.HasPrefix(v.Vers, "v") {
// TODO: this is the naive approach, perhaps consider actually parse it
// using the semver lib
return v.Vers
}

// Any non-semver value is invalid, and thus indistinguishable from a
// nonexistent version value, so the default zero value of v0.0.0 is used.
return "v0.0.0"
_ = semver.MustParse(v.Vers)
return v.Vers
}

// StringVerbose returns the verbose version of the version stringification.
// The format returned is [semver]-[hash] where the special value
// 'v0.0.0' and 'source' are used when version is not available and/or the
// libray has been built from source, respectively.
// StringVerbose returns the version along with extended version metadata.
func (v Version) StringVerbose() string {
var (
vers = v.Vers
kver = v.Kver
hash = v.Hash
)
if vers == "" {
vers = "v0.0.0"
}
if hash == "" {
hash = "source"
if strings.HasPrefix(kver, "knative-") {
kver = strings.Split(kver, "-")[1]
}
funcVersion := fmt.Sprintf("%s-%s", vers, hash)
return fmt.Sprintf("Version: %s\n"+
"SocatImage: %s\n"+
"TarImage: %s", funcVersion,
return fmt.Sprintf(
"Version: %s\n"+
"Knative: %s\n"+
"Commit: %s\n"+
"SocatImage: %s\n"+
"TarImage: %s\n",
vers,
kver,
hash,
k8s.SocatImage,
k8s.TarImage)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ func TestVerbose(t *testing.T) {
{
name: "verbose as version's flag",
args: []string{"version", "-v"},
want: "Version: v0.42.0-cafe",
wantLF: 3,
want: "Version: v0.42.0",
wantLF: 6,
},
{
name: "no verbose",
Expand All @@ -191,6 +191,7 @@ func TestVerbose(t *testing.T) {
Version: Version{
Vers: "v0.42.0",
Hash: "cafe",
Kver: "v1.10.0",
}})

cmd.SetArgs(tt.args)
Expand Down
Loading