Skip to content
Merged

1.2.4 #146

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
1cccda5
removing changelog
digitalghost-dev May 14, 2025
eeea9a5
updating tests to use golden files (#136)
digitalghost-dev May 14, 2025
320ffaf
updating tests
digitalghost-dev May 14, 2025
0a3c3d0
adding test data (#136)
digitalghost-dev May 17, 2025
5967c28
moving pokemon command to own package (#143)
digitalghost-dev May 18, 2025
5217d16
changing to strings.Builder() to return string (#137)
digitalghost-dev May 18, 2025
09f236e
adding test data (#136)
digitalghost-dev May 19, 2025
674f710
updating test data (#136)
digitalghost-dev May 19, 2025
9b2b5fa
updating to switch statement (#142)
digitalghost-dev May 19, 2025
2c91204
updating version numbers
digitalghost-dev May 19, 2025
4776c74
updating function arguments
digitalghost-dev May 22, 2025
e2c0d4d
adding test data (#136)
digitalghost-dev May 22, 2025
eece390
adding io.Writer for Fprintf (#137)
digitalghost-dev May 22, 2025
c3023ac
updating tests
digitalghost-dev May 22, 2025
a7d00fe
changing to strings.Builder() to return string (#137)
digitalghost-dev May 22, 2025
01ada72
adding test data (#136)
digitalghost-dev May 23, 2025
366466e
updating tests
digitalghost-dev May 23, 2025
66f9841
returning an error (#144)
digitalghost-dev May 23, 2025
127113f
updating to use golden files (#136)
digitalghost-dev May 23, 2025
43a64ef
returning an error (#144)
digitalghost-dev May 24, 2025
aee9fa1
updating tests
digitalghost-dev May 24, 2025
46827fd
returning err instead of nil
digitalghost-dev May 24, 2025
daf5726
returning an error (#144)
digitalghost-dev May 24, 2025
964b590
updating tests
digitalghost-dev May 24, 2025
cb0c72b
updating flag calls to return an error (#144)
digitalghost-dev May 24, 2025
10ca070
updating path finding
digitalghost-dev May 24, 2025
e13d931
returning an error (#144)
digitalghost-dev May 25, 2025
4178d1a
updating tests
digitalghost-dev May 25, 2025
3ed3cbd
adding test data (#136)
digitalghost-dev May 25, 2025
9cc0a59
add executeAndPrint function to wrap command execution and handle output
digitalghost-dev May 25, 2025
c6c7476
updating error handling
digitalghost-dev May 25, 2025
93f30bb
adding func for image flag error checking (#145)
digitalghost-dev May 25, 2025
e9b3041
moving output wrapper function to own file
digitalghost-dev May 25, 2025
b047c56
importing functions from utils package
digitalghost-dev May 26, 2025
57bf0df
updating tests
digitalghost-dev May 26, 2025
214b978
returning an error (#144)
digitalghost-dev May 26, 2025
e3522b3
updating tests
digitalghost-dev May 26, 2025
602db2d
updating tests
digitalghost-dev May 27, 2025
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.2.3'
VERSION_NUMBER: 'v1.2.4'
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.2.3
- -s -w -X main.version=v1.2.4

archives:
- format: tar.gz
Expand Down
567 changes: 0 additions & 567 deletions CHANGELOG.md

This file was deleted.

2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ RUN go mod download

COPY . .

RUN go build -ldflags "-X main.version=v1.2.3" -o poke-cli .
RUN go build -ldflags "-X main.version=v1.2.4" -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.2.3?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.2.4?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.2.3 <command> [subcommand] flag]
docker run --rm -it digitalghostdev/poke-cli:v1.2.4 <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.2.3 -c "cd /app && exec sh"
docker run --rm -it --name poke-cli --entrypoint /bin/sh digitalghostdev/poke-cli:v1.2.4 -c "cd /app && exec sh"
# placed into the /app directory, run the program with './poke-cli'
# example: ./poke-cli ability swift-swim
```
Expand Down
45 changes: 27 additions & 18 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
import (
"flag"
"fmt"
"github.com/digitalghost-dev/poke-cli/cmd"
"github.com/digitalghost-dev/poke-cli/cmd/ability"
"github.com/digitalghost-dev/poke-cli/cmd/move"
"github.com/digitalghost-dev/poke-cli/cmd/natures"
"github.com/digitalghost-dev/poke-cli/cmd/pokemon"
"github.com/digitalghost-dev/poke-cli/cmd/search"
"github.com/digitalghost-dev/poke-cli/cmd/types"
"github.com/digitalghost-dev/poke-cli/cmd/utils"
"github.com/digitalghost-dev/poke-cli/flags"
"github.com/digitalghost-dev/poke-cli/styling"
"os"
"runtime/debug"
"strings"
)

var version = "(devel)"
Expand All @@ -38,13 +40,9 @@
}
}

func printWrapper(f func() string) func() {
return func() {
fmt.Println(f())
}
}

func runCLI(args []string) int {
var output strings.Builder

mainFlagSet := flag.NewFlagSet("poke-cli", flag.ContinueOnError)

// -l, --latest flag retrieves the latest Docker image and GitHub release versions available
Expand Down Expand Up @@ -97,27 +95,34 @@
}

commands := map[string]func(){
"ability": printWrapper(ability.AbilityCommand),
"move": printWrapper(move.MoveCommand),
"natures": printWrapper(natures.NaturesCommand),
"pokemon": cmd.PokemonCommand,
"types": printWrapper(types.TypesCommand),
"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),

Check warning on line 102 in cli.go

View check run for this annotation

Codecov / codecov/patch

cli.go#L98-L102

Added lines #L98 - L102 were not covered by tests
"search": search.SearchCommand,
}

if len(os.Args) < 2 {
cmdArg := ""
if len(os.Args) >= 2 {
cmdArg = os.Args[1]
}
cmdFunc, exists := commands[cmdArg]

switch {
case len(os.Args) < 2:

Check warning on line 113 in cli.go

View check run for this annotation

Codecov / codecov/patch

cli.go#L106-L113

Added lines #L106 - L113 were not covered by tests
mainFlagSet.Usage()
return 1
} else if *latestFlag || *shortLatestFlag {
case *latestFlag || *shortLatestFlag:

Check warning on line 116 in cli.go

View check run for this annotation

Codecov / codecov/patch

cli.go#L116

Added line #L116 was not covered by tests
flags.LatestFlag()
return 0
} else if *currentVersionFlag || *shortCurrentVersionFlag {
case *currentVersionFlag || *shortCurrentVersionFlag:

Check warning on line 119 in cli.go

View check run for this annotation

Codecov / codecov/patch

cli.go#L119

Added line #L119 was not covered by tests
currentVersion()
return 0
} else if cmdFunc, exists := commands[os.Args[1]]; exists {
case exists:

Check warning on line 122 in cli.go

View check run for this annotation

Codecov / codecov/patch

cli.go#L122

Added line #L122 was not covered by tests
cmdFunc()
return 0
} else {
default:

Check warning on line 125 in cli.go

View check run for this annotation

Codecov / codecov/patch

cli.go#L125

Added line #L125 was not covered by tests
command := os.Args[1]
errMessage := styling.ErrorBorder.Render(
styling.ErrorColor.Render("Error!"),
Expand All @@ -131,7 +136,11 @@
fmt.Sprintf("\n\t%-15s %s", "types", "Get details about a typing"),
fmt.Sprintf("\n\nAlso run %s for more info!", styling.StyleBold.Render("poke-cli -h")),
)
fmt.Printf("%s\n", errMessage)
output.WriteString(errMessage)

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

Check warning on line 143 in cli.go

View check run for this annotation

Codecov / codecov/patch

cli.go#L139-L143

Added lines #L139 - L143 were not covered by tests
return 1
}
}
Expand Down
120 changes: 16 additions & 104 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package main

import (
"bytes"
"github.com/digitalghost-dev/poke-cli/cmd/utils"
"github.com/digitalghost-dev/poke-cli/styling"
"github.com/stretchr/testify/assert"
"os"
"regexp"
"testing"
Expand Down Expand Up @@ -93,119 +96,28 @@ func TestRunCLI(t *testing.T) {
expectedCode int
}{
{
name: "No Arguments",
args: []string{},
expectedOutput: "╭──────────────────────────────────────────────────────────╮\n" +
"│Welcome! This tool displays data related to Pokémon! │\n" +
"│ │\n" +
"│ USAGE: │\n" +
"│ poke-cli [flag] │\n" +
"│ poke-cli <command> [flag] │\n" +
"│ poke-cli <command> <subcommand> [flag] │\n" +
"│ │\n" +
"│ FLAGS: │\n" +
"│ -h, --help Shows the help menu │\n" +
"│ -l, --latest Prints the latest version available │\n" +
"│ -v, --version Prints the current version │\n" +
"│ │\n" +
"│ COMMANDS: │\n" +
"│ ability Get details about an ability │\n" +
"│ move Get details about a move │\n" +
"│ natures Get details about all natures │\n" +
"│ pokemon Get details about a Pokémon │\n" +
"│ search Search for a resource │\n" +
"│ types Get details about a typing │\n" +
"│ │\n" +
"│ hint: when calling a resource with a space, use a hyphen │\n" +
"│ example: poke-cli ability strong-jaw │\n" +
"│ example: poke-cli pokemon flutter-mane │\n" +
"╰──────────────────────────────────────────────────────────╯",
expectedCode: 0,
},
{
name: "Help Flag Short",
args: []string{"-h"},
expectedOutput: "╭──────────────────────────────────────────────────────────╮\n" +
"│Welcome! This tool displays data related to Pokémon! │\n" +
"│ │\n" +
"│ USAGE: │\n" +
"│ poke-cli [flag] │\n" +
"│ poke-cli <command> [flag] │\n" +
"│ poke-cli <command> <subcommand> [flag] │\n" +
"│ │\n" +
"│ FLAGS: │\n" +
"│ -h, --help Shows the help menu │\n" +
"│ -l, --latest Prints the latest version available │\n" +
"│ -v, --version Prints the current version │\n" +
"│ │\n" +
"│ COMMANDS: │\n" +
"│ ability Get details about an ability │\n" +
"│ move Get details about a move │\n" +
"│ natures Get details about all natures │\n" +
"│ pokemon Get details about a Pokémon │\n" +
"│ search Search for a resource │\n" +
"│ types Get details about a typing │\n" +
"│ │\n" +
"│ hint: when calling a resource with a space, use a hyphen │\n" +
"│ example: poke-cli ability strong-jaw │\n" +
"│ example: poke-cli pokemon flutter-mane │\n" +
"╰──────────────────────────────────────────────────────────╯",
expectedCode: 0,
},
{
name: "Help Flag Long",
args: []string{"--help"},
expectedOutput: "╭──────────────────────────────────────────────────────────╮\n" +
"│Welcome! This tool displays data related to Pokémon! │\n" +
"│ │\n" +
"│ USAGE: │\n" +
"│ poke-cli [flag] │\n" +
"│ poke-cli <command> [flag] │\n" +
"│ poke-cli <command> <subcommand> [flag] │\n" +
"│ │\n" +
"│ FLAGS: │\n" +
"│ -h, --help Shows the help menu │\n" +
"│ -l, --latest Prints the latest version available │\n" +
"│ -v, --version Prints the current version │\n" +
"│ │\n" +
"│ COMMANDS: │\n" +
"│ ability Get details about an ability │\n" +
"│ move Get details about a move │\n" +
"│ natures Get details about all natures │\n" +
"│ pokemon Get details about a Pokémon │\n" +
"│ search Search for a resource │\n" +
"│ types Get details about a typing │\n" +
"│ │\n" +
"│ hint: when calling a resource with a space, use a hyphen │\n" +
"│ example: poke-cli ability strong-jaw │\n" +
"│ example: poke-cli pokemon flutter-mane │\n" +
"╰──────────────────────────────────────────────────────────╯",

expectedCode: 0,
},
{
name: "Invalid Command",
args: []string{"invalid"},
expectedOutput: "Error!",
expectedCode: 1,
name: "Test CLI help flag",
args: []string{"--help"},
expectedOutput: utils.LoadGolden(t, "cli_help.golden"),
expectedCode: 0,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exit = func(code int) {}
originalArgs := os.Args
os.Args = append([]string{"poke-cli"}, tt.args...)
defer func() { os.Args = originalArgs }()

var exitCode int
output := captureOutput(func() {
code := runCLI(tt.args)
if code != tt.expectedCode {
t.Errorf("Expected exit code %d, got %d", tt.expectedCode, code)
}
exitCode = runCLI(tt.args)
})

output = stripANSI(output)
cleanOutput := styling.StripANSI(output)

if !bytes.Contains([]byte(output), []byte(tt.expectedOutput)) {
t.Errorf("Expected output to contain %q, got %q", tt.expectedOutput, output)
}
assert.Equal(t, tt.expectedOutput, cleanOutput, "Output should match expected")
assert.Equal(t, tt.expectedCode, exitCode, "Exit code should match expected")
})
}
}
Expand Down
16 changes: 7 additions & 9 deletions cmd/ability/ability.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"strings"
)

func AbilityCommand() string {
func AbilityCommand() (string, error) {
var output strings.Builder

flag.Usage = func() {
Expand All @@ -38,12 +38,12 @@

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

if err := utils.ValidateAbilityArgs(args); err != nil {
output.WriteString(err.Error())
return output.String()
return output.String(), err

Check warning on line 46 in cmd/ability/ability.go

View check run for this annotation

Codecov / codecov/patch

cmd/ability/ability.go#L46

Added line #L46 was not covered by tests
}

endpoint := strings.ToLower(args[1])
Expand All @@ -55,7 +55,7 @@
if os.Getenv("GO_TESTING") != "1" {
os.Exit(1)
}
return output.String()
return output.String(), err

Check warning on line 58 in cmd/ability/ability.go

View check run for this annotation

Codecov / codecov/patch

cmd/ability/ability.go#L58

Added line #L58 was not covered by tests
}

abilitiesStruct, abilityName, err := connections.AbilityApiCall(endpoint, abilityName, connections.APIURL)
Expand All @@ -64,7 +64,7 @@
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return err.Error()
return err.Error(), nil
}

// Extract English short_effect
Expand Down Expand Up @@ -108,11 +108,9 @@
if *pokemonFlag || *shortPokemonFlag {
if err := flags.PokemonAbilitiesFlag(&output, endpoint, abilityName); err != nil {
output.WriteString(fmt.Sprintf("error parsing flags: %v\n", err))
if os.Getenv("GO_TESTING") != "1" {
os.Exit(1)
}
return "", fmt.Errorf("error parsing flags: %w", err)

Check warning on line 111 in cmd/ability/ability.go

View check run for this annotation

Codecov / codecov/patch

cmd/ability/ability.go#L111

Added line #L111 was not covered by tests
}
}

return output.String()
return output.String(), nil
}
12 changes: 11 additions & 1 deletion cmd/ability/ability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func TestAbilityCommand(t *testing.T) {
args: []string{"ability", "--help"},
expectedOutput: utils.LoadGolden(t, "ability_help.golden"),
},
{
name: "Ability help flag",
args: []string{"ability", "-h"},
expectedOutput: utils.LoadGolden(t, "ability_help.golden"),
},
{
name: "Ability command: clear-body",
args: []string{"ability", "clear-body"},
Expand All @@ -43,6 +48,11 @@ func TestAbilityCommand(t *testing.T) {
expectedOutput: utils.LoadGolden(t, "ability_misspelled.golden"),
wantError: true,
},
{
name: "Ability command: --pokemon flag",
args: []string{"ability", "anger-point", "--pokemon"},
expectedOutput: utils.LoadGolden(t, "ability_flag_pokemon.golden"),
},
}

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

output := AbilityCommand()
output, _ := AbilityCommand()
cleanOutput := styling.StripANSI(output)

assert.Equal(t, tt.expectedOutput, cleanOutput, "Output should match expected")
Expand Down
Loading