-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
102 lines (88 loc) · 2.58 KB
/
Copy pathmain.go
File metadata and controls
102 lines (88 loc) · 2.58 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"bufio"
"fmt"
"os"
"time"
"github.com/igor570/pokeGO/internal/cache"
"github.com/igor570/pokeGO/internal/commands"
"github.com/igor570/pokeGO/internal/models"
)
const initialURL = "https://pokeapi.co/api/v2/location-area"
func main() {
config := &commands.Config{
Next: initialURL,
Previous: "",
}
cache := cache.NewCache(time.Second * 5)
pokedex := make(map[string]models.PokemonResponse)
type cliCommand struct {
name string
description string
callback func([]string) error
}
commands := map[string]cliCommand{
"exit": {
name: "exit",
description: "Exits the pokedex",
callback: func(args []string) error { return commands.CommandExit() },
},
"help": {
name: "help",
description: "Shows available commands",
callback: func(args []string) error { return commands.CommandHelp() }, // this is basically how we can do () => commandMap(config) in ts
},
"map": {
name: "map",
description: "Displays 20 locations in Pokemon",
callback: func(args []string) error { return commands.CommandMap(config, cache) },
},
"mapb": {
name: "mapb",
description: "Displays previous 20 locations in Pokemon",
callback: func(args []string) error { return commands.CommandMapBack(config, cache) },
},
"explore": {
name: "explore",
description: "Shows pokemon in a given location: explore <location>",
callback: func(args []string) error { return commands.CommandExplore(cache, args[0]) },
},
"catch": {
name: "catch",
description: "Attempt to catch a pokemon",
callback: func(args []string) error { return commands.CommandCatch(args[0], pokedex) },
},
"inspect": {
name: "inspect",
description: "Inspect the stats of a caught pokemon",
callback: func(args []string) error { return commands.CommandInspect(args[0], pokedex) },
},
"pokedex": {
name: "pokedex",
description: "List all caught Pokemon",
callback: func(args []string) error { return commands.CommandPokedex(pokedex) },
},
}
input := bufio.NewScanner(os.Stdin)
// Create a REPL
for {
fmt.Print("Pokedex > ")
// make sure there is an input, otherwise exit the repl
if !input.Scan() {
break
}
stringInput := input.Text()
formattedInput := CleanInput(stringInput)
// eg: help -minimal
cmd := formattedInput[0]
args := formattedInput[1:]
if foundCommand, exists := commands[cmd]; exists {
err := foundCommand.callback(args)
if err != nil {
fmt.Println(err)
}
} else {
fmt.Println("Unknown command")
}
}
}