Skip to content

Commit 083b9c6

Browse files
Merge pull request #146 from digitalghost-dev/1.2.4
1.2.4
2 parents a519245 + 602db2d commit 083b9c6

35 files changed

+542
-898
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.2.3'
29+
VERSION_NUMBER: 'v1.2.4'
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.2.3
17+
- -s -w -X main.version=v1.2.4
1818

1919
archives:
2020
- format: tar.gz

CHANGELOG.md

Lines changed: 0 additions & 567 deletions
This file was deleted.

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ RUN go mod download
88

99
COPY . .
1010

11-
RUN go build -ldflags "-X main.version=v1.2.3" -o poke-cli .
11+
RUN go build -ldflags "-X main.version=v1.2.4" -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.2.3?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.2.4?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.2.3 <command> [subcommand] flag]
79+
docker run --rm -it digitalghostdev/poke-cli:v1.2.4 <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.2.3 -c "cd /app && exec sh"
83+
docker run --rm -it --name poke-cli --entrypoint /bin/sh digitalghostdev/poke-cli:v1.2.4 -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: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ package main
33
import (
44
"flag"
55
"fmt"
6-
"github.com/digitalghost-dev/poke-cli/cmd"
76
"github.com/digitalghost-dev/poke-cli/cmd/ability"
87
"github.com/digitalghost-dev/poke-cli/cmd/move"
98
"github.com/digitalghost-dev/poke-cli/cmd/natures"
9+
"github.com/digitalghost-dev/poke-cli/cmd/pokemon"
1010
"github.com/digitalghost-dev/poke-cli/cmd/search"
1111
"github.com/digitalghost-dev/poke-cli/cmd/types"
12+
"github.com/digitalghost-dev/poke-cli/cmd/utils"
1213
"github.com/digitalghost-dev/poke-cli/flags"
1314
"github.com/digitalghost-dev/poke-cli/styling"
1415
"os"
1516
"runtime/debug"
17+
"strings"
1618
)
1719

1820
var version = "(devel)"
@@ -38,13 +40,9 @@ func currentVersion() {
3840
}
3941
}
4042

