Skip to content

Commit

Permalink
src: interstitial commit version
Browse files Browse the repository at this point in the history
The Knative version is now included in version command verbose output

Building an unreleased version no longer returns v0.0.0, but instead the
value provided by `git describe --tags` which is the most recent tagged
release with a suffix consisting of the number of commits since that
release and the short hash.

Verbose output now always includes the current commit on a dedicated
line.
  • Loading branch information
lkingland committed Jun 19, 2023
1 parent cabba3f commit 6115367
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 31 deletions.
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

0 comments on commit 6115367

Please sign in to comment.