From c9d93ba3a0ce992d4cfe96cf6897144a58bda1ca Mon Sep 17 00:00:00 2001 From: Christian Date: Sat, 5 Oct 2024 07:50:48 -0700 Subject: [PATCH 1/4] updating fmt.Errorf() to accept strings (#44, #45) --- cmd/validateargs.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/cmd/validateargs.go b/cmd/validateargs.go index 388b936..499ee91 100644 --- a/cmd/validateargs.go +++ b/cmd/validateargs.go @@ -9,11 +9,13 @@ import ( func ValidatePokemonArgs(args []string) error { if len(args) > 5 { - return fmt.Errorf(errorBorder.Render(errorColor.Render("Error!"), "\nToo many arguments")) + errMessage := errorBorder.Render(errorColor.Render("Error!"), "\nToo many arguments") + return fmt.Errorf("%s", errMessage) } if len(args) < 3 { - 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")) + errMessage := 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") + return fmt.Errorf("%s", errMessage) } if len(args) > 3 { @@ -22,7 +24,8 @@ func ValidatePokemonArgs(args []string) error { 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)) + renderedError := errorBorder.Render(formattedString) + return fmt.Errorf("%s", renderedError) } } } @@ -38,12 +41,13 @@ func ValidatePokemonArgs(args []string) error { // ValidateTypesArgs validates the command line arguments func ValidateTypesArgs(args []string) error { if len(args) > 3 { - return fmt.Errorf(errorBorder.Render(errorColor.Render("Error!"), "\nToo many arguments")) + errMessage := errorBorder.Render(errorColor.Render("Error!"), "\nToo many arguments") + return fmt.Errorf("%s", errMessage) } if len(args) == 3 && (args[2] != "-h" && args[2] != "--help") { - fmt.Println(errorBorder.Render("Error! The only currently available options\nafter [types] command is '-h' or '--help'")) - return nil + errMessage := errorBorder.Render("Error! The only currently available options\nafter [types] command is '-h' or '--help'") + return fmt.Errorf("%s", errMessage) } return nil From b36d4d3a09ce4dc551c960660b60e715f6a043a2 Mon Sep 17 00:00:00 2001 From: Christian Date: Sat, 5 Oct 2024 07:59:02 -0700 Subject: [PATCH 2/4] adding TooManyArgs test (#43) --- cmd/types_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/cmd/types_test.go b/cmd/types_test.go index 06ef7e3..bd2f957 100644 --- a/cmd/types_test.go +++ b/cmd/types_test.go @@ -16,3 +16,16 @@ func TestValidateTypesArgs_ValidInput(t *testing.T) { assert.NoError(t, err, "Expected no error for valid input") } } + +func TestValidateTypesArgs_TooManyArgs(t *testing.T) { + invalidInputs := [][]string{ + {"poke-cli", "types", "ground"}, + } + expectedError := "error, too many arguemnts\n" + + for _, input := range invalidInputs { + err := ValidateTypesArgs(input) + assert.Error(t, err, "Expected error for too many arguments") + assert.NotEqual(t, expectedError, err.Error()) + } +} From edcb953f2a458e123cf380f2701f676288f789db Mon Sep 17 00:00:00 2001 From: Christian Date: Sat, 5 Oct 2024 08:00:15 -0700 Subject: [PATCH 3/4] moving code that processes reults into a helper method (#42) --- cmd/types.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/cmd/types.go b/cmd/types.go index 5d426eb..88f22cd 100644 --- a/cmd/types.go +++ b/cmd/types.go @@ -50,6 +50,19 @@ func (m model) View() string { return "Select a type! Hit 'Q' or 'CTRL-C' to quit.\n" + baseStyle.Render(m.table.View()) + "\n" } +func selectionResult(endpoint, typesName string) error { + baseURL := "https://pokeapi.co/api/v2/" + + typeResponse, typeName, _ := connections.TypesApiCall(endpoint, typesName, baseURL) + + capitalizedString := cases.Title(language.English).String(typeName) + + pokemonCount := len(typeResponse.Pokemon) + + fmt.Printf("You selected Type: %s\nNumber of Pokémon with type: %d\n", capitalizedString, pokemonCount) + return nil +} + func TypesCommand() { flag.Usage = func() { helpMessage := helpBorder.Render( @@ -123,8 +136,6 @@ func TypesCommand() { t.SetStyles(s) m := model{table: t} - - // Run the program and capture the final state programModel, err := tea.NewProgram(m).Run() if err != nil { fmt.Println("Error running program:", err) @@ -133,14 +144,14 @@ func TypesCommand() { // Type assert to model and access the selected option finalModel := programModel.(model) - - endpoint := strings.ToLower(args[1])[0:4] typesName := strings.ToLower(finalModel.selectedOption) - typeResponse, typeName, _ := connections.TypesApiCall(endpoint, typesName, "https://pokeapi.co/api/v2/") - capitalizedString := cases.Title(language.English).String(typeName) - - pokemonCount := len(typeResponse.Pokemon) + // Extract the first 4 characters of the endpoint from the argument + endpoint := strings.ToLower(args[1])[0:4] - fmt.Printf("You selected Type: %s\nNumber of Pokémon with type: %d\n", capitalizedString, pokemonCount) + // Call the selectionTable function with the selected type and endpoint + if err := selectionResult(endpoint, typesName); err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } } From 6fb76dfaa3b9c26c25008a021b16d1e5aed520a2 Mon Sep 17 00:00:00 2001 From: Christian Date: Sat, 5 Oct 2024 08:01:32 -0700 Subject: [PATCH 4/4] updating version numbers --- .github/workflows/ci.yml | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4bde0b3..ef0f996 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,7 @@ on: branches: - main env: - VERSION_NUMBER: 'v0.6.0' + VERSION_NUMBER: 'v0.6.1' REGISTRY_NAME: digitalghostdev/poke-cli jobs: diff --git a/README.md b/README.md index b23187b..7971dbd 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ pokemon-logo

Pokémon CLI

version-label - docker-image-size + docker-image-size