github.com/exercism/v2-configlet@v3.9.2+incompatible/cmd/version.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 6 "github.com/exercism/configlet/ui" 7 "github.com/spf13/cobra" 8 ) 9 10 var checkLatestVersion bool 11 12 // versionCmd represents the version command 13 var versionCmd = &cobra.Command{ 14 Use: "version", 15 Short: "Output the current version of the tool", 16 Long: "Output the current version of the tool", 17 Example: fmt.Sprintf(" %s version", binaryName), 18 Run: runVersion, 19 Args: cobra.ExactArgs(0), 20 } 21 22 func runVersion(cmd *cobra.Command, args []string) { 23 // we don't want any UI formatting prepended to this 24 fmt.Printf("configlet version %s\n", configletCLI.Version) 25 26 if !checkLatestVersion { 27 return 28 } 29 30 ok, err := configletCLI.IsUpToDate() 31 if err != nil { 32 ui.PrintError(err) 33 return 34 } 35 msg := "Your CLI version is up to date." 36 37 if !ok { 38 msg = fmt.Sprintf("A new CLI version is available. Run `%s upgrade` to update to %s", binaryName, configletCLI.LatestRelease.Version()) 39 } 40 fmt.Println(msg) 41 } 42 43 func init() { 44 RootCmd.AddCommand(versionCmd) 45 versionCmd.Flags().BoolVarP(&checkLatestVersion, "latest", "l", false, "check latest available version.") 46 }