Skip to content
Merged

1.3.1 #152

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ on:
- main

env:
VERSION_NUMBER: 'v1.3.0'
VERSION_NUMBER: 'v1.3.1'
DOCKERHUB_REGISTRY_NAME: 'digitalghostdev/poke-cli'
AWS_REGION: 'us-west-2'

Expand Down
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ builds:
- windows
- darwin
ldflags:
- -s -w -X main.version=v1.3.0
- -s -w -X main.version=v1.3.1

archives:
- format: tar.gz
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# build 1
FROM golang:1.24.2-alpine3.21 AS build
FROM golang:1.24.4-alpine3.21 AS build

WORKDIR /app

Expand All @@ -8,7 +8,7 @@ RUN go mod download

COPY . .

RUN go build -ldflags "-X main.version=v1.3.0" -o poke-cli .
RUN go build -ldflags "-X main.version=v1.3.1" -o poke-cli .

# build 2
FROM --platform=$BUILDPLATFORM alpine:latest
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<img height="250" width="350" src="pokemon.svg" alt="pokemon-logo"/>
<h1>Pokémon CLI</h1>
<img src="https://img.shields.io/github/v/release/digitalghost-dev/poke-cli?style=flat-square&logo=git&logoColor=FFCC00&label=Release%20Version&labelColor=EEE&color=FFCC00" alt="version-label">
<img src="https://img.shields.io/docker/image-size/digitalghostdev/poke-cli/v1.3.0?arch=arm64&style=flat-square&logo=docker&logoColor=FFCC00&labelColor=EEE&color=FFCC00" alt="docker-image-size">
<img src="https://img.shields.io/docker/image-size/digitalghostdev/poke-cli/v1.3.1?arch=arm64&style=flat-square&logo=docker&logoColor=FFCC00&labelColor=EEE&color=FFCC00" alt="docker-image-size">
<img src="https://img.shields.io/github/actions/workflow/status/digitalghost-dev/poke-cli/ci.yml?branch=main&style=flat-square&logo=github&logoColor=FFCC00&label=CI&labelColor=EEE&color=FFCC00" alt="ci-status-badge">
</div>
<div align="center">
Expand Down Expand Up @@ -76,11 +76,11 @@ View future plans in the [Roadmap](#roadmap) section.
3. Choose how to interact with the container:
* Run a single command and exit:
```bash
docker run --rm -it digitalghostdev/poke-cli:v1.3.0 <command> [subcommand] flag]
docker run --rm -it digitalghostdev/poke-cli:v1.3.1 <command> [subcommand] flag]
```
* Enter the container and use its shell:
```bash
docker run --rm -it --name poke-cli --entrypoint /bin/sh digitalghostdev/poke-cli:v1.3.0 -c "cd /app && exec sh"
docker run --rm -it --name poke-cli --entrypoint /bin/sh digitalghostdev/poke-cli:v1.3.1 -c "cd /app && exec sh"
# placed into the /app directory, run the program with './poke-cli'
# example: ./poke-cli ability swift-swim
```
Expand Down
22 changes: 12 additions & 10 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,28 @@
return 2
}

