Skip to content

Commit ef71de0

Browse files
Merge pull request #152 from digitalghost-dev/1.3.1
1.3.1
2 parents c30394c + 79180a2 commit ef71de0

File tree

16 files changed

+219
-104
lines changed

16 files changed

+219
-104
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ on:
2626
- main
2727

2828
env:
29-
VERSION_NUMBER: 'v1.3.0'
29+
VERSION_NUMBER: 'v1.3.1'
3030
DOCKERHUB_REGISTRY_NAME: 'digitalghostdev/poke-cli'
3131
AWS_REGION: 'us-west-2'
3232

.goreleaser.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ builds:
1414
- windows
1515
- darwin
1616
ldflags:
17-
- -s -w -X main.version=v1.3.0
17+
- -s -w -X main.version=v1.3.1
1818

1919
archives:
2020
- format: tar.gz

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# build 1
2-
FROM golang:1.24.2-alpine3.21 AS build
2+
FROM golang:1.24.4-alpine3.21 AS build
33

44
WORKDIR /app
55

@@ -8,7 +8,7 @@ RUN go mod download
88

99
COPY . .
1010

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

1313
# build 2
1414
FROM --platform=$BUILDPLATFORM alpine:latest

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<img height="250" width="350" src="pokemon.svg" alt="pokemon-logo"/>
33
<h1>Pokémon CLI</h1>
44
<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">
5-
<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">
5+
<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">
66
<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">
77
</div>
88
<div align="center">
@@ -76,11 +76,11 @@ View future plans in the [Roadmap](#roadmap) section.
7676
3. Choose how to interact with the container:
7777
* Run a single command and exit:
7878
```bash
79-
docker run --rm -it digitalghostdev/poke-cli:v1.3.0 <command> [subcommand] flag]
79+
docker run --rm -it digitalghostdev/poke-cli:v1.3.1 <command> [subcommand] flag]
8080
```
8181
* Enter the container and use its shell:
8282
```bash
83-
docker run --rm -it --name poke-cli --entrypoint /bin/sh digitalghostdev/poke-cli:v1.3.0 -c "cd /app && exec sh"
83+
docker run --rm -it --name poke-cli --entrypoint /bin/sh digitalghostdev/poke-cli:v1.3.1 -c "cd /app && exec sh"
8484
# placed into the /app directory, run the program with './poke-cli'
8585
# example: ./poke-cli ability swift-swim
8686
```

cli.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,28 @@ func runCLI(args []string) int {
9494
return 2
9595
}
9696

97-
commands := map[string]func(){
97+
remainingArgs := mainFlagSet.Args()
98+
99+
commands := map[string]func() int{
98100
"ability": utils.HandleCommandOutput(ability.AbilityCommand),
99101
"move": utils.HandleCommandOutput(move.MoveCommand),
100102
"natures": utils.HandleCommandOutput(natures.NaturesCommand),
101103
"pokemon": utils.HandleCommandOutput(pokemon.PokemonCommand),
102104
"types": utils.HandleCommandOutput(types.TypesCommand),
103-
"search": search.SearchCommand,
105+
"search": func() int {
106+
search.SearchCommand()
107+
return 0
108+
},
104109
}
105110

106111
cmdArg := ""
107-
if len(os.Args) >= 2 {
108-
cmdArg = os.Args[1]
112+
if len(remainingArgs) >= 1 {
113+
cmdArg = remainingArgs[0]
109114
}
110115
cmdFunc, exists := commands[cmdArg]
111116

112117
switch {
113-
case len(os.Args) < 2:
118+
case len(remainingArgs) == 0 && !*latestFlag && !*shortLatestFlag && !*currentVersionFlag && !*shortCurrentVersionFlag:
114119
mainFlagSet.Usage()
115120
return 1
116121
case *latestFlag || *shortLatestFlag:
@@ -120,13 +125,11 @@ func runCLI(args []string) int {
120125
currentVersion()
121126
return 0
122127
case exists:
123-
cmdFunc()
124-
return 0
128+
return cmdFunc()
125129
default:
126-
command := os.Args[1]
127130
errMessage := styling.ErrorBorder.Render(
128131
styling.ErrorColor.Render("Error!"),
129-
fmt.Sprintf("\n\t%-15s", fmt.Sprintf("'%s' is not a valid command.\n", command)),
132+
fmt.Sprintf("\n\t%-15s", fmt.Sprintf("'%s' is not a valid command.\n", cmdArg)),
130133
styling.StyleBold.Render("\nCommands:"),
131134
fmt.Sprintf("\n\t%-15s %s", "ability", "Get details about an ability"),
132135
fmt.Sprintf("\n\t%-15s %s", "move", "Get details about a move"),
@@ -138,7 +141,6 @@ func runCLI(args []string) int {
138141
)
139142
output.WriteString(errMessage)
140143

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

144146
return 1

cli_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,32 @@ func TestRunCLI(t *testing.T) {
122122
}
123123
}
124124

125+
// TODO: finish testing different commands?
126+
func TestRunCLI_VariousCommands(t *testing.T) {
127+
tests := []struct {
128+
name string
129+
args []string
130+
expected int
131+
}{
132+
//{"Invalid command", []string{"foobar"}, 1},
133+
{"Latest flag long", []string{"--latest"}, 0},
134+
{"Latest flag short", []string{"-l"}, 0},
135+
{"Version flag long", []string{"--version"}, 0},
136+
{"Version flag short", []string{"-v"}, 0},
137+
//{"Missing Pokémon name", []string{"pokemon"}, 1},
138+
//{"Another invalid command", []string{"invalid"}, 1},
139+
}
140+
141+
for _, tt := range tests {
142+
t.Run(tt.name, func(t *testing.T) {
143+
exitCode := runCLI(tt.args)
144+
if exitCode != tt.expected {
145+
t.Errorf("expected %d, got %d for args %v", tt.expected, exitCode, tt.args)
146+
}
147+
})
148+
}
149+
}
150+
125151
func TestMainFunction(t *testing.T) {
126152
originalExit := exit
127153
defer func() { exit = originalExit }() // Restore original exit after test

cmd/move/move.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ func MoveCommand() (string, error) {
4444
endpoint := strings.ToLower(args[0])
4545
moveName := strings.ToLower(args[1])
4646

47-
moveStruct, moveName, _ := connections.MoveApiCall(endpoint, moveName, connections.APIURL)
47+
moveStruct, moveName, err := connections.MoveApiCall(endpoint, moveName, connections.APIURL)
48+
if err != nil {
49+
output.WriteString(err.Error())
50+
return output.String(), err
51+
}
4852

4953
moveInfoContainer(&output, moveStruct, moveName)
5054
moveEffectContainer(&output, moveStruct)

cmd/pokemon/pokemon.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,19 @@ func PokemonCommand() (string, error) {
6565
os.Exit(1)
6666
}
6767

68-
_, pokemonName, pokemonID, pokemonWeight, pokemonHeight, err := connections.PokemonApiCall(endpoint, pokemonName, connections.APIURL)
68+
pokemonStruct, pokemonName, err := connections.PokemonApiCall(endpoint, pokemonName, connections.APIURL)
6969
if err != nil {
70-
fmt.Println(err)
71-
os.Exit(1)
70+
output.WriteString(err.Error())
71+
return output.String(), err
7272
}
7373
capitalizedString := cases.Title(language.English).String(strings.ReplaceAll(pokemonName, "-", " "))
7474

7575
// Weight calculation
76-
weightKilograms := float64(pokemonWeight) / 10
76+
weightKilograms := float64(pokemonStruct.Weight) / 10
7777
weightPounds := float64(weightKilograms) * 2.20462
7878

7979
// Height calculation
80-
heightMeters := float64(pokemonHeight) / 10
80+
heightMeters := float64(pokemonStruct.Height) / 10
8181
heightFeet := heightMeters * 3.28084
8282
feet := int(heightFeet)
8383
inches := int(math.Round((heightFeet - float64(feet)) * 12)) // Use math.Round to avoid truncation
@@ -90,7 +90,7 @@ func PokemonCommand() (string, error) {
9090

9191
output.WriteString(fmt.Sprintf(
9292
"Your selected Pokémon: %s\n%s National Pokédex #: %d\n%s Weight: %.1fkg (%.1f lbs)\n%s Height: %.1fm (%d′%02d″)\n",
93-
capitalizedString, styling.ColoredBullet, pokemonID,
93+
capitalizedString, styling.ColoredBullet, pokemonStruct.ID,
9494
styling.ColoredBullet, weightKilograms, weightPounds,
9595
styling.ColoredBullet, heightFeet, feet, inches,
9696
))

cmd/utils/output.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ import (
55
"os"
66
)
77

8-
// HandleCommandOutput wraps a function that returns (string, error) into a no-arg function
9-
// that prints the output to stdout or stderr depending on whether an error occurred.
10-
func HandleCommandOutput(fn func() (string, error)) func() {
11-
return func() {
8+
// HandleCommandOutput takes a function that returns (string, error) and wraps it in a no-argument
9+
// function that writes the returned string to stdout if there's no error, or to stderr if there is.
10+
// It returns an exit code: 0 on success, 1 on error.
11+
func HandleCommandOutput(fn func() (string, error)) func() int {
12+
return func() int {
1213
output, err := fn()
1314
if err != nil {
1415
fmt.Fprintln(os.Stderr, output)
15-
return
16+
return 1
1617
}
1718
fmt.Println(output)
19+
return 0
1820
}
1921
}

cmd/utils/output_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ func TestHandleCommandOutput_Success(t *testing.T) {
3131
return "it worked", nil
3232
}
3333

34-
output := captureOutput(&os.Stdout, HandleCommandOutput(fn))
34+
output := captureOutput(&os.Stdout, func() {
35+
HandleCommandOutput(fn)()
36+
})
3537

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

46-
output := captureOutput(&os.Stderr, HandleCommandOutput(fn))
48+
output := captureOutput(&os.Stderr, func() {
49+
HandleCommandOutput(fn)()
50+
})
4751

4852
if output != "something failed\n" {
4953
t.Errorf("expected 'something failed\\n', got %q", output)

0 commit comments

Comments
 (0)