From 3bbff1629d446d21932323f6dac17b36adf35f1b Mon Sep 17 00:00:00 2001 From: Christian Date: Wed, 4 Dec 2024 13:24:30 -0800 Subject: [PATCH] adding version flag (#78) --- cli.go | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/cli.go b/cli.go index 488447b..3cfe5b9 100644 --- a/cli.go +++ b/cli.go @@ -7,24 +7,44 @@ import ( "github.com/digitalghost-dev/poke-cli/cmd" "github.com/digitalghost-dev/poke-cli/flags" "os" + "runtime/debug" ) var ( styleBold = lipgloss.NewStyle().Bold(true) helpBorder = lipgloss.NewStyle(). - BorderStyle(lipgloss.RoundedBorder()). - BorderForeground(lipgloss.Color("#FFCC00")) + BorderStyle(lipgloss.RoundedBorder()). + BorderForeground(lipgloss.Color("#FFCC00")) errorColor = lipgloss.NewStyle().Foreground(lipgloss.Color("#F2055C")) errorBorder = lipgloss.NewStyle(). - BorderStyle(lipgloss.RoundedBorder()). - BorderForeground(lipgloss.Color("#F2055C")) + BorderStyle(lipgloss.RoundedBorder()). + BorderForeground(lipgloss.Color("#F2055C")) ) +var version = "(devel)" + +func currentVersion() { + buildInfo, ok := debug.ReadBuildInfo() + if !ok { + fmt.Println("Unable to determine version information.") + return + } + + if buildInfo.Main.Version != "" { + fmt.Printf("Version: %s\n", buildInfo.Main.Version) + } else { + fmt.Println("Version: unknown") + } +} + func runCLI(args []string) int { mainFlagSet := flag.NewFlagSet("poke-cli", flag.ContinueOnError) latestFlag := mainFlagSet.Bool("latest", false, "Prints the program's latest Docker Image and Release versions.") shortLatestFlag := mainFlagSet.Bool("l", false, "Prints the program's latest Docker Image and Release versions.") + currentVersionFlag := mainFlagSet.Bool("version", false, "Prints the current version") + shortCurrentVersionFlag := mainFlagSet.Bool("v", false, "Prints the current version") + mainFlagSet.Usage = func() { helpMessage := helpBorder.Render( "Welcome! This tool displays data related to Pokémon!", @@ -34,8 +54,8 @@ func runCLI(args []string) int { fmt.Sprintf("\n\t%-15s %s", "poke-cli [flag]", ""), "\n\n", styleBold.Render("FLAGS:"), fmt.Sprintf("\n\t%-15s %s", "-h, --help", "Shows the help menu"), - fmt.Sprintf("\n\t%-15s %s", "-l, --latest", "Prints the latest available"), - fmt.Sprintf("\n\t%-15s %s", "", "version of the program"), + fmt.Sprintf("\n\t%-15s %s", "-l, --latest", "Prints the latest version available"), + fmt.Sprintf("\n\t%-15s %s", "-v, --version", "Prints the current version"), "\n\n", styleBold.Render("AVAILABLE COMMANDS:"), fmt.Sprintf("\n\t%-15s %s", "pokemon", "Get details of a specific Pokémon"), fmt.Sprintf("\n\t%-15s %s", "types", "Get details of a specific typing"), @@ -70,6 +90,9 @@ func runCLI(args []string) int { } else if *latestFlag || *shortLatestFlag { flags.LatestFlag() return 0 + } else if *currentVersionFlag || *shortCurrentVersionFlag { + currentVersion() + return 0 } else if cmdFunc, exists := commands[os.Args[1]]; exists { cmdFunc() return 0