commands := map[string]func(){
remainingArgs := mainFlagSet.Args()

commands := map[string]func() int{
"ability": utils.HandleCommandOutput(ability.AbilityCommand),
"move": utils.HandleCommandOutput(move.MoveCommand),
"natures": utils.HandleCommandOutput(natures.NaturesCommand),
"pokemon": utils.HandleCommandOutput(pokemon.PokemonCommand),
"types": utils.HandleCommandOutput(types.TypesCommand),
"search": search.SearchCommand,
"search": func() int {
search.SearchCommand()
return 0
},

Check warning on line 108 in cli.go

View check run for this annotation

Codecov / codecov/patch

cli.go#L106-L108

Added lines #L106 - L108 were not covered by tests
}

cmdArg := ""
if len(os.Args) >= 2 {
cmdArg = os.Args[1]
if len(remainingArgs) >= 1 {
cmdArg = remainingArgs[0]

Check warning on line 113 in cli.go

View check run for this annotation

Codecov / codecov/patch

cli.go#L113

Added line #L113 was not covered by tests
}
cmdFunc, exists := commands[cmdArg]

switch {
case len(os.Args) < 2:
case len(remainingArgs) == 0 && !*latestFlag && !*shortLatestFlag && !*currentVersionFlag && !*shortCurrentVersionFlag:

Check warning on line 118 in cli.go

View check run for this annotation

Codecov / codecov/patch

cli.go#L118

Added line #L118 was not covered by tests
mainFlagSet.Usage()
return 1
case *latestFlag || *shortLatestFlag:
Expand All @@ -120,13 +125,11 @@
currentVersion()
return 0
case exists:
cmdFunc()
return 0
return cmdFunc()

Check warning on line 128 in cli.go

View check run for this annotation

Codecov / codecov/patch

cli.go#L128

Added line #L128 was not covered by tests
default:
command := os.Args[1]
errMessage := styling.ErrorBorder.Render(
styling.ErrorColor.Render("Error!"),
fmt.Sprintf("\n\t%-15s", fmt.Sprintf("'%s' is not a valid command.\n", command)),
fmt.Sprintf("\n\t%-15s", fmt.Sprintf("'%s' is not a valid command.\n", cmdArg)),

Check warning on line 132 in cli.go

View check run for this annotation

Codecov / codecov/patch

cli.go#L132

Added line #L132 was not covered by tests
styling.StyleBold.Render("\nCommands:"),
fmt.Sprintf("\n\t%-15s %s", "ability", "Get details about an ability"),
fmt.Sprintf("\n\t%-15s %s", "move", "Get details about a move"),
Expand All @@ -138,7 +141,6 @@
)
output.WriteString(errMessage)

// This would typically be returned in a function or passed to something else
fmt.Println(output.String())

return 1
Expand Down
26 changes: 26 additions & 0 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,32 @@ func TestRunCLI(t *testing.T) {
}
}

// TODO: finish testing different commands?
func TestRunCLI_VariousCommands(t *testing.T) {
tests := []struct {
name string
args []string
expected int
}{
//{"Invalid command", []string{"foobar"}, 1},
{"Latest flag long", []string{"--latest"}, 0},
{"Latest flag short", []string{"-l"}, 0},
{"Version flag long", []string{"--version"}, 0},
{"Version flag short", []string{"-v"}, 0},
//{"Missing Pokémon name", []string{"pokemon"}, 1},
//{"Another invalid command", []string{"invalid"}, 1},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exitCode := runCLI(tt.args)
if exitCode != tt.expected {
t.Errorf("expected %d, got %d for args %v", tt.expected, exitCode, tt.args)
}
})
}
}

func TestMainFunction(t *testing.T) {
originalExit := exit
defer func() { exit = originalExit }() // Restore original exit after test
Expand Down
6 changes: 5 additions & 1 deletion cmd/move/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@
endpoint := strings.ToLower(args[0])
moveName := strings.ToLower(args[1])

moveStruct, moveName, _ := connections.MoveApiCall(endpoint, moveName, connections.APIURL)
moveStruct, moveName, err := connections.MoveApiCall(endpoint, moveName, connections.APIURL)
if err != nil {
output.WriteString(err.Error())
return output.String(), err
}

Check warning on line 51 in cmd/move/move.go

View check run for this annotation

Codecov / codecov/patch

cmd/move/move.go#L49-L51

Added lines #L49 - L51 were not covered by tests

moveInfoContainer(&output, moveStruct, moveName)
moveEffectContainer(&output, moveStruct)
Expand Down
12 changes: 6 additions & 6 deletions cmd/pokemon/pokemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@
os.Exit(1)
}

_, pokemonName, pokemonID, pokemonWeight, pokemonHeight, err := connections.PokemonApiCall(endpoint, pokemonName, connections.APIURL)
pokemonStruct, pokemonName, err := connections.PokemonApiCall(endpoint, pokemonName, connections.APIURL)
if err != nil {
fmt.Println(err)
os.Exit(1)
output.WriteString(err.Error())
return output.String(), err

Check warning on line 71 in cmd/pokemon/pokemon.go

View check run for this annotation

Codecov / codecov/patch

cmd/pokemon/pokemon.go#L70-L71

Added lines #L70 - L71 were not covered by tests
}
capitalizedString := cases.Title(language.English).String(strings.ReplaceAll(pokemonName, "-", " "))

