This commit is contained in:
40
main.go
40
main.go
@@ -19,7 +19,7 @@ type config struct {
|
||||
type cliCommand struct {
|
||||
name string
|
||||
description string
|
||||
callback func(*config) error
|
||||
callback func(*config, ...string) error
|
||||
}
|
||||
|
||||
var commands map[string]cliCommand
|
||||
@@ -37,6 +37,11 @@ func main() {
|
||||
description: "Exit the Pokedex",
|
||||
callback: commandExit,
|
||||
},
|
||||
"explore": {
|
||||
name: "explore <location_name>",
|
||||
description: "Explore a location",
|
||||
callback: commandExplore,
|
||||
},
|
||||
"help": {
|
||||
name: "help",
|
||||
description: "Display a help message",
|
||||
@@ -61,11 +66,14 @@ func main() {
|
||||
word := input.Text()
|
||||
cleanedWord := cleanInput(word)
|
||||
command := cleanedWord[0]
|
||||
|
||||
var args []string
|
||||
if len(cleanedWord) > 1 {
|
||||
args = cleanedWord[1:]
|
||||
}
|
||||
//check if command is in commands map
|
||||
cmd, ok := commands[command]
|
||||
if ok {
|
||||
err := cmd.callback(cfg)
|
||||
err := cmd.callback(cfg, args...)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
@@ -77,7 +85,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func commandExit(*config) error {
|
||||
func commandExit(*config, ...string) error {
|
||||
fmt.Println("Closing the Pokedex... Goodbye!")
|
||||
os.Exit(0)
|
||||
return nil
|
||||
@@ -89,7 +97,7 @@ func cleanInput(text string) []string {
|
||||
return words
|
||||
}
|
||||
|
||||
func printHelpMessage(*config) error {
|
||||
func printHelpMessage(*config, ...string) error {
|
||||
fmt.Print("Welcome to the Pokedex!\nUsage:\n\n")
|
||||
for _, v := range commands {
|
||||
fmt.Printf("%s: %s\n", v.name, v.description)
|
||||
@@ -97,7 +105,7 @@ func printHelpMessage(*config) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func commandMapf(cfg *config) error {
|
||||
func commandMapf(cfg *config, _ ...string) error {
|
||||
locationsResp, err := cfg.pokeapiClient.ListLocations(cfg.nextLocationsURL)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -112,7 +120,7 @@ func commandMapf(cfg *config) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func commandMapb(cfg *config) error {
|
||||
func commandMapb(cfg *config, _ ...string) error {
|
||||
if cfg.prevLocationsURL == nil {
|
||||
return errors.New("you're on the first page")
|
||||
}
|
||||
@@ -130,3 +138,21 @@ func commandMapb(cfg *config) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func commandExplore(cfg *config, args ...string) error {
|
||||
if len(args) != 1 {
|
||||
return errors.New("you must provide a location name")
|
||||
}
|
||||
|
||||
name := args[0]
|
||||
location, err := cfg.pokeapiClient.GetLocation(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("Exploring %s...\n", location.Name)
|
||||
fmt.Println("Found Pokemon: ")
|
||||
for _, enc := range location.PokemonEncounters {
|
||||
fmt.Printf(" - %s\n", enc.Pokemon.Name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user