github.com/verrazzano/verrazzano@v1.7.1/tools/vz/cmd/version/version.go (about) 1 // Copyright (c) 2022, 2024, 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 cmdhelpers "github.com/verrazzano/verrazzano/tools/vz/cmd/helpers" 10 "github.com/verrazzano/verrazzano/tools/vz/pkg/helpers" 11 "github.com/verrazzano/verrazzano/tools/vz/pkg/templates" 12 "os" 13 "regexp" 14 ) 15 16 var cliVersion string 17 var buildDate string 18 var gitCommit string 19 20 const ( 21 CommandName = "version" 22 helpShort = "Verrazzano version information" 23 helpLong = `The command 'version' reports information about the version of the vz tool being run` 24 helpExample = `vz version` 25 ) 26 27 // statusOutputTemplate - template for output of status command 28 const versionOutputTemplate = ` 29 Version: v{{.cli_version}} 30 BuildDate: {{.build_date}} 31 GitCommit: {{.git_commit}} 32 ` 33 34 func NewCmdVersion(vzHelper helpers.VZHelper) *cobra.Command { 35 cmd := cmdhelpers.NewCommand(vzHelper, CommandName, helpShort, helpLong) 36 cmd.Example = helpExample 37 cmd.RunE = func(cmd *cobra.Command, args []string) error { 38 return runCmdVersion(vzHelper) 39 } 40 41 // Verifies that the CLI args are not set at the creation of a command 42 vzHelper.VerifyCLIArgsNil(cmd) 43 44 return cmd 45 } 46 47 func runCmdVersion(vzHelper helpers.VZHelper) error { 48 templateValues := map[string]string{ 49 "cli_version": cliVersion, 50 "build_date": buildDate, 51 "git_commit": gitCommit, 52 } 53 54 result, err := templates.ApplyTemplate(versionOutputTemplate, templateValues) 55 if err != nil { 56 return fmt.Errorf("Failed to generate %s command output: %s", CommandName, err.Error()) 57 } 58 _, _ = fmt.Fprintf(vzHelper.GetOutputStream(), result) 59 60 return nil 61 } 62 63 func GetEffectiveDocsVersion() string { 64 if os.Getenv("USE_V8O_DOC_STAGE") == "true" || len(cliVersion) == 0 { 65 return "devel" 66 } 67 var re = regexp.MustCompile(`(?m)(\d.\d)(.*)`) 68 s := re.FindAllStringSubmatch(cliVersion, -1)[0][1] //This will get the group 1 of 1st match which is "1.4.0" to "1.4" 69 return fmt.Sprintf("v%s", s) //return v1.4 by appending prefex 'v' 70 } 71 72 func GetCLIVersion() string { 73 return cliVersion 74 } 75 76 func GetVZCLIVersionMessageString() string { 77 return "\nThis command was run with vz CLI Version " + GetCLIVersion() + "\n" 78 }