/
main.go
53 lines (47 loc) · 1.08 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"log"
"os"
"time"
"github.com/brunopma/gokedex-cli/client"
"github.com/brunopma/gokedex-cli/model"
)
func main() {
PokemonID := flag.Int(
"i", 0, "Pokemon ID to fetch, currently there's 807 available pokemon!",
)
PokemonName := flag.String(
"n", "text", "Pokemon ID to fetch",
)
clientTimeout := flag.Int64(
"t", int64(client.DefaultClientTimeout.Seconds()), "Client timeout in seconds",
)
outputType := flag.String(
"o", "text", "Print output in format: text/json",
)
flag.Parse()
pokeapiClient := client.NewPokeapiClient()
pokeapiClient.SetTimeout(time.Duration(*clientTimeout) * time.Second)
var pokemonResp model.Pokemon
var err error
switch {
case *PokemonID > 0:
pokemonResp, err = pokeapiClient.FetchPokemon(*PokemonID)
case *PokemonName != "text":
pokemonResp, err = pokeapiClient.FetchPokemon(*PokemonName)
default:
flag.Usage()
os.Exit(0)
}
if err != nil {
log.Println(err)
os.Exit(0)
}
if *outputType == "json" {
fmt.Println(pokemonResp.JSON())
} else {
fmt.Println(pokemonResp.PrettyString())
}
}