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 now="$(date -u '+%Y-%m-%d %H:%M:%S')"
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:-}"
# Use vYYYYMMDD-local-<hash> for the version string, if not passed.
if [[ -z "${version}" ]]; then
@ -26,7 +26,5 @@ function build_flags() {
version="v$(date +%Y%m%d)-local-${commit}"
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} -X ${pkg}.ServingVersion=${serving_version}"
echo "-X '${pkg}.BuildDate=${now}' -X ${pkg}.Version=${version} -X ${pkg}.GitRevision=${rev}"
}

View File

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

View File

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

View File

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

View File

@ -54,7 +54,7 @@ func splitTargets(s, separator string, partsCount int) ([]string, error) {
parts := strings.Split(s, separator)
if len(parts) != partsCount {
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
}