Skip to content

Commit

Permalink
adding version flag (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalghost-dev committed Dec 4, 2024
1 parent 8cb4c85 commit 3bbff16
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!",
Expand All @@ -34,8 +54,8 @@ func runCLI(args []string) int {
fmt.Sprintf("\n\t%-15s %s", "poke-cli <command> <subcommand> [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"),
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3bbff16

Please sign in to comment.