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

[release-1.10]: src: interstitial commit version (#1817) #1850

Merged
merged 1 commit into from
Jul 7, 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 @@ -23,8 +23,9 @@ DATE := $(shell date -u +"%Y%m%dT%H%M%SZ")
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.date=$(DATE) -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
4 changes: 2 additions & 2 deletions 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 date, vers, hash string
var vers, kver, hash string

func main() {
ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -33,8 +33,8 @@ func main() {
cfg := cmd.RootCommandConfig{
Name: "func",
Version: cmd.Version{
Date: date,
Vers: vers,
Kver: kver,
Hash: hash,
}}

Expand Down
57 changes: 24 additions & 33 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/Masterminds/semver"
"github.com/ory/viper"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -328,61 +328,52 @@ func cwd() (cwd string) {
return cwd
}

// Version information populated on build.
type Version struct {
// Date of compilation
Date string
// 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]-[date] 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
date = v.Date
)
if vers == "" {
vers = "v0.0.0"
}
if hash == "" {
hash = "source"
}
if date == "" {
date = time.Now().Format(time.RFC3339)
if strings.HasPrefix(kver, "knative-") {
kver = strings.Split(kver, "-")[1]
}
funcVersion := fmt.Sprintf("%s-%s-%s", vers, hash, date)
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-1970-01-01",
wantLF: 3,
want: "Version: v0.42.0",
wantLF: 6,
},
{
name: "no verbose",
Expand All @@ -192,6 +192,7 @@ func TestVerbose(t *testing.T) {
Date: "1970-01-01",
Vers: "v0.42.0",
Hash: "cafe",
Kver: "v1.10.0",
}})

cmd.SetArgs(tt.args)
Expand Down