Skip to content

Commit

Permalink
Merge pull request #33 from digitalghost-dev/0.4.2
Browse files Browse the repository at this point in the history
0.4.2
  • Loading branch information
digitalghost-dev authored Sep 11, 2024
2 parents 64a3523 + 746a5c3 commit e4b1e62
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ git clone https://github.com/digitalghost-dev/poke-cli.git
### Docker
Use a Docker Image instead:
```bash
docker run --rm -it digitalghostdev/poke-cli:v0.4.1 [command] [subcommand] [flag]
docker run --rm -it digitalghostdev/poke-cli:v0.4.2 [command] [subcommand] [flag]
```

## Usage
Expand Down
40 changes: 31 additions & 9 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ func TestCLI(t *testing.T) {
expectedExit int
}{
{
args: []string{"pokemon"},
expectedOutput: "Please declare a Pokémon's name after [pokemon] command\nRun 'poke-cli --help' for more details\nerror: insufficient arguments\n",
expectedExit: 1,
args: []string{"pokemon"},
expectedOutput: "╭────────────────────────────────────────────────────────────╮\n" +
"│Error! │\n" +
"│Please declare a Pokémon's name after the [pokemon] command │\n" +
"│Run 'poke-cli pokemon -h' for more details │\n" +
"│error: insufficient arguments │\n" +
"╰────────────────────────────────────────────────────────────╯\n",
expectedExit: 1,
},
{
args: []string{"pokemon", "bulbasaur"},
Expand All @@ -33,14 +38,20 @@ func TestCLI(t *testing.T) {
expectedExit: 0,
},
{
args: []string{"pokemon", "chimchar", "types"},
expectedOutput: "Error: Invalid argument 'types'. Only flags are allowed after declaring a Pokémon's name\n",
expectedExit: 1,
args: []string{"pokemon", "chimchar", "types"},
expectedOutput: "╭─────────────────────────────────────────────────────────────────────────────────╮\n" +
"│Error! │\n" +
"│Invalid argument 'types'. Only flags are allowed after declaring a Pokémon's name│\n" +
"╰─────────────────────────────────────────────────────────────────────────────────╯\n",
expectedExit: 1,
},
{
args: []string{"pokemon", "flutter-mane", "types"},
expectedOutput: "Error: Invalid argument 'types'. Only flags are allowed after declaring a Pokémon's name\n",
expectedExit: 1,
args: []string{"pokemon", "flutter-mane", "types"},
expectedOutput: "╭─────────────────────────────────────────────────────────────────────────────────╮\n" +
"│Error! │\n" +
"│Invalid argument 'types'. Only flags are allowed after declaring a Pokémon's name│\n" +
"╰─────────────────────────────────────────────────────────────────────────────────╯\n",
expectedExit: 1,
},
{
args: []string{"pokemon", "AmPhaROs", "--types", "--abilities"},
Expand All @@ -52,6 +63,17 @@ func TestCLI(t *testing.T) {
expectedOutput: "Your selected Pokémon: Cloyster\nNational Pokédex #: 91\n──────\nTyping\nType 1: water\nType 2: ice\n─────────\nAbilities\nAbility 1: shell-armor\nAbility 2: skill-link\nHidden Ability: overcoat\n",
expectedExit: 0,
},
{
args: []string{"pokemon", "gyarados", "--help"},
expectedOutput: "╭──────────────────────────────────────────────────────────────╮\n" +
"│poke-cli pokemon <pokemon-name> [flags] │\n" +
"│ │\n" +
"│FLAGS: │\n" +
"│ -a, --abilities Prints out the Pokémon's abilities. │\n" +
"│ -t, --types Prints out the Pokémon's typing. │\n" +
"╰──────────────────────────────────────────────────────────────╯\n",
expectedExit: 0,
},
}

for _, test := range tests {
Expand Down
73 changes: 39 additions & 34 deletions cmd/pokemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,63 @@ import (
"strings"
)

var (
errorColor = lipgloss.NewStyle().Foreground(lipgloss.Color("#F2055C"))
errorBorder = lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("#F2055C"))
helpBorder = lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("#FFCC00"))
styleBold = lipgloss.NewStyle().Bold(true)
styleItalic = lipgloss.NewStyle().Italic(true)
)

// ValidateArgs validates the command line arguments
func ValidateArgs(args []string, errorColor lipgloss.Style) error {
func ValidateArgs(args []string) error {

if len(args) > 5 {
return fmt.Errorf("error: too many arguments")
return fmt.Errorf(errorBorder.Render(errorColor.Render("Error!"), "\nToo many arguments"))
}

if len(args) < 3 {
fmt.Println(errorColor.Render("Please declare a Pokémon's name after [pokemon] command"))
fmt.Println(errorColor.Render("Run 'poke-cli --help' for more details"))
return fmt.Errorf("error: insufficient arguments")
return fmt.Errorf(errorBorder.Render(errorColor.Render("Error!"), "\nPlease declare a Pokémon's name after the [pokemon] command", "\nRun 'poke-cli pokemon -h' for more details", "\nerror: insufficient arguments"))
}

if len(args) > 3 {
for _, arg := range args[3:] {
if arg[0] != '-' {
errorMsg := fmt.Sprintf("Error: Invalid argument '%s'. Only flags are allowed after declaring a Pokémon's name", arg)
return fmt.Errorf(errorColor.Render(strings.TrimSpace(errorMsg)))
errorTitle := errorColor.Render("Error!")
errorString := fmt.Sprintf("\nInvalid argument '%s'. Only flags are allowed after declaring a Pokémon's name", arg)
formattedString := errorTitle + errorString
return fmt.Errorf(errorBorder.Render(formattedString))
}
}
}

if args[2] == "-h" || args[2] == "--help" {
flag.Usage()
os.Exit(0)
}

return nil
}

// PokemonCommand processes the Pokémon command
func PokemonCommand() {
const red = lipgloss.Color("#F2055C")
var errorColor = lipgloss.NewStyle().Foreground(red)

var styleBold = lipgloss.NewStyle().Bold(true)
var styleItalic = lipgloss.NewStyle().Italic(true)

flag.Usage = func() {
// Usage section
fmt.Println(styleBold.Render("\nUSAGE:"))
fmt.Println("\t", "poke-cli", styleBold.Render("pokemon"), "[flag]")
fmt.Println("\t", "Get details about a specific Pokémon")
fmt.Println("\t", "----------")
fmt.Println("\t", styleItalic.Render("Examples:"), "\t", "poke-cli pokemon bulbasaur")
fmt.Println("\t\t\t", "poke-cli pokemon flutter-mane --types")
fmt.Println("\t\t\t", "poke-cli pokemon excadrill -t -a")

// Flags section
fmt.Println(styleBold.Render("\nFLAGS:"))
fmt.Println("\t", "-a, --abilities", "\t", "Prints out the Pokémon's abilities.")
fmt.Println("\t", "-t, --types", "\t\t", "Prints out the Pokémon's typing.")
fmt.Print("\n")
fmt.Println(
helpBorder.Render(styleBold.Render("USAGE:"), "\n\t", "poke-cli", styleBold.Render("pokemon"), "<pokemon-name>", "[flag]",
"\n\t", "Get details about a specific Pokémon",
"\n\t", "----------",
"\n\t", styleItalic.Render("Examples:"), "\t", "poke-cli pokemon bulbasaur",
"\n\t\t\t", "poke-cli pokemon flutter-mane --types",
"\n\t\t\t", "poke-cli pokemon excadrill -t -a",
"\n",
styleBold.Render("\nFLAGS:"), "\n\t", "-a, --abilities", "\t", "Prints out the Pokémon's abilities.",
"\n\t", "-t, --types", "\t\t", "Prints out the Pokémon's typing."),
)
}

flag.Parse()
Expand All @@ -68,22 +77,18 @@ func PokemonCommand() {

args := os.Args

err := ValidateArgs(args, errorColor)
err := ValidateArgs(args)
if err != nil {
fmt.Println(errorColor.Render(err.Error()))
fmt.Println(err.Error())
os.Exit(1)
}

if args[2] == "-h" || args[2] == "--help" {
flag.Usage()
os.Exit(0)
}

endpoint := os.Args[1]
endpoint := strings.ToLower(args[1])
pokemonName := strings.ToLower(args[2])

if err := pokeFlags.Parse(args[3:]); err != nil {
fmt.Printf("error parsing flags: %v\n", err)
pokeFlags.Usage()
os.Exit(1)
}

Expand Down
11 changes: 3 additions & 8 deletions cmd/pokemon_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package cmd

import (
"github.com/charmbracelet/lipgloss"
"github.com/stretchr/testify/assert"
"testing"
)

const red = lipgloss.Color("#F2055C")

var errorColor = lipgloss.NewStyle().Foreground(red)

func TestValidateArgs_ValidInput(t *testing.T) {
validInputs := [][]string{
{"poke-cli", "pokemon", "pikachu"},
Expand All @@ -20,7 +15,7 @@ func TestValidateArgs_ValidInput(t *testing.T) {
}

for _, input := range validInputs {
err := ValidateArgs(input, errorColor)
err := ValidateArgs(input)
assert.NoError(t, err, "Expected no error for valid input")
}
}
Expand All @@ -36,7 +31,7 @@ func TestValidateArgs_InvalidFlag(t *testing.T) {
}

for i, input := range invalidInputs {
err := ValidateArgs(input, errorColor)
err := ValidateArgs(input)
assert.Error(t, err, "Expected error for invalid flag")
assert.NotEmpty(t, expectedErrors[i], err.Error())
}
Expand All @@ -49,7 +44,7 @@ func TestValidateArgs_TooManyArgs(t *testing.T) {
expectedError := "error: too many arguments\n"

for _, input := range invalidInput {
err := ValidateArgs(input, errorColor)
err := ValidateArgs(input)
assert.Error(t, err, "Expected error for too many arguments")
assert.NotEqual(t, expectedError, err.Error())
}
Expand Down
15 changes: 15 additions & 0 deletions flags/pokemonflagset.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import (
"github.com/digitalghost-dev/poke-cli/connections"
)

var (
helpBorder = lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("#FFCC00"))
styleBold = lipgloss.NewStyle().Bold(true)
)

func SetupPokemonFlagSet() (*flag.FlagSet, *bool, *bool, *bool, *bool) {
pokeFlags := flag.NewFlagSet("pokeFlags", flag.ExitOnError)

Expand All @@ -18,6 +25,14 @@ func SetupPokemonFlagSet() (*flag.FlagSet, *bool, *bool, *bool, *bool) {
abilitiesFlag := pokeFlags.Bool("abilities", false, "Print the declared Pokémon's abilities")
shortAbilitiesFlag := pokeFlags.Bool("a", false, "Print the declared Pokémon's abilities")

pokeFlags.Usage = func() {
fmt.Println(
helpBorder.Render("poke-cli pokemon <pokemon-name> [flags]",
styleBold.Render("\n\nFLAGS:"), "\n\t", "-a, --abilities", "\t", "Prints out the Pokémon's abilities.",
"\n\t", "-t, --types", "\t\t", "Prints out the Pokémon's typing."),
)
}

return pokeFlags, typesFlag, shortTypesFlag, abilitiesFlag, shortAbilitiesFlag
}

Expand Down

0 comments on commit e4b1e62

Please sign in to comment.