github.com/verrazzano/verrazzano@v1.7.0/tools/vz/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 9 "github.com/spf13/cobra" 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 = "Verrazzano version information" 24 helpLong = `The command 'version' reports information about the version of the vz tool being run` 25 helpExample = `vz 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 ` 34 35 func NewCmdVersion(vzHelper helpers.VZHelper) *cobra.Command { 36 cmd := cmdhelpers.NewCommand(vzHelper, CommandName, helpShort, helpLong) 37 cmd.Example = helpExample 38 cmd.RunE = func(cmd *cobra.Command, args []string) error { 39 return runCmdVersion(vzHelper) 40 } 41 42 // Verifies that the CLI args are not set at the creation of a command 43 vzHelper.VerifyCLIArgsNil(cmd) 44 45 return cmd 46 } 47 48 func runCmdVersion(vzHelper helpers.VZHelper) error { 49 50 templateValues := map[string]string{ 51 "cli_version": cliVersion, 52 "build_date": buildDate, 53 "git_commit": gitCommit, 54 } 55 56 result, err := templates.ApplyTemplate(versionOutputTemplate, templateValues) 57 if err != nil { 58 return fmt.Errorf("Failed to generate %s command output: %s", CommandName, err.Error()) 59 } 60 _, _ = fmt.Fprintf(vzHelper.GetOutputStream(), result) 61 62 return nil 63 } 64 65 func GetEffectiveDocsVersion() string { 66 if os.Getenv("USE_V8O_DOC_STAGE") == "true" || len(cliVersion) == 0 { 67 return "devel" 68 } 69 var re = regexp.MustCompile(`(?m)(\d.\d)(.*)`) 70 s := re.FindAllStringSubmatch(cliVersion, -1)[0][1] //This will get the group 1 of 1st match which is "1.4.0" to "1.4" 71 return fmt.Sprintf("v%s", s) //return v1.4 by appending prefex 'v' 72 } 73 74 func GetCLIVersion() string { 75 return cliVersion 76 }