Skip to content

Commit

Permalink
Merge pull request #86 from digitalghost-dev/0.9.2
Browse files Browse the repository at this point in the history
0.9.2
  • Loading branch information
digitalghost-dev authored Dec 14, 2024
2 parents 1413337 + db1ec6e commit 7957cfe
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ on:
- '.goreleaser.yaml'
branches:
- main

env:
VERSION_NUMBER: 'v0.9.1'
VERSION_NUMBER: 'v0.9.2'
DOCKERHUB_REGISTRY_NAME: 'digitalghostdev/poke-cli'
AWS_REGION: 'us-west-2'

Expand Down
46 changes: 24 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<img height="250" width="350" src="https://cdn.simpleicons.org/pokemon/FFCC00" 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/v0.9.1?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/v0.9.2?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">
<img src="https://img.shields.io/github/actions/workflow/status/digitalghost-dev/poke-cli/go_test.yml?style=flat-square&logo=go&logoColor=00ADD8&label=Tests&labelColor=EEE&color=00ADD8" alt="tests-label">
<img src="https://img.shields.io/github/go-mod/go-version/digitalghost-dev/poke-cli?style=flat-square&logo=Go&labelColor=EEE&color=00ADD8" alt="go-version">

<img src="https://img.shields.io/github/go-mod/go-version/digitalghost-dev/poke-cli?style=flat-square&logo=Go&labelColor=EEE&color=00ADD8" alt="go-version"/>
<img src="https://img.shields.io/codecov/c/github/digitalghost-dev/poke-cli?token=05GBSAOQIT&style=flat-square&logo=codecov&logoColor=00ADD8&labelColor=EEE&color=00ADD8" alt="codecov"/>
</div>

## Overview
Expand Down Expand Up @@ -68,7 +68,7 @@ _Use a Docker Image_
* Necessary.

```bash
docker run --rm -i -t digitalghostdev/poke-cli:v0.9.1 <command> [subcommand] flag]
docker run --rm -i -t digitalghostdev/poke-cli:v0.9.2 <command> [subcommand] flag]
```

### Go Install
Expand All @@ -83,23 +83,23 @@ _If you have Go already, install the executable yourself_
## Usage
By running `poke-cli [-h | --help]`, it'll display information on how to use the tool.
```
╭──────────────────────────────────────────────────────╮
│Welcome! This tool displays data related to Pokémon! │
│ │
│ USAGE: │
│ poke-cli [flag] │
│ poke-cli <command> [flag] │
│ poke-cli <command> <subcommand> [flag] │
│ │
│ FLAGS: │
│ -h, --help Shows the help menu │
│ -l, --latest Prints the latest available
version of the program
│ │
│ AVAILABLE COMMANDS: │
│ pokemon Get details of a specific Pokémon │
│ types Get details of a specific typing │
╰──────────────────────────────────────────────────────╯
╭────────────────────────────────────────────────────────
│Welcome! This tool displays data related to Pokémon!
│ USAGE:
│ poke-cli [flag]
│ poke-cli <command> [flag]
│ poke-cli <command> <subcommand> [flag]
│ FLAGS:
│ -h, --help Shows the help menu
│ -l, --latest Prints the latest version available
-v, --version Prints the current version
│ AVAILABLE COMMANDS:
│ pokemon Get details of a specific Pokémon
│ types Get details of a specific typing
╰────────────────────────────────────────────────────────
```

---
Expand All @@ -119,4 +119,6 @@ _Not 100% up-to-date, may add or remove some of these choices_
- [ ] `-m | --moves`: display learnable moves.
- [x] `types`: get data about a specific typing.
- [ ] `ability`: get data about a specific ability.
- [ ] `move`: get data about a specific move.
- [ ] `-p | --pokemon`: display Pokémon that learn this ability.
- [ ] `move`: get data about a specific move.
- [ ] `-p | --pokemon`: display Pokémon that learn this move.
2 changes: 1 addition & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func runCLI(args []string) int {
latestFlag := mainFlagSet.Bool("latest", false, "Prints the program's latest Docker image and release versions.")
shortLatestFlag := mainFlagSet.Bool("l", false, "Prints the program's latest Docker image and release versions.")

// -v, --version flag retrives the currently installed version
// -v, --version flag retrieves the currently installed version
currentVersionFlag := mainFlagSet.Bool("version", false, "Prints the current version")
shortCurrentVersionFlag := mainFlagSet.Bool("v", false, "Prints the current version")

Expand Down
100 changes: 99 additions & 1 deletion cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,63 @@ import (
"testing"
)

func TestCurrentVersion(t *testing.T) {
tests := []struct {
name string
version string
expectedOutput string
}{
{
name: "Version set by ldflags",
version: "v1.0.0",
expectedOutput: "Version: v1.0.0\n",
},
{
name: "Version set to (devel)",
version: "(devel)",
expectedOutput: "Version: (devel)\n", // Simplified assumption
},
}

// Save the original version and restore it after tests
originalVersion := version
defer func() { version = originalVersion }()

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Set the version for this test case
version = tt.version

// Redirect stdout to capture the output
r, w, _ := os.Pipe()
oldStdout := os.Stdout
os.Stdout = w

// Call the function
currentVersion()

// Close the writer and restore stdout
err := w.Close()
if err != nil {
t.Fatalf("Failed to close pipe: %v", err)
}
os.Stdout = oldStdout

// Read the output from the pipe
var buf bytes.Buffer
if _, err := buf.ReadFrom(r); err != nil {
t.Fatalf("Failed to read from pipe: %v", err)
}

// Compare the output with the expected result
got := buf.String()
if got != tt.expectedOutput {
t.Errorf("Expected %q, got %q", tt.expectedOutput, got)
}
})
}
}

