fix(version): Displays supported APIs and version (#453)

* fix(version): Displays supported APIs and version

 Fixes #404

 ```
  ./kn version
Version:      v20191017-local-6328a73-dirty
Build Date:   2019-10-17 09:55:55
Git Revision: 6328a73
Supported APIs:
- serving.knative.dev/v1alpha1 (knative-serving v0.8.0)
 ```

* Removes ServingVersion feeding from ldflags

 - hardcodes the supported APIs as they're supposed to be updated when we bump deps

* Fixes typo in e2e tests

* Uses array for deterministic order of printing supported APIs
This commit is contained in:
Navid Shaikh 2019-10-18 16:05:18 +05:30 committed by Knative Prow Robot
parent f03508f74a
commit df04573590
5 changed files with 30 additions and 48 deletions

View File

@ -16,7 +16,7 @@ function build_flags() {
local base="${1}" local base="${1}"
local now="$(date -u '+%Y-%m-%d %H:%M:%S')" local now="$(date -u '+%Y-%m-%d %H:%M:%S')"
local rev="$(git rev-parse --short HEAD)" local rev="$(git rev-parse --short HEAD)"
local pkg="knative.dev/client/pkg/kn/commands" local pkg="knative.dev/client/pkg/kn/commands/version"
local version="${TAG:-}" local version="${TAG:-}"
# Use vYYYYMMDD-local-<hash> for the version string, if not passed. # Use vYYYYMMDD-local-<hash> for the version string, if not passed.
if [[ -z "${version}" ]]; then if [[ -z "${version}" ]]; then
@ -26,7 +26,5 @@ function build_flags() {
version="v$(date +%Y%m%d)-local-${commit}" version="v$(date +%Y%m%d)-local-${commit}"
fi fi
local serving_version=$(grep 'knative.dev/serving' ${base}/go.mod | sed -e 's/.*serving \(.*\)/\1/') echo "-X '${pkg}.BuildDate=${now}' -X ${pkg}.Version=${version} -X ${pkg}.GitRevision=${rev}"
echo "-X '${pkg}.BuildDate=${now}' -X ${pkg}.Version=${version} -X ${pkg}.GitRevision=${rev} -X ${pkg}.ServingVersion=${serving_version}"
} }

View File

@ -12,11 +12,12 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package commands package version
import ( import (
"fmt" "fmt"
"strings"
"knative.dev/client/pkg/kn/commands"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -24,39 +25,26 @@ import (
var Version string var Version string
var BuildDate string var BuildDate string
var GitRevision string var GitRevision string
var ServingVersion string
// VersionsAPIs hold the list of supported versions and APIs for component kn can work with // update this var as we add more deps
type VersionsAPIs struct { var apiVersions = []string{
Versions, APIs []string "serving.knative.dev/v1alpha1 (knative-serving v0.8.0)",
}
// 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 // NewVersionCommand implements 'kn version' command
func NewVersionCommand(p *KnParams) *cobra.Command { func NewVersionCommand(p *commands.KnParams) *cobra.Command {
versionCmd := &cobra.Command{ versionCmd := &cobra.Command{
Use: "version", Use: "version",
Short: "Prints the client version", Short: "Prints the client version",
RunE: func(cmd *cobra.Command, args []string) error { Run: func(cmd *cobra.Command, args []string) {
out := cmd.OutOrStdout() out := cmd.OutOrStdout()
fmt.Fprintf(out, "Version: %s\n", Version) fmt.Fprintf(out, "Version: %s\n", Version)
fmt.Fprintf(out, "Build Date: %s\n", BuildDate) fmt.Fprintf(out, "Build Date: %s\n", BuildDate)
fmt.Fprintf(out, "Git Revision: %s\n", GitRevision) fmt.Fprintf(out, "Git Revision: %s\n", GitRevision)
fmt.Fprintf(out, "Support:\n") fmt.Fprintf(out, "Supported APIs:\n")
if m, ok := supportMatrix[ServingVersion]; ok { for _, api := range apiVersions {
fmt.Fprintf(out, "- Serving: %s\n", strings.Join(m.Versions, " ")) fmt.Fprintf(out, "- %s\n", api)
fmt.Fprintf(out, "- 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.Fprintf(out, "- Serving: %s\n", ServingVersion)
} }
return nil
}, },
} }
return versionCmd return versionCmd

View File

@ -12,30 +12,30 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package commands package version
import ( import (
"bytes" "bytes"
"testing" "testing"
"text/template" "text/template"
"knative.dev/client/pkg/kn/commands"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"gotest.tools/assert" "gotest.tools/assert"
) )
type versionOutput struct { type versionOutput struct {
Version string Version string
BuildDate string BuildDate string
GitRevision string GitRevision string
VersionsAPIs *VersionsAPIs
} }
var versionOutputTemplate = `Version: {{.Version}} var versionOutputTemplate = `Version: {{.Version}}
Build Date: {{.BuildDate}} Build Date: {{.BuildDate}}
Git Revision: {{.GitRevision}} Git Revision: {{.GitRevision}}
Support: Supported APIs:
- Serving: {{index .VersionsAPIs.Versions 0}} {{index .VersionsAPIs.Versions 1}} - serving.knative.dev/v1alpha1 (knative-serving v0.8.0)
- API(s): {{index .VersionsAPIs.APIs 0}}
` `
const ( const (
@ -47,7 +47,7 @@ const (
func TestVersion(t *testing.T) { func TestVersion(t *testing.T) {
var ( var (
versionCmd *cobra.Command versionCmd *cobra.Command
knParams *KnParams knParams *commands.KnParams
expectedVersionOutput string expectedVersionOutput string
output *bytes.Buffer output *bytes.Buffer
) )
@ -56,16 +56,14 @@ func TestVersion(t *testing.T) {
Version = fakeVersion Version = fakeVersion
BuildDate = fakeBuildDate BuildDate = fakeBuildDate
GitRevision = fakeGitRevision GitRevision = fakeGitRevision
ServingVersion = knServingDep
expectedVersionOutput = genVersionOuput(t, versionOutputTemplate, expectedVersionOutput = genVersionOuput(t, versionOutputTemplate,
versionOutput{ versionOutput{
fakeVersion, fakeVersion,
fakeBuildDate, fakeBuildDate,
fakeGitRevision, fakeGitRevision})
supportMatrix[ServingVersion]})
knParams = &KnParams{} knParams = &commands.KnParams{}
versionCmd = NewVersionCommand(knParams) versionCmd = NewVersionCommand(knParams)
output = new(bytes.Buffer) output = new(bytes.Buffer)
versionCmd.SetOutput(output) versionCmd.SetOutput(output)
@ -76,21 +74,18 @@ func TestVersion(t *testing.T) {
assert.Equal(t, versionCmd.Use, "version") assert.Equal(t, versionCmd.Use, "version")
assert.Equal(t, versionCmd.Short, "Prints the client version") assert.Equal(t, versionCmd.Short, "Prints the client version")
assert.Assert(t, versionCmd.RunE != nil) assert.Assert(t, versionCmd.Run != nil)
}) })
t.Run("prints version, build date, git revision, supported serving version and APIs", func(t *testing.T) { t.Run("prints version, build date, git revision, supported APIs", func(t *testing.T) {
setup() setup()
err := versionCmd.RunE(versionCmd, []string{}) versionCmd.Run(versionCmd, []string{})
assert.NilError(t, err)
assert.Equal(t, output.String(), expectedVersionOutput) assert.Equal(t, output.String(), expectedVersionOutput)
}) })
} }
// Private
func genVersionOuput(t *testing.T, templ string, vOutput versionOutput) string { func genVersionOuput(t *testing.T, templ string, vOutput versionOutput) string {
tmpl, err := template.New("versionOutput").Parse(versionOutputTemplate) tmpl, err := template.New("versionOutput").Parse(versionOutputTemplate)
assert.NilError(t, err) assert.NilError(t, err)

View File

@ -34,6 +34,7 @@ import (
"knative.dev/client/pkg/kn/commands/revision" "knative.dev/client/pkg/kn/commands/revision"
"knative.dev/client/pkg/kn/commands/route" "knative.dev/client/pkg/kn/commands/route"
"knative.dev/client/pkg/kn/commands/service" "knative.dev/client/pkg/kn/commands/service"
"knative.dev/client/pkg/kn/commands/version"
"knative.dev/client/pkg/kn/flags" "knative.dev/client/pkg/kn/flags"
) )
@ -139,7 +140,7 @@ func NewKnCommand(params ...commands.KnParams) *cobra.Command {
rootCmd.AddCommand(plugin.NewPluginCommand(p)) rootCmd.AddCommand(plugin.NewPluginCommand(p))
rootCmd.AddCommand(route.NewRouteCommand(p)) rootCmd.AddCommand(route.NewRouteCommand(p))
rootCmd.AddCommand(commands.NewCompletionCommand(p)) rootCmd.AddCommand(commands.NewCompletionCommand(p))
rootCmd.AddCommand(commands.NewVersionCommand(p)) rootCmd.AddCommand(version.NewVersionCommand(p))
// Deal with empty and unknown sub command groups // Deal with empty and unknown sub command groups
EmptyAndUnknownSubCommands(rootCmd) EmptyAndUnknownSubCommands(rootCmd)

View File

@ -54,7 +54,7 @@ func splitTargets(s, separator string, partsCount int) ([]string, error) {
parts := strings.Split(s, separator) parts := strings.Split(s, separator)
if len(parts) != partsCount { if len(parts) != partsCount {
return nil, fmt.Errorf("expecting %d targets, got %d targets "+ return nil, fmt.Errorf("expecting %d targets, got %d targets "+
"targets: %s seprator: %s", partsCount, len(parts), s, separator) "targets: %s separator: %s", partsCount, len(parts), s, separator)
} }
return parts, nil return parts, nil
} }