From 554eb9736f382e657829885d7f72415192655b24 Mon Sep 17 00:00:00 2001 From: Christian Date: Sun, 1 Sep 2024 17:05:46 -0700 Subject: [PATCH 1/5] updating tests --- cli_test.go | 29 ++++++++++++++++++++--------- cmd/pokemon_test.go | 11 +++-------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/cli_test.go b/cli_test.go index 31fca32..db0135b 100644 --- a/cli_test.go +++ b/cli_test.go @@ -13,9 +13,14 @@ func TestCLI(t *testing.T) { expectedExit int }{ { - args: []string{"pokemon"}, - expectedOutput: "Please declare a Pokémon's name after [pokemon] command\nRun 'poke-cli --help' for more details\nerror: insufficient arguments\n", - expectedExit: 1, + args: []string{"pokemon"}, + expectedOutput: "╭────────────────────────────────────────────────────────────╮\n" + + "│Error! │\n" + + "│Please declare a Pokémon's name after the [pokemon] command │\n" + + "│Run 'poke-cli pokemon -h' for more details │\n" + + "│error: insufficient arguments │\n" + + "╰────────────────────────────────────────────────────────────╯\n", + expectedExit: 1, }, { args: []string{"pokemon", "bulbasaur"}, @@ -33,14 +38,20 @@ func TestCLI(t *testing.T) { expectedExit: 0, }, { - args: []string{"pokemon", "chimchar", "types"}, - expectedOutput: "Error: Invalid argument 'types'. Only flags are allowed after declaring a Pokémon's name\n", - expectedExit: 1, + args: []string{"pokemon", "chimchar", "types"}, + expectedOutput: "╭─────────────────────────────────────────────────────────────────────────────────╮\n" + + "│Error! │\n" + + "│Invalid argument 'types'. Only flags are allowed after declaring a Pokémon's name│\n" + + "╰─────────────────────────────────────────────────────────────────────────────────╯\n", + expectedExit: 1, }, { - args: []string{"pokemon", "flutter-mane", "types"}, - expectedOutput: "Error: Invalid argument 'types'. Only flags are allowed after declaring a Pokémon's name\n", - expectedExit: 1, + args: []string{"pokemon", "flutter-mane", "types"}, + expectedOutput: "╭─────────────────────────────────────────────────────────────────────────────────╮\n" + + "│Error! │\n" + + "│Invalid argument 'types'. Only flags are allowed after declaring a Pokémon's name│\n" + + "╰─────────────────────────────────────────────────────────────────────────────────╯\n", + expectedExit: 1, }, { args: []string{"pokemon", "AmPhaROs", "--types", "--abilities"}, diff --git a/cmd/pokemon_test.go b/cmd/pokemon_test.go index 77043d8..cfbdd80 100644 --- a/cmd/pokemon_test.go +++ b/cmd/pokemon_test.go @@ -1,15 +1,10 @@ package cmd import ( - "github.com/charmbracelet/lipgloss" "github.com/stretchr/testify/assert" "testing" ) -const red = lipgloss.Color("#F2055C") - -var errorColor = lipgloss.NewStyle().Foreground(red) - func TestValidateArgs_ValidInput(t *testing.T) { validInputs := [][]string{ {"poke-cli", "pokemon", "pikachu"}, @@ -20,7 +15,7 @@ func TestValidateArgs_ValidInput(t *testing.T) { } for _, input := range validInputs { - err := ValidateArgs(input, errorColor) + err := ValidateArgs(input) assert.NoError(t, err, "Expected no error for valid input") } } @@ -36,7 +31,7 @@ func TestValidateArgs_InvalidFlag(t *testing.T) { } for i, input := range invalidInputs { - err := ValidateArgs(input, errorColor) + err := ValidateArgs(input) assert.Error(t, err, "Expected error for invalid flag") assert.NotEmpty(t, expectedErrors[i], err.Error()) } @@ -49,7 +44,7 @@ func TestValidateArgs_TooManyArgs(t *testing.T) { expectedError := "error: too many arguments\n" for _, input := range invalidInput { - err := ValidateArgs(input, errorColor) + err := ValidateArgs(input) assert.Error(t, err, "Expected error for too many arguments") assert.NotEqual(t, expectedError, err.Error()) } From ec5c56b42d1ac6a574d3c09a647197489816314f Mon Sep 17 00:00:00 2001 From: Christian Date: Wed, 4 Sep 2024 21:57:51 -0700 Subject: [PATCH 2/5] updating tests --- cli_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/cli_test.go b/cli_test.go index db0135b..8015948 100644 --- a/cli_test.go +++ b/cli_test.go @@ -63,6 +63,17 @@ func TestCLI(t *testing.T) { expectedOutput: "Your selected Pokémon: Cloyster\nNational Pokédex #: 91\n──────\nTyping\nType 1: water\nType 2: ice\n─────────\nAbilities\nAbility 1: shell-armor\nAbility 2: skill-link\nHidden Ability: overcoat\n", expectedExit: 0, }, + { + args: []string{"pokemon", "gyarados", "--help"}, + expectedOutput: "╭──────────────────────────────────────────────────────────────╮\n" + + "│poke-cli pokemon [flags] │\n" + + "│ │\n" + + "│FLAGS: │\n" + + "│ -a, --abilities Prints out the Pokémon's abilities. │\n" + + "│ -t, --types Prints out the Pokémon's typing. │\n" + + "╰──────────────────────────────────────────────────────────────╯\n", + expectedExit: 0, + }, } for _, test := range tests { From f0543302032a3a1df296e47c91d4e11242c45ddd Mon Sep 17 00:00:00 2001 From: Christian Date: Sun, 8 Sep 2024 16:51:12 -0600 Subject: [PATCH 3/5] updating version number --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1a75a06..762fc2b 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ git clone https://github.com/digitalghost-dev/poke-cli.git ### Docker Use a Docker Image instead: ```bash -docker run --rm -it digitalghostdev/poke-cli:v0.4.1 [command] [subcommand] [flag] +docker run --rm -it digitalghostdev/poke-cli:v0.4.2 [command] [subcommand] [flag] ``` ## Usage From 60ace86f23e77d3b3ea7881951610238eef46485 Mon Sep 17 00:00:00 2001 From: Christian Date: Mon, 9 Sep 2024 19:59:40 -0700 Subject: [PATCH 4/5] adding customized help menu --- flags/pokemonflagset.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/flags/pokemonflagset.go b/flags/pokemonflagset.go index 3569aea..28431ec 100644 --- a/flags/pokemonflagset.go +++ b/flags/pokemonflagset.go @@ -9,6 +9,13 @@ import ( "github.com/digitalghost-dev/poke-cli/connections" ) +var ( + helpBorder = lipgloss.NewStyle(). + BorderStyle(lipgloss.RoundedBorder()). + BorderForeground(lipgloss.Color("#FFCC00")) + styleBold = lipgloss.NewStyle().Bold(true) +) + func SetupPokemonFlagSet() (*flag.FlagSet, *bool, *bool, *bool, *bool) { pokeFlags := flag.NewFlagSet("pokeFlags", flag.ExitOnError) @@ -18,6 +25,14 @@ func SetupPokemonFlagSet() (*flag.FlagSet, *bool, *bool, *bool, *bool) { abilitiesFlag := pokeFlags.Bool("abilities", false, "Print the declared Pokémon's abilities") shortAbilitiesFlag := pokeFlags.Bool("a", false, "Print the declared Pokémon's abilities") + pokeFlags.Usage = func() { + fmt.Println( + helpBorder.Render("poke-cli pokemon [flags]", + styleBold.Render("\n\nFLAGS:"), "\n\t", "-a, --abilities", "\t", "Prints out the Pokémon's abilities.", + "\n\t", "-t, --types", "\t\t", "Prints out the Pokémon's typing."), + ) + } + return pokeFlags, typesFlag, shortTypesFlag, abilitiesFlag, shortAbilitiesFlag } From 746a5c31c81afef93a2acdd382fef3718383bf5b Mon Sep 17 00:00:00 2001 From: Christian Date: Mon, 9 Sep 2024 19:59:58 -0700 Subject: [PATCH 5/5] adding borders to output --- cmd/pokemon.go | 73 +++++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/cmd/pokemon.go b/cmd/pokemon.go index 3e9de14..3741af9 100644 --- a/cmd/pokemon.go +++ b/cmd/pokemon.go @@ -12,54 +12,63 @@ import ( "strings" ) +var ( + errorColor = lipgloss.NewStyle().Foreground(lipgloss.Color("#F2055C")) + errorBorder = lipgloss.NewStyle(). + BorderStyle(lipgloss.RoundedBorder()). + BorderForeground(lipgloss.Color("#F2055C")) + helpBorder = lipgloss.NewStyle(). + BorderStyle(lipgloss.RoundedBorder()). + BorderForeground(lipgloss.Color("#FFCC00")) + styleBold = lipgloss.NewStyle().Bold(true) + styleItalic = lipgloss.NewStyle().Italic(true) +) + // ValidateArgs validates the command line arguments -func ValidateArgs(args []string, errorColor lipgloss.Style) error { +func ValidateArgs(args []string) error { if len(args) > 5 { - return fmt.Errorf("error: too many arguments") + return fmt.Errorf(errorBorder.Render(errorColor.Render("Error!"), "\nToo many arguments")) } if len(args) < 3 { - fmt.Println(errorColor.Render("Please declare a Pokémon's name after [pokemon] command")) - fmt.Println(errorColor.Render("Run 'poke-cli --help' for more details")) - return fmt.Errorf("error: insufficient arguments") + return fmt.Errorf(errorBorder.Render(errorColor.Render("Error!"), "\nPlease declare a Pokémon's name after the [pokemon] command", "\nRun 'poke-cli pokemon -h' for more details", "\nerror: insufficient arguments")) } if len(args) > 3 { for _, arg := range args[3:] { if arg[0] != '-' { - errorMsg := fmt.Sprintf("Error: Invalid argument '%s'. Only flags are allowed after declaring a Pokémon's name", arg) - return fmt.Errorf(errorColor.Render(strings.TrimSpace(errorMsg))) + errorTitle := errorColor.Render("Error!") + errorString := fmt.Sprintf("\nInvalid argument '%s'. Only flags are allowed after declaring a Pokémon's name", arg) + formattedString := errorTitle + errorString + return fmt.Errorf(errorBorder.Render(formattedString)) } } } + if args[2] == "-h" || args[2] == "--help" { + flag.Usage() + os.Exit(0) + } + return nil } // PokemonCommand processes the Pokémon command func PokemonCommand() { - const red = lipgloss.Color("#F2055C") - var errorColor = lipgloss.NewStyle().Foreground(red) - - var styleBold = lipgloss.NewStyle().Bold(true) - var styleItalic = lipgloss.NewStyle().Italic(true) flag.Usage = func() { - // Usage section - fmt.Println(styleBold.Render("\nUSAGE:")) - fmt.Println("\t", "poke-cli", styleBold.Render("pokemon"), "[flag]") - fmt.Println("\t", "Get details about a specific Pokémon") - fmt.Println("\t", "----------") - fmt.Println("\t", styleItalic.Render("Examples:"), "\t", "poke-cli pokemon bulbasaur") - fmt.Println("\t\t\t", "poke-cli pokemon flutter-mane --types") - fmt.Println("\t\t\t", "poke-cli pokemon excadrill -t -a") - - // Flags section - fmt.Println(styleBold.Render("\nFLAGS:")) - fmt.Println("\t", "-a, --abilities", "\t", "Prints out the Pokémon's abilities.") - fmt.Println("\t", "-t, --types", "\t\t", "Prints out the Pokémon's typing.") - fmt.Print("\n") + fmt.Println( + helpBorder.Render(styleBold.Render("USAGE:"), "\n\t", "poke-cli", styleBold.Render("pokemon"), "", "[flag]", + "\n\t", "Get details about a specific Pokémon", + "\n\t", "----------", + "\n\t", styleItalic.Render("Examples:"), "\t", "poke-cli pokemon bulbasaur", + "\n\t\t\t", "poke-cli pokemon flutter-mane --types", + "\n\t\t\t", "poke-cli pokemon excadrill -t -a", + "\n", + styleBold.Render("\nFLAGS:"), "\n\t", "-a, --abilities", "\t", "Prints out the Pokémon's abilities.", + "\n\t", "-t, --types", "\t\t", "Prints out the Pokémon's typing."), + ) } flag.Parse() @@ -68,22 +77,18 @@ func PokemonCommand() { args := os.Args - err := ValidateArgs(args, errorColor) + err := ValidateArgs(args) if err != nil { - fmt.Println(errorColor.Render(err.Error())) + fmt.Println(err.Error()) os.Exit(1) } - if args[2] == "-h" || args[2] == "--help" { - flag.Usage() - os.Exit(0) - } - - endpoint := os.Args[1] + endpoint := strings.ToLower(args[1]) pokemonName := strings.ToLower(args[2]) if err := pokeFlags.Parse(args[3:]); err != nil { fmt.Printf("error parsing flags: %v\n", err) + pokeFlags.Usage() os.Exit(1) }