41-
func printWrapper(f func() string) func() {
42-
return func() {
43-
fmt.Println(f())
44-
}
45-
}
46-
4743
func runCLI(args []string) int {
44+
var output strings.Builder
45+
4846
mainFlagSet := flag.NewFlagSet("poke-cli", flag.ContinueOnError)
4947

5048
// -l, --latest flag retrieves the latest Docker image and GitHub release versions available
@@ -97,27 +95,34 @@ func runCLI(args []string) int {
9795
}
9896

9997
commands := map[string]func(){
100-
"ability": printWrapper(ability.AbilityCommand),
101-
"move": printWrapper(move.MoveCommand),
102-
"natures": printWrapper(natures.NaturesCommand),
103-
"pokemon": cmd.PokemonCommand,
104-
"types": printWrapper(types.TypesCommand),
98+
"ability": utils.HandleCommandOutput(ability.AbilityCommand),
99+
"move": utils.HandleCommandOutput(move.MoveCommand),
100+
"natures": utils.HandleCommandOutput(natures.NaturesCommand),
101+
"pokemon": utils.HandleCommandOutput(pokemon.PokemonCommand),
102+
"types": utils.HandleCommandOutput(types.TypesCommand),
105103
"search": search.SearchCommand,
106104
}
107105

108-
if len(os.Args) < 2 {
106+
cmdArg := ""
107+
if len(os.Args) >= 2 {
108+
cmdArg = os.Args[1]
109+
}
110+
cmdFunc, exists := commands[cmdArg]
111+
112+
switch {
113+
case len(os.Args) < 2:
109114
mainFlagSet.Usage()
110115
return 1
111-
} else if *latestFlag || *shortLatestFlag {
116+
case *latestFlag || *shortLatestFlag:
112117
flags.LatestFlag()
113118
return 0
114-
} else if *currentVersionFlag || *shortCurrentVersionFlag {
119+
case *currentVersionFlag || *shortCurrentVersionFlag:
115120
currentVersion()
116121
return 0
117-
} else if cmdFunc, exists := commands[os.Args[1]]; exists {
122+
case exists:
118123
cmdFunc()
119124
return 0
120-
} else {
125+
default:
121126
command := os.Args[1]
122127
errMessage := styling.ErrorBorder.Render(
123128
styling.ErrorColor.Render("Error!"),
@@ -131,7 +136,11 @@ func runCLI(args []string) int {
131136
fmt.Sprintf("\n\t%-15s %s", "types", "Get details about a typing"),
132137
fmt.Sprintf("\n\nAlso run %s for more info!", styling.StyleBold.Render("poke-cli -h")),
133138
)
134-
fmt.Printf("%s\n", errMessage)
139+
output.WriteString(errMessage)
140+
141+
// This would typically be returned in a function or passed to something else
142+
fmt.Println(output.String())
143+
135144
return 1
136145
}
137146
}

cli_test.go

Lines changed: 16 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package main
22

33
import (
44
"bytes"
5+
"github.com/digitalghost-dev/poke-cli/cmd/utils"
6+
"github.com/digitalghost-dev/poke-cli/styling"
7+
"github.com/stretchr/testify/assert"
58
"os"
69
"regexp"
710
"testing"
@@ -93,119 +96,28 @@ func TestRunCLI(t *testing.T) {
9396
expectedCode int
9497
}{
9598
{
96-
name: "No Arguments",
97-
args: []string{},
98-
expectedOutput: "╭──────────────────────────────────────────────────────────╮\n" +
99-
"│Welcome! This tool displays data related to Pokémon! │\n" +
100-
"│ │\n" +
101-
"│ USAGE: │\n" +
102-
"│ poke-cli [flag] │\n" +
103-
"│ poke-cli <command> [flag] │\n" +
104-
"│ poke-cli <command> <subcommand> [flag] │\n" +
105-
"│ │\n" +
106-
"│ FLAGS: │\n" +
107-
"│ -h, --help Shows the help menu │\n" +
108-
"│ -l, --latest Prints the latest version available │\n" +
109-
"│ -v, --version Prints the current version │\n" +
110-
"│ │\n" +
111-
"│ COMMANDS: │\n" +
112-
"│ ability Get details about an ability │\n" +
113-
"│ move Get details about a move │\n" +
114-
"│ natures Get details about all natures │\n" +
115-
"│ pokemon Get details about a Pokémon │\n" +
116-
"│ search Search for a resource │\n" +
117-
"│ types Get details about a typing │\n" +
118-
"│ │\n" +
119-
"│ hint: when calling a resource with a space, use a hyphen │\n" +
120-
"│ example: poke-cli ability strong-jaw │\n" +
121-
"│ example: poke-cli pokemon flutter-mane │\n" +
122-
"╰──────────────────────────────────────────────────────────╯",
123-
expectedCode: 0,
124-
},
125-
{
126-
name: "Help Flag Short",
127-
args: []string{"-h"},
128-
expectedOutput: "╭──────────────────────────────────────────────────────────╮\n" +
129-
"│Welcome! This tool displays data related to Pokémon! │\n" +
130-
"│ │\n" +
131-
"│ USAGE: │\n" +
132-
"│ poke-cli [flag] │\n" +
133-
"│ poke-cli <command> [flag] │\n" +
134-
"│ poke-cli <command> <subcommand> [flag] │\n" +
135-
"│ │\n" +
136-
"│ FLAGS: │\n" +
137-
"│ -h, --help Shows the help menu │\n" +
138-
"│ -l, --latest Prints the latest version available │\n" +
139-
"│ -v, --version Prints the current version │\n" +
140-
"│ │\n" +
141-
"│ COMMANDS: │\n" +
142-
"│ ability Get details about an ability │\n" +
143-
"│ move Get details about a move │\n" +
144-
"│ natures Get details about all natures │\n" +
145-
"│ pokemon Get details about a Pokémon │\n" +
146-
"│ search Search for a resource │\n" +
147-
"│ types Get details about a typing │\n" +
148-
"│ │\n" +
149-
"│ hint: when calling a resource with a space, use a hyphen │\n" +
150-
"│ example: poke-cli ability strong-jaw │\n" +
151-
"│ example: poke-cli pokemon flutter-mane │\n" +
152-
"╰──────────────────────────────────────────────────────────╯",
153-
expectedCode: 0,
154-
},
155-
{
156-
name: "Help Flag Long",
157-
args: []string{"--help"},
158-
expectedOutput: "╭──────────────────────────────────────────────────────────╮\n" +
159-
"│Welcome! This tool displays data related to Pokémon! │\n" +
160-
"│ │\n" +
161-
"│ USAGE: │\n" +
162-
"│ poke-cli [flag] │\n" +
163-
"│ poke-cli <command> [flag] │\n" +
164-
"│ poke-cli <command> <subcommand> [flag] │\n" +
165-
"│ │\n" +
166-
"│ FLAGS: │\n" +
167-
"│ -h, --help Shows the help menu │\n" +
168-
"│ -l, --latest Prints the latest version available │\n" +
169-
"│ -v, --version Prints the current version │\n" +
170-
"│ │\n" +
171-
"│ COMMANDS: │\n" +
172-
"│ ability Get details about an ability │\n" +
173-
"│ move Get details about a move │\n" +
174-
"│ natures Get details about all natures │\n" +
175-
"│ pokemon Get details about a Pokémon │\n" +
176-
"│ search Search for a resource │\n" +
177-
"│ types Get details about a typing │\n" +
178-
"│ │\n" +
179-
"│ hint: when calling a resource with a space, use a hyphen │\n" +
180-
"│ example: poke-cli ability strong-jaw │\n" +
181-
"│ example: poke-cli pokemon flutter-mane │\n" +
182-
"╰──────────────────────────────────────────────────────────╯",
183-
184-
expectedCode: 0,
185-
},
186-
{
187-
name: "Invalid Command",
188-
args: []string{"invalid"},
189-
expectedOutput: "Error!",
190-
expectedCode: 1,
99+
name: "Test CLI help flag",
100+
args: []string{"--help"},
101+
expectedOutput: utils.LoadGolden(t, "cli_help.golden"),
102+
expectedCode: 0,
191103
},
192104
}
193105

194106
for _, tt := range tests {
195107
t.Run(tt.name, func(t *testing.T) {
196-
exit = func(code int) {}
108+
originalArgs := os.Args
109+
os.Args = append([]string{"poke-cli"}, tt.args...)
110+
defer func() { os.Args = originalArgs }()
111+
112+
var exitCode int
197113
output := captureOutput(func() {
198-
code := runCLI(tt.args)
199-
if code != tt.expectedCode {
200-
t.Errorf("Expected exit code %d, got %d", tt.expectedCode, code)
201-
}
114+
exitCode = runCLI(tt.args)
202115
})
203116

204-
output = stripANSI(output)
117+
cleanOutput := styling.StripANSI(output)
205118

206-
if !bytes.Contains([]byte(output), []byte(tt.expectedOutput)) {
207-
t.Errorf("Expected output to contain %q, got %q", tt.expectedOutput, output)
208-
}
119+
assert.Equal(t, tt.expectedOutput, cleanOutput, "Output should match expected")
120+
assert.Equal(t, tt.expectedCode, exitCode, "Exit code should match expected")
209121
})
210122
}
211123
}

cmd/ability/ability.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"strings"
1414
)
1515

16-
func AbilityCommand() string {
16+
func AbilityCommand() (string, error) {
1717
var output strings.Builder
1818

1919
flag.Usage = func() {
@@ -38,12 +38,12 @@ func AbilityCommand() string {
3838

3939
if len(os.Args) == 3 && (os.Args[2] == "-h" || os.Args[2] == "--help") {
4040
flag.Usage()
41-
return output.String()
41+
return output.String(), nil
4242
}
4343

4444
if err := utils.ValidateAbilityArgs(args); err != nil {
4545
output.WriteString(err.Error())
46-
return output.String()
46+
return output.String(), err
4747
}
4848

4949
endpoint := strings.ToLower(args[1])
@@ -55,7 +55,7 @@ func AbilityCommand() string {
5555
if os.Getenv("GO_TESTING") != "1" {
5656
os.Exit(1)
5757
}
58-
return output.String()
58+
return output.String(), err
5959
}
6060

6161
abilitiesStruct, abilityName, err := connections.AbilityApiCall(endpoint, abilityName, connections.APIURL)
@@ -64,7 +64,7 @@ func AbilityCommand() string {
6464
fmt.Fprintln(os.Stderr, err)
6565
os.Exit(1)
6666
}
67-
return err.Error()
67+
return err.Error(), nil
6868
}
6969

7070
// Extract English short_effect
@@ -108,11 +108,9 @@ func AbilityCommand() string {
108108
if *pokemonFlag || *shortPokemonFlag {
109109
if err := flags.PokemonAbilitiesFlag(&output, endpoint, abilityName); err != nil {
110110
output.WriteString(fmt.Sprintf("error parsing flags: %v\n", err))
111-
if os.Getenv("GO_TESTING") != "1" {
112-
os.Exit(1)
113-
}
111+
return "", fmt.Errorf("error parsing flags: %w", err)
114112
}
115113
}
116114

117-
return output.String()
115+
return output.String(), nil
118116
}

cmd/ability/ability_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ func TestAbilityCommand(t *testing.T) {
3232
args: []string{"ability", "--help"},
3333
expectedOutput: utils.LoadGolden(t, "ability_help.golden"),
3434
},
35+
{
36+
name: "Ability help flag",
37+
args: []string{"ability", "-h"},
38+
expectedOutput: utils.LoadGolden(t, "ability_help.golden"),
39+
},
3540
{
3641
name: "Ability command: clear-body",
3742
args: []string{"ability", "clear-body"},
@@ -43,6 +48,11 @@ func TestAbilityCommand(t *testing.T) {
4348
expectedOutput: utils.LoadGolden(t, "ability_misspelled.golden"),
4449
wantError: true,
4550
},
51+
{
52+
name: "Ability command: --pokemon flag",
53+
args: []string{"ability", "anger-point", "--pokemon"},
54+
expectedOutput: utils.LoadGolden(t, "ability_flag_pokemon.golden"),
55+
},
4656
}
4757

4858
for _, tt := range tests {
@@ -51,7 +61,7 @@ func TestAbilityCommand(t *testing.T) {
5161
os.Args = append([]string{"poke-cli"}, tt.args...)
5262
defer func() { os.Args = originalArgs }()
5363

54-
output := AbilityCommand()
64+
output, _ := AbilityCommand()
5565
cleanOutput := styling.StripANSI(output)
5666

5767
assert.Equal(t, tt.expectedOutput, cleanOutput, "Output should match expected")

0 commit comments

Comments
 (0)