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

kn version command to list supported serving versions and APIs #370

Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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]

|===

Expand Down
24 changes: 22 additions & 2 deletions pkg/kn/commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package commands

import (
"fmt"
"strings"

"github.com/spf13/cobra"
)
Expand All @@ -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 {
navidshaikh marked this conversation as resolved.
Show resolved Hide resolved
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",
Expand All @@ -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
},
}
Expand Down
39 changes: 20 additions & 19 deletions pkg/kn/commands/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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()
}