feat: implemented explore command
This commit is contained in:
+31
-4
@@ -15,6 +15,7 @@ type context struct {
|
||||
client *pokeapi.Client
|
||||
prevOffset int
|
||||
nextOffset int
|
||||
args []string
|
||||
}
|
||||
|
||||
func (r *context) register(c cliCommand) {
|
||||
@@ -30,24 +31,30 @@ func NewContext() *context {
|
||||
callback: commandExit,
|
||||
}
|
||||
|
||||
var helpCommand = cliCommand{
|
||||
helpCommand := cliCommand{
|
||||
name: "help",
|
||||
description: "Displays a help message",
|
||||
callback: commandHelp,
|
||||
}
|
||||
|
||||
var mapCommand = cliCommand{
|
||||
mapCommand := cliCommand{
|
||||
name: "map",
|
||||
description: "Displays the names of the next 20 location areas",
|
||||
callback: commandMap,
|
||||
}
|
||||
|
||||
var mapBackCommand = cliCommand{
|
||||
mapBackCommand := cliCommand{
|
||||
name: "mapb",
|
||||
description: "Displays the names of the previous 20 location areas",
|
||||
callback: commandMapBack,
|
||||
}
|
||||
|
||||
exploreCommand := cliCommand{
|
||||
name: "explore",
|
||||
description: "Displays a list of pokemon in the area",
|
||||
callback: commandExplore,
|
||||
}
|
||||
|
||||
c := &context{
|
||||
commands: map[string]cliCommand{},
|
||||
client: pokeapi.NewClient(pokecache.NewCache(5 * time.Second)),
|
||||
@@ -59,6 +66,7 @@ func NewContext() *context {
|
||||
c.register(helpCommand)
|
||||
c.register(mapCommand)
|
||||
c.register(mapBackCommand)
|
||||
c.register(exploreCommand)
|
||||
|
||||
return c
|
||||
}
|
||||
@@ -99,7 +107,7 @@ func commandHelp(c *context) error {
|
||||
}
|
||||
|
||||
func getMaps(c *context, offset int) error {
|
||||
page, err := c.client.Locations.Get(offset)
|
||||
page, err := c.client.Locations.List(offset)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Unable to get locations")
|
||||
@@ -138,3 +146,22 @@ func commandMapBack(c *context) error {
|
||||
|
||||
return getMaps(c, c.prevOffset)
|
||||
}
|
||||
|
||||
func commandExplore(c *context) error {
|
||||
if len(c.args) < 1 {
|
||||
fmt.Println("Please provide area name. i.e. explore <area_name>")
|
||||
return nil
|
||||
}
|
||||
|
||||
area, err := c.client.Locations.Get(c.args[0])
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, encounter := range area.PokemonEncounters {
|
||||
fmt.Println(encounter.Pokemon.Name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user