github.com/verrazzano/verrazzano@v1.7.0/tools/psr/psrctl/cmd/version/version.go (about)

     1  // Copyright (c) 2022, 2023, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package version
     5  
     6  import (
     7  	"fmt"
     8  	"github.com/spf13/cobra"
     9  	"github.com/verrazzano/verrazzano/tools/psr/psrctl/cmd/constants"
    10  	cmdhelpers "github.com/verrazzano/verrazzano/tools/vz/cmd/helpers"
    11  	"github.com/verrazzano/verrazzano/tools/vz/pkg/helpers"
    12  	"github.com/verrazzano/verrazzano/tools/vz/pkg/templates"
    13  	"os"
    14  	"regexp"
    15  )
    16  
    17  var cliVersion string
    18  var buildDate string
    19  var gitCommit string
    20  
    21  const (
    22  	CommandName = "version"
    23  	helpShort   = "PSR CLI version information"
    24  	helpLong    = `The command 'version' reports information about the version of the psrctl tool being run`
    25  	helpExample = `psrctl version`
    26  )
    27  
    28  // statusOutputTemplate - template for output of status command
    29  const versionOutputTemplate = `
    30  Version:     v{{.cli_version}}
    31  BuildDate:   {{.build_date}}
    32  GitCommit:   {{.git_commit}}
    33  WorkerImage: {{.worker_image}}
    34  `
    35  
    36  func NewCmdVersion(vzHelper helpers.VZHelper) *cobra.Command {
    37  	cmd := cmdhelpers.NewCommand(vzHelper, CommandName, helpShort, helpLong)
    38  	cmd.Example = helpExample
    39  	cmd.RunE = func(cmd *cobra.Command, args []string) error {
    40  		return runCmdVersion(vzHelper)
    41  	}
    42  
    43  	return cmd
    44  }
    45  
    46  func runCmdVersion(vzHelper helpers.VZHelper) error {
    47  
    48  	templateValues := map[string]string{
    49  		"cli_version":  cliVersion,
    50  		"build_date":   buildDate,
    51  		"git_commit":   gitCommit,
    52  		"worker_image": constants.GetDefaultWorkerImage(),
    53  	}
    54  
    55  	result, err := templates.ApplyTemplate(versionOutputTemplate, templateValues)
    56  	if err != nil {
    57  		return fmt.Errorf("Failed to generate %s command output: %s", CommandName, err.Error())
    58  	}
    59  	_, _ = fmt.Fprintf(vzHelper.GetOutputStream(), result)
    60  
    61  	return nil
    62  }
    63  
    64  func GetEffectiveDocsVersion() string {
    65  	if os.Getenv("USE_V8O_DOC_STAGE") == "true" || len(cliVersion) == 0 {
    66  		return "devel"
    67  	}
    68  	var re = regexp.MustCompile(`(?m)(\d.\d)(.*)`)
    69  	s := re.FindAllStringSubmatch(cliVersion, -1)[0][1] //This will get the group 1 of 1st match which is "1.4.0" to "1.4"
    70  	return fmt.Sprintf("v%s", s)                        //return v1.4 by appending prefix 'v'
    71  }
    72  
    73  func GetCLIVersion() string {
    74  	return cliVersion
    75  }