From dccaf366118159b327f9125324c07c5c753b7d21 Mon Sep 17 00:00:00 2001 From: Navid Shaikh Date: Tue, 20 Aug 2019 03:55:59 +0530 Subject: [PATCH] kn version command to list supported serving versions and APIs (#370) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #369 - Replace `Dependencies:` with `Support:` - Add list of supported Serving versions kn can work with - Add list of supported Serving APIs kn can work with ``` ✗ ./kn version Version: v20190816-local-e36089f-dirty Build Date: 2019-08-16 13:08:26 Git Revision: e36089f Support: - Serving: v0.8.0 v0.7.1 - API(s): v1alpha1 ``` --- CHANGELOG.adoc | 3 +++ pkg/kn/commands/version.go | 24 ++++++++++++++++++-- pkg/kn/commands/version_test.go | 39 +++++++++++++++++---------------- 3 files changed, 45 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 366f7218e..a3f86626f 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -46,6 +46,9 @@ | Support traffic splitting and tagging targets | https://github.com/knative/client/pull/345[#345] +| 🎁 +| kn version command shows supported Serving and API versions +| https://github.com/knative/client/pull/370[#370] |=== diff --git a/pkg/kn/commands/version.go b/pkg/kn/commands/version.go index 8a69f1f88..622a26900 100644 --- a/pkg/kn/commands/version.go +++ b/pkg/kn/commands/version.go @@ -16,6 +16,7 @@ package commands import ( "fmt" + "strings" "github.com/spf13/cobra" ) @@ -25,6 +26,18 @@ var BuildDate string var GitRevision string var ServingVersion string +// VersionsAPIs hold the list of supported versions and APIs for component kn can work with +type VersionsAPIs struct { + Versions, APIs []string +} + +// update this var as we increase the serving version in go.mod +var knServingDep = "v0.8.0" +var supportMatrix = map[string]*VersionsAPIs{ + knServingDep: {[]string{"v0.8.0", "v0.7.1"}, []string{"v1alpha1"}}, +} + +// NewVersionCommand implements 'kn version' command func NewVersionCommand(p *KnParams) *cobra.Command { versionCmd := &cobra.Command{ Use: "version", @@ -33,8 +46,15 @@ func NewVersionCommand(p *KnParams) *cobra.Command { fmt.Printf("Version: %s\n", Version) fmt.Printf("Build Date: %s\n", BuildDate) fmt.Printf("Git Revision: %s\n", GitRevision) - fmt.Printf("Dependencies:\n") - fmt.Printf("- serving: %s\n", ServingVersion) + fmt.Printf("Support:\n") + if m, ok := supportMatrix[ServingVersion]; ok { + fmt.Printf("- Serving: %s\n", strings.Join(m.Versions, " ")) + fmt.Printf("- API(s): %s\n", strings.Join(m.APIs, " ")) + } else { + // ensure the go build works when we update, + // but version command tests fails to prevent shipping + fmt.Printf("- Serving: %s\n", ServingVersion) + } return nil }, } diff --git a/pkg/kn/commands/version_test.go b/pkg/kn/commands/version_test.go index 921b67ea0..ef03d495a 100644 --- a/pkg/kn/commands/version_test.go +++ b/pkg/kn/commands/version_test.go @@ -24,24 +24,24 @@ import ( ) type versionOutput struct { - Version string - BuildDate string - GitRevision string - ServingVersion string + Version string + BuildDate string + GitRevision string + VersionsAPIs *VersionsAPIs } var versionOutputTemplate = `Version: {{.Version}} Build Date: {{.BuildDate}} Git Revision: {{.GitRevision}} -Dependencies: -- serving: {{.ServingVersion}} +Support: +- Serving: {{index .VersionsAPIs.Versions 0}} {{index .VersionsAPIs.Versions 1}} +- API(s): {{index .VersionsAPIs.APIs 0}} ` const ( - fakeVersion = "fake-version" - fakeBuildDate = "fake-build-date" - fakeGitRevision = "fake-git-revision" - fakeServingVersion = "fake-serving-version" + fakeVersion = "fake-version" + fakeBuildDate = "fake-build-date" + fakeGitRevision = "fake-git-revision" ) func TestVersion(t *testing.T) { @@ -55,14 +55,14 @@ func TestVersion(t *testing.T) { Version = fakeVersion BuildDate = fakeBuildDate GitRevision = fakeGitRevision - ServingVersion = fakeServingVersion + ServingVersion = knServingDep expectedVersionOutput = genVersionOuput(t, versionOutputTemplate, versionOutput{ - Version: fakeVersion, - BuildDate: fakeBuildDate, - GitRevision: fakeGitRevision, - ServingVersion: fakeServingVersion}) + fakeVersion, + fakeBuildDate, + fakeGitRevision, + supportMatrix[ServingVersion]}) knParams = &KnParams{} versionCmd = NewVersionCommand(knParams) @@ -78,26 +78,27 @@ func TestVersion(t *testing.T) { assert.Assert(t, versionCmd.RunE != nil) }) - t.Run("prints version, build date, git revision, and serving version string", func(t *testing.T) { + t.Run("prints version, build date, git revision, supported serving version and APIs", func(t *testing.T) { setup() CaptureStdout(t) defer ReleaseStdout(t) err := versionCmd.RunE(nil, []string{}) - assert.Assert(t, err == nil) + assert.NilError(t, err) assert.Equal(t, ReadStdout(t), expectedVersionOutput) }) + } // Private func genVersionOuput(t *testing.T, templ string, vOutput versionOutput) string { tmpl, err := template.New("versionOutput").Parse(versionOutputTemplate) - assert.Assert(t, err == nil) + assert.NilError(t, err) buf := bytes.Buffer{} err = tmpl.Execute(&buf, vOutput) - assert.Assert(t, err == nil) + assert.NilError(t, err) return buf.String() }