From 8c06f69b53d95f3512007a41e97ac531c61cf79b Mon Sep 17 00:00:00 2001 From: Chmouel Boudjnah Date: Tue, 25 Jun 2024 12:14:17 +0200 Subject: [PATCH] add flag to show version information from servers instead of showing the client version show the live information on cluster if we pass the --live flag to opc version Signed-off-by: Chmouel Boudjnah --- pkg/version.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++-- pkg/version.json | 8 +++++++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/pkg/version.go b/pkg/version.go index 111a3f92d..3643a9068 100644 --- a/pkg/version.go +++ b/pkg/version.go @@ -8,11 +8,14 @@ import ( _ "embed" paccli "github.com/openshift-pipelines/pipelines-as-code/pkg/cli" + tkncli "github.com/tektoncd/cli/pkg/cli" + tknversion "github.com/tektoncd/cli/pkg/version" - // paccli "github.com/openshift-pipelines/opc/pkg" "github.com/spf13/cobra" ) +var serverFlag = "server" + //go:embed version.json var versionFile string @@ -27,13 +30,55 @@ type versions struct { ManualApprovalGate string `json:"manualapprovalgate"` } +func getLiveInformations(iostreams *paccli.IOStreams) error { + tp := &tkncli.TektonParams{} + cs, err := tp.Clients() + if err != nil { + return err + } + namespace := "openshift-pipelines" + operatorNamespace := "openshift-operators" + + chainsVersion, _ := tknversion.GetChainsVersion(cs, namespace) + if chainsVersion != "" { + fmt.Fprintf(iostreams.Out, "Chains version: %s\n", chainsVersion) + } + pipelineVersion, _ := tknversion.GetPipelineVersion(cs, namespace) + if pipelineVersion == "" { + pipelineVersion = "unknown, " + + "pipeline controller may be installed in another namespace." + } + fmt.Fprintf(iostreams.Out, "Pipeline version: %s\n", pipelineVersion) + triggersVersion, _ := tknversion.GetTriggerVersion(cs, namespace) + if triggersVersion != "" { + fmt.Fprintf(iostreams.Out, "Triggers version: %s\n", triggersVersion) + } + operatorVersion, _ := tknversion.GetOperatorVersion(cs, operatorNamespace) + if operatorVersion != "" { + fmt.Fprintf(iostreams.Out, "Operator version: %s\n", operatorVersion) + } + hubVersion, _ := tknversion.GetHubVersion(cs, namespace) + if hubVersion != "" { + fmt.Fprintf(iostreams.Out, "Hub version: %s\n", hubVersion) + } + return nil +} + func VersionCommand(ioStreams *paccli.IOStreams) *cobra.Command { cmd := &cobra.Command{ Use: "version", Short: "Print opc version", Long: "Print OpenShift Pipeline Client version", - RunE: func(_ *cobra.Command, args []string) error { + RunE: func(cmd *cobra.Command, args []string) error { var v versions + server, err := cmd.Flags().GetBool(serverFlag) + if err != nil { + return err + } + if server { + // TODO(chmouel): pac version when it's refactored in pac code + return getLiveInformations(ioStreams) + } if err := json.Unmarshal([]byte(versionFile), &v); err != nil { return fmt.Errorf("cannot unmarshall versions: %w", err) } @@ -65,5 +110,7 @@ func VersionCommand(ioStreams *paccli.IOStreams) *cobra.Command { "commandType": "main", }, } + + cmd.Flags().BoolP(serverFlag, "s", false, "Get the services version information from cluster instead of the client version.") return cmd } diff --git a/pkg/version.json b/pkg/version.json index d99d7e163..487c1d958 100644 --- a/pkg/version.json +++ b/pkg/version.json @@ -1 +1,7 @@ -{"pac": "0.27.2", "tkn": "0.38.0", "results": "0.11.0", "manualapprovalgate": "0.3.0", "opc": "devel"} +{ + "pac": "0.27.2", + "tkn": "0.38.0", + "results": "0.11.0", + "manualapprovalgate": "0.3.0", + "opc": "devel" +}