From 4d5c5fdc58f8dfa23e17d5178ff275e6156004dc Mon Sep 17 00:00:00 2001 From: Anshul Verma Date: Fri, 31 Jul 2020 17:04:34 +0530 Subject: [PATCH] Fixes Issue #1067 Added --component option to just output the version of the components like client|pipeline|triggers --- docs/cmd/tkn_version.md | 1 + docs/man/man1/tkn-version.1 | 4 +++ pkg/cmd/version/version.go | 60 ++++++++++++++++++++++++--------- pkg/cmd/version/version_test.go | 17 ++++++++++ 4 files changed, 66 insertions(+), 16 deletions(-) diff --git a/docs/cmd/tkn_version.md b/docs/cmd/tkn_version.md index 9582f3679..3e5ac6111 100644 --- a/docs/cmd/tkn_version.md +++ b/docs/cmd/tkn_version.md @@ -16,6 +16,7 @@ Prints version information ``` --check check if a newer version is available + --component string provide a particular component name for its version (client|pipeline|triggers|dashboard) -c, --context string name of the kubeconfig context to use (default: kubectl config current-context) -h, --help help for version -k, --kubeconfig string kubectl config file (default: $HOME/.kube/config) diff --git a/docs/man/man1/tkn-version.1 b/docs/man/man1/tkn-version.1 index 48677d067..71c4e8c17 100644 --- a/docs/man/man1/tkn-version.1 +++ b/docs/man/man1/tkn-version.1 @@ -23,6 +23,10 @@ Prints version information \fB\-\-check\fP[=false] check if a newer version is available +.PP +\fB\-\-component\fP="" + provide a particular component name for its version (client|pipeline|triggers|dashboard) + .PP \fB\-c\fP, \fB\-\-context\fP="" name of the kubeconfig context to use (default: kubectl config current\-context) diff --git a/pkg/cmd/version/version.go b/pkg/cmd/version/version.go index be46aa74b..5461b7e26 100644 --- a/pkg/cmd/version/version.go +++ b/pkg/cmd/version/version.go @@ -43,6 +43,8 @@ var ( skipCheckFlag = "false" // NOTE: use go build -ldflags "-X github.com/tektoncd/cli/pkg/cmd/version.namespace=tekton-pipelines" namespace string + + component = "" ) // Command returns version command @@ -68,24 +70,48 @@ func Command(p cli.Params) *cobra.Command { return nil }, RunE: func(cmd *cobra.Command, args []string) error { - fmt.Fprintf(cmd.OutOrStdout(), "Client version: %s\n", clientVersion) - cs, err := p.Clients() if err == nil { - pipelineVersion, _ := version.GetPipelineVersion(cs, namespace) - if pipelineVersion == "" { - pipelineVersion = "unknown, " + - "pipeline controller may be installed in another namespace please use tkn version -n {namespace}" - } - - fmt.Fprintf(cmd.OutOrStdout(), "Pipeline version: %s\n", pipelineVersion) - triggersVersion, _ := version.GetTriggerVersion(cs, namespace) - if triggersVersion != "" { - fmt.Fprintf(cmd.OutOrStdout(), "Triggers version: %s\n", triggersVersion) - } - dashboardVersion, _ := version.GetDashboardVersion(cs, namespace) - if dashboardVersion != "" { - fmt.Fprintf(cmd.OutOrStdout(), "Dashboard version: %s\n", dashboardVersion) + switch component { + case "": + fmt.Fprintf(cmd.OutOrStdout(), "Client version: %s\n", clientVersion) + pipelineVersion, _ := version.GetPipelineVersion(cs, namespace) + if pipelineVersion == "" { + pipelineVersion = "unknown, " + + "pipeline controller may be installed in another namespace please use tkn version -n {namespace}" + } + + fmt.Fprintf(cmd.OutOrStdout(), "Pipeline version: %s\n", pipelineVersion) + triggersVersion, _ := version.GetTriggerVersion(cs, namespace) + if triggersVersion != "" { + fmt.Fprintf(cmd.OutOrStdout(), "Triggers version: %s\n", triggersVersion) + } + dashboardVersion, _ := version.GetDashboardVersion(cs, namespace) + if dashboardVersion != "" { + fmt.Fprintf(cmd.OutOrStdout(), "Dashboard version: %s\n", dashboardVersion) + } + case "client": + fmt.Fprintf(cmd.OutOrStdout(), "%s\n", clientVersion) + case "pipeline": + pipelineVersion, _ := version.GetPipelineVersion(cs, namespace) + if pipelineVersion == "" { + pipelineVersion = "unknown" + } + fmt.Fprintf(cmd.OutOrStdout(), "%s\n", pipelineVersion) + case "triggers": + triggersVersion, _ := version.GetTriggerVersion(cs, namespace) + if triggersVersion == "" { + triggersVersion = "unknown" + } + fmt.Fprintf(cmd.OutOrStdout(), "%s\n", triggersVersion) + case "dashboard": + dashboardVersion, _ := version.GetDashboardVersion(cs, namespace) + if dashboardVersion == "" { + dashboardVersion = "unknown" + } + fmt.Fprintf(cmd.OutOrStdout(), "%s\n", dashboardVersion) + default: + fmt.Fprintf(cmd.OutOrStdout(), "Invalid component value\n") } } @@ -104,6 +130,8 @@ func Command(p cli.Params) *cobra.Command { "namespace to check installed controller version") flags.AddTektonOptions(cmd) + cmd.Flags().StringVarP(&component, "component", "", "", "provide a particular component name for its version (client|pipeline|triggers|dashboard)") + if skipCheckFlag != "true" { cmd.Flags().BoolVar(&check, "check", false, "check if a newer version is available") } diff --git a/pkg/cmd/version/version_test.go b/pkg/cmd/version/version_test.go index 2826e36ed..e8a8050e6 100644 --- a/pkg/cmd/version/version_test.go +++ b/pkg/cmd/version/version_test.go @@ -91,6 +91,23 @@ func TestVersionGood(t *testing.T) { golden.Assert(t, got, fmt.Sprintf("%s.golden", t.Name())) } +func TestComponentVersion(t *testing.T) { + v := clientVersion + defer func() { clientVersion = v }() + + t.Run("test_client", func(t *testing.T) {}) + clientVersion = "v1.2.3" + + seedData, _ := test.SeedTestData(t, pipelinetest.Data{}) + + cs := pipelinetest.Clients{Kube: seedData.Kube} + p := &test.Params{Kube: cs.Kube} + version := Command(p) + got, _ := test.ExecuteCommand(version, "version", "--component", "client") + expected := "v1.2.3\n" + test.AssertOutput(t, expected, got) +} + func TestVersionBad(t *testing.T) { v := clientVersion defer func() { clientVersion = v }()