// Weight calculation
weightKilograms := float64(pokemonWeight) / 10
weightKilograms := float64(pokemonStruct.Weight) / 10
weightPounds := float64(weightKilograms) * 2.20462

// Height calculation
heightMeters := float64(pokemonHeight) / 10
heightMeters := float64(pokemonStruct.Height) / 10
heightFeet := heightMeters * 3.28084
feet := int(heightFeet)
inches := int(math.Round((heightFeet - float64(feet)) * 12)) // Use math.Round to avoid truncation
Expand All @@ -90,7 +90,7 @@

output.WriteString(fmt.Sprintf(
"Your selected Pokémon: %s\n%s National Pokédex #: %d\n%s Weight: %.1fkg (%.1f lbs)\n%s Height: %.1fm (%d′%02d″)\n",
capitalizedString, styling.ColoredBullet, pokemonID,
capitalizedString, styling.ColoredBullet, pokemonStruct.ID,
styling.ColoredBullet, weightKilograms, weightPounds,
styling.ColoredBullet, heightFeet, feet, inches,
))
Expand Down
12 changes: 7 additions & 5 deletions cmd/utils/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import (
"os"
)

// HandleCommandOutput wraps a function that returns (string, error) into a no-arg function
// that prints the output to stdout or stderr depending on whether an error occurred.
func HandleCommandOutput(fn func() (string, error)) func() {
return func() {
// HandleCommandOutput takes a function that returns (string, error) and wraps it in a no-argument
// function that writes the returned string to stdout if there's no error, or to stderr if there is.
// It returns an exit code: 0 on success, 1 on error.
func HandleCommandOutput(fn func() (string, error)) func() int {
return func() int {
output, err := fn()
if err != nil {
fmt.Fprintln(os.Stderr, output)
return
return 1
}
fmt.Println(output)
return 0
}
}
8 changes: 6 additions & 2 deletions cmd/utils/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func TestHandleCommandOutput_Success(t *testing.T) {
return "it worked", nil
}

output := captureOutput(&os.Stdout, HandleCommandOutput(fn))
output := captureOutput(&os.Stdout, func() {
HandleCommandOutput(fn)()
})

if output != "it worked\n" {
t.Errorf("expected 'it worked\\n', got %q", output)
Expand All @@ -43,7 +45,9 @@ func TestHandleCommandOutput_Error(t *testing.T) {
return "something failed", errors.New("error")
}

output := captureOutput(&os.Stderr, HandleCommandOutput(fn))
output := captureOutput(&os.Stderr, func() {
HandleCommandOutput(fn)()
})

if output != "something failed\n" {
t.Errorf("expected 'something failed\\n', got %q", output)
Expand Down
8 changes: 4 additions & 4 deletions connections/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ func MoveApiCall(endpoint string, moveName string, baseURL string) (structs.Move
return structs.MoveJSONStruct{}, "", fmt.Errorf("%s", errMessage)
}

return moveStruct, moveName, nil
return moveStruct, moveStruct.Name, nil
}

// PokemonApiCall function for calling the pokemon endpoint of the pokeAPI
func PokemonApiCall(endpoint string, pokemonName string, baseURL string) (structs.PokemonJSONStruct, string, int, int, int, error) {
func PokemonApiCall(endpoint string, pokemonName string, baseURL string) (structs.PokemonJSONStruct, string, error) {
fullURL := baseURL + endpoint + "/" + pokemonName

var pokemonStruct structs.PokemonJSONStruct
Expand All @@ -96,10 +96,10 @@ func PokemonApiCall(endpoint string, pokemonName string, baseURL string) (struct
styling.ErrorColor.Render("Error!"),
"\nPokémon not found.\n\u2022 Perhaps a typo?\n\u2022 Missing a hyphen instead of a space?",
)
return structs.PokemonJSONStruct{}, "", 0, 0, 0, fmt.Errorf("%s", errMessage)
return structs.PokemonJSONStruct{}, "", fmt.Errorf("%s", errMessage)
}

return pokemonStruct, pokemonStruct.Name, pokemonStruct.ID, pokemonStruct.Weight, pokemonStruct.Height, nil
return pokemonStruct, pokemonStruct.Name, nil
}

// TypesApiCall function for calling the type endpoint of the pokeAPI
Expand Down
Loading