Skip to content

Commit f38444b

Browse files
lancelkingland
andauthored
src: interstitial commit version (#1817) (#1850)
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. Co-authored-by: Luke Kingland <luke@lukekingland.com>
1 parent 62081fb commit f38444b

File tree

4 files changed

+32
-39
lines changed

4 files changed

+32
-39
lines changed

Makefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ DATE := $(shell date -u +"%Y%m%dT%H%M%SZ")
2323
HASH := $(shell git rev-parse --short HEAD 2>/dev/null)
2424
VTAG := $(shell git tag --points-at HEAD | head -1)
2525
VTAG := $(shell [ -z $(VTAG) ] && echo $(ETAG) || echo $(VTAG))
26-
VERS ?= $(shell [ -z $(VTAG) ] && echo 'tip' || echo $(VTAG) )
27-
LDFLAGS := "-X main.date=$(DATE) -X main.vers=$(VERS) -X main.hash=$(HASH)"
26+
VERS ?= $(shell git describe --tags --match 'v*')
27+
KVER ?= $(shell git describe --tags --match 'knative-*')
28+
LDFLAGS := "-X main.date=$(DATE) -X main.vers=$(VERS) -X main.kver=$(KVER) -X main.hash=$(HASH)"
2829

2930
# All Code prerequisites, including generated files, etc.
3031
CODE := $(shell find . -name '*.go') generate/zz_filesystem_generated.go go.mod schema/func_yaml-schema.json

cmd/func/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
// Statically-populated build metadata set by `make build`.
18-
var date, vers, hash string
18+
var vers, kver, hash string
1919

2020
func main() {
2121
ctx, cancel := context.WithCancel(context.Background())
@@ -33,8 +33,8 @@ func main() {
3333
cfg := cmd.RootCommandConfig{
3434
Name: "func",
3535
Version: cmd.Version{
36-
Date: date,
3736
Vers: vers,
37+
Kver: kver,
3838
Hash: hash,
3939
}}
4040

cmd/root.go

+24-33
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"os"
77
"path/filepath"
88
"strings"
9-
"time"
109

10+
"github.com/Masterminds/semver"
1111
"github.com/ory/viper"
1212
"github.com/spf13/cobra"
1313
"github.com/spf13/pflag"
@@ -329,61 +329,52 @@ func cwd() (cwd string) {
329329
return cwd
330330
}
331331

332+
// Version information populated on build.
332333
type Version struct {
333334
// Date of compilation
334335
Date string
335336
// Version tag of the git commit, or 'tip' if no tag.
336337
Vers string
338+
// Kver is the version of knative in which func was most recently
339+
// If the build is not tagged as being released with a specific Knative
340+
// build, this is the most recent version of knative along with a suffix
341+
// consisting of the number of commits which have been added since it was
342+
// included in Knative.
343+
Kver string
337344
// Hash of the currently active git commit on build.
338345
Hash string
339346
// Verbose printing enabled for the string representation.
340347
Verbose bool
341348
}
342349

343-
// Return the stringification of the Version struct, which takes into account
344-
// the verbosity setting.
350+
// Return the stringification of the Version struct.
345351
func (v Version) String() string {
346352
if v.Verbose {
347353
return v.StringVerbose()
348354
}
349-
350-
// Ensure that the value returned is parseable as a semver, with the special
351-
// value v0.0.0 as the default indicating there is no version information
352-
// available.
353-
if strings.HasPrefix(v.Vers, "v") {
354-
// TODO: this is the naive approach, perhaps consider actually parse it
355-
// using the semver lib
356-
return v.Vers
357-
}
358-
359-
// Any non-semver value is invalid, and thus indistinguishable from a
360-
// nonexistent version value, so the default zero value of v0.0.0 is used.
361-
return "v0.0.0"
355+
_ = semver.MustParse(v.Vers)
356+
return v.Vers
362357
}
363358

364-
// StringVerbose returns the verbose version of the version stringification.
365-
// The format returned is [semver]-[hash]-[date] where the special value
366-
// 'v0.0.0' and 'source' are used when version is not available and/or the
367-
// libray has been built from source, respectively.
359+
// StringVerbose returns the version along with extended version metadata.
368360
func (v Version) StringVerbose() string {
369361
var (
370362
vers = v.Vers
363+
kver = v.Kver
371364
hash = v.Hash
372-
date = v.Date
373365
)
374-
if vers == "" {
375-
vers = "v0.0.0"
376-
}
377-
if hash == "" {
378-
hash = "source"
379-
}
380-
if date == "" {
381-
date = time.Now().Format(time.RFC3339)
366+
if strings.HasPrefix(kver, "knative-") {
367+
kver = strings.Split(kver, "-")[1]
382368
}
383-
funcVersion := fmt.Sprintf("%s-%s-%s", vers, hash, date)
384-
return fmt.Sprintf("Version: %s\n"+
385-
"SocatImage: %s\n"+
386-
"TarImage: %s", funcVersion,
369+
return fmt.Sprintf(
370+
"Version: %s\n"+
371+
"Knative: %s\n"+
372+
"Commit: %s\n"+
373+
"SocatImage: %s\n"+
374+
"TarImage: %s\n",
375+
vers,
376+
kver,
377+
hash,
387378
k8s.SocatImage,
388379
k8s.TarImage)
389380
}

cmd/root_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ func TestVerbose(t *testing.T) {
170170
{
171171
name: "verbose as version's flag",
172172
args: []string{"version", "-v"},
173-
want: "Version: v0.42.0-cafe-1970-01-01",
174-
wantLF: 3,
173+
want: "Version: v0.42.0",
174+
wantLF: 6,
175175
},
176176
{
177177
name: "no verbose",
@@ -192,6 +192,7 @@ func TestVerbose(t *testing.T) {
192192
Date: "1970-01-01",
193193
Vers: "v0.42.0",
194194
Hash: "cafe",
195+
Kver: "v1.10.0",
195196
}})
196197

197198
cmd.SetArgs(tt.args)

0 commit comments

Comments
 (0)