var ansiRegex = regexp.MustCompile(`\x1b\[[0-9;]*m`)

func captureOutput(f func()) string {
Expand Down Expand Up @@ -110,7 +167,7 @@ func TestRunCLI(t *testing.T) {
{
name: "Latest Flag",
args: []string{"-l"},
expectedOutput: "Latest Docker image version: v0.9.0\nLatest release tag: v0.9.0\n",
expectedOutput: "Latest Docker image version: v0.9.1\nLatest release tag: v0.9.1\n",
expectedCode: 0,
},
}
Expand All @@ -133,3 +190,44 @@ func TestRunCLI(t *testing.T) {
})
}
}

func TestMainFunction(t *testing.T) {
originalExit := exit
defer func() { exit = originalExit }() // Restore original exit after test

tests := []struct {
name string
args []string
expectedOutput string
expectedCode int
}{
{
name: "Run main command",
args: []string{"poke-cli"},
expectedOutput: "Welcome! This tool displays data related to Pokémon!",
expectedCode: 0,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exitCode := 0
exit = func(code int) { exitCode = code }

output := captureOutput(func() {
os.Args = tt.args
main()
})

output = stripANSI(output)

if exitCode != tt.expectedCode {
t.Errorf("Expected exit code %d, got %d", tt.expectedCode, exitCode)
}

if !bytes.Contains([]byte(output), []byte(tt.expectedOutput)) {
t.Errorf("Expected output to contain %q, got %q", tt.expectedOutput, output)
}
})
}
}
2 changes: 1 addition & 1 deletion cmd/pokemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func PokemonCommand() {
}

_, pokemonName, pokemonID, pokemonWeight, pokemonHeight := connections.PokemonApiCall(endpoint, pokemonName, "https://pokeapi.co/api/v2/")
capitalizedString := cases.Title(language.English).String(pokemonName)
capitalizedString := cases.Title(language.English).String(strings.Replace(pokemonName, "-", " ", -1))

// Weight calculation
weightKilograms := float64(pokemonWeight) / 10
Expand Down
39 changes: 34 additions & 5 deletions flags/pokemonflagset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"fmt"
"github.com/charmbracelet/lipgloss"
"github.com/digitalghost-dev/poke-cli/connections"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"strings"
)

Expand Down Expand Up @@ -58,13 +60,38 @@ func AbilitiesFlag(endpoint string, pokemonName string) error {

header("Abilities")

// Anonymous function to format ability names
formatAbilityName := func(name string) string {
exceptions := map[string]bool{
"of": true,
"the": true,
"to": true,
"as": true,
}

name = strings.Replace(name, "-", " ", -1)
words := strings.Split(name, " ")
titleCaser := cases.Title(language.English)

// Process each word
for i, word := range words {
if _, found := exceptions[strings.ToLower(word)]; found && i != 0 {
words[i] = strings.ToLower(word)
} else {
words[i] = titleCaser.String(word)
}
}
return strings.Join(words, " ")
}

for _, pokeAbility := range pokemonStruct.Abilities {
formattedName := formatAbilityName(pokeAbility.Ability.Name)
if pokeAbility.Slot == 1 {
fmt.Printf("Ability %d: %s\n", pokeAbility.Slot, pokeAbility.Ability.Name)
fmt.Printf("Ability %d: %s\n", pokeAbility.Slot, formattedName)
} else if pokeAbility.Slot == 2 {
fmt.Printf("Ability %d: %s\n", pokeAbility.Slot, pokeAbility.Ability.Name)
fmt.Printf("Ability %d: %s\n", pokeAbility.Slot, formattedName)
} else {
fmt.Printf("Hidden Ability: %s\n", pokeAbility.Ability.Name)
fmt.Printf("Hidden Ability: %s\n", formattedName)
}
}

Expand Down Expand Up @@ -181,9 +208,11 @@ func TypesFlag(endpoint string, pokemonName string) error {
colorHex, exists := colorMap[pokeType.Type.Name]
if exists {
color := lipgloss.Color(colorHex)
fmt.Printf("Type %d: %s\n", pokeType.Slot, lipgloss.NewStyle().Bold(true).Foreground(color).Render(pokeType.Type.Name))
style := lipgloss.NewStyle().Bold(true).Foreground(color)
styledName := style.Render(cases.Title(language.English).String(pokeType.Type.Name)) // Apply styling here
fmt.Printf("Type %d: %s\n", pokeType.Slot, styledName) // Interpolate styled text
} else {
fmt.Printf("Type %d: %s\n", pokeType.Slot, pokeType.Type.Name)
fmt.Printf("Type %d: %s\n", pokeType.Slot, cases.Title(language.English).String(pokeType.Type.Name))
}
}

Expand Down
8 changes: 4 additions & 4 deletions flags/pokemonflagset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func TestAbilitiesFlag(t *testing.T) {
// Define the expected output based on the API response
expectedOutput := `─────────
Abilities
Ability 1: overgrow
Hidden Ability: chlorophyll
Ability 1: Overgrow
Hidden Ability: Chlorophyll
`

// Assert the actual output matches the expected output
Expand Down Expand Up @@ -153,8 +153,8 @@ func TestTypesFlag(t *testing.T) {
// Define expected output components
expectedOutput := `──────
Typing
Type 1: grass
Type 2: poison
Type 1: Grass
Type 2: Poison
`

// Assert output contains the expected header and typing information
Expand Down

0 comments on commit 7957cfe

Please sign in to comment.