Skip to content

Commit

Permalink
Fixes Issue tektoncd#1067
Browse files Browse the repository at this point in the history
    Added --component option to just output the version of the components like client|pipeline|triggers
  • Loading branch information
anshulvermapatel authored and tekton-robot committed Jun 1, 2021
1 parent 068545e commit 4d5c5fd
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 16 deletions.
1 change: 1 addition & 0 deletions docs/cmd/tkn_version.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions docs/man/man1/tkn-version.1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
60 changes: 44 additions & 16 deletions pkg/cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}
}

Expand All @@ -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")
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/cmd/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }()
Expand Down

0 comments on commit 4d5c5fd

Please sign in to comment.