-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_map.go
More file actions
65 lines (53 loc) · 1.3 KB
/
Copy pathcommand_map.go
File metadata and controls
65 lines (53 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"errors"
"fmt"
"strconv"
"strings"
)
func getIDFromURL(url string) (int, error) {
segments := strings.Split(url, "/")
idStr := segments[len(segments)-2]
targetID, err := strconv.Atoi(idStr)
if err != nil {
return 0, fmt.Errorf("error converting ID to integer: %v", err)
}
return targetID, nil
}
func mapForward(cfg *config, args ...string) error {
resp, err := cfg.pokeapiClient.ListLocationAreas(cfg.nextLocationAreaURL)
if err != nil {
return err
}
fmt.Println("Available locations:")
for _, location := range resp.Results {
id, err := getIDFromURL(location.URL)
if err != nil {
return err
}
fmt.Printf("%v) %v\n", id, location.Name)
}
cfg.nextLocationAreaURL = resp.Next
cfg.prevLocationAreaULR = resp.Previous
return nil
}
func mapBackward(cfg *config, args ...string) error {
if cfg.prevLocationAreaULR == nil {
return errors.New("you're on first page")
}
resp, err := cfg.pokeapiClient.ListLocationAreas(cfg.prevLocationAreaULR)
if err != nil {
return err
}
fmt.Println("Available locations:")
for _, location := range resp.Results {
id, err := getIDFromURL(location.URL)
if err != nil {
return err
}
fmt.Printf("%v) %v\n", id, location.Name)
}
cfg.nextLocationAreaURL = resp.Next
cfg.prevLocationAreaULR = resp.Previous
return nil
}