github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/ctl/ctr.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package ctl 21 22 import ( 23 "fmt" 24 "os" 25 "os/exec" 26 "path/filepath" 27 "strings" 28 29 "github.com/spf13/cobra" 30 31 viper "github.com/1aal/kubeblocks/pkg/viperx" 32 ) 33 34 const cliVersionTemplateString = "CLI version: %s \nRuntime version: %s\n" 35 36 var RootCmd = &cobra.Command{ 37 Use: "lorryctl", 38 Short: "LORRY CLI", 39 Long: ` 40 / / ____ ____________ __ / ____/ /______/ / 41 / / / __ \/ ___/ ___/ / / / / / / __/ ___/ / 42 / /___/ /_/ / / / / / /_/ / / /___/ /_/ / / / 43 /_____/\____/_/ /_/ \__, / \____/\__/_/ /_/ 44 /____/ 45 =============================== 46 Lorry service client`, 47 Run: func(cmd *cobra.Command, _ []string) { 48 if versionFlag { 49 printVersion() 50 } else { 51 _ = cmd.Help() 52 } 53 }, 54 } 55 56 type lorryVersion struct { 57 CliVersion string `json:"Cli version"` 58 RuntimeVersion string `json:"Runtime version"` 59 } 60 61 var ( 62 cliVersion string 63 versionFlag bool 64 lorryVer lorryVersion 65 lorryRuntimePath string 66 ) 67 68 // Execute adds all child commands to the root command. 69 func Execute(cliVersion, apiVersion string) { 70 lorryVer = lorryVersion{ 71 CliVersion: cliVersion, 72 RuntimeVersion: apiVersion, 73 } 74 75 cobra.OnInitialize(initConfig) 76 77 setVersion() 78 79 if err := RootCmd.Execute(); err != nil { 80 fmt.Println(err) 81 os.Exit(-1) 82 } 83 } 84 85 func setVersion() { 86 template := fmt.Sprintf(cliVersionTemplateString, lorryVer.CliVersion, lorryVer.RuntimeVersion) 87 RootCmd.SetVersionTemplate(template) 88 } 89 90 func printVersion() { 91 fmt.Printf(cliVersionTemplateString, lorryVer.CliVersion, lorryVer.RuntimeVersion) 92 } 93 94 func initConfig() { 95 // err intentionally ignored since lorry may not yet be installed. 96 runtimeVer := GetRuntimeVersion() 97 98 lorryVer = lorryVersion{ 99 // Set in Execute() method in this file before initConfig() is called by cmd.Execute(). 100 CliVersion: cliVersion, 101 RuntimeVersion: strings.ReplaceAll(runtimeVer, "\n", ""), 102 } 103 104 viper.SetEnvPrefix("lorry") 105 viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) 106 viper.AutomaticEnv() 107 } 108 109 func init() { 110 RootCmd.PersistentFlags().StringVarP(&lorryRuntimePath, "kb-runtime-dir", "", "/kubeblocks/", "The directory of kubeblocks binaries") 111 } 112 113 // GetRuntimeVersion returns the version for the local lorry runtime. 114 func GetRuntimeVersion() string { 115 lorryCMD := filepath.Join(lorryRuntimePath, "lorry") 116 117 out, err := exec.Command(lorryCMD, "--version").Output() 118 if err != nil { 119 return "n/a\n" 120 } 121 return string(out) 122 }