-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountries.go
114 lines (98 loc) · 2.79 KB
/
countries.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
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
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"github.com/urfave/cli"
)
const baseURL = "https://restcountries.eu/rest/v2/name/"
const endOfURL = "?fullText=true"
type Country struct {
Capital string `json:"capital"`
Subregion string `json:"subregion"`
Population int `json:"population"`
Currency []CurrencyJson `json:"currencies"`
Language []LanguageJson `json:"languages"`
}
type CurrencyJson struct {
CurrencyName string `json:"name"`
CurrencySymbol string `json:"symbol"`
}
type LanguageJson struct {
LanguageName string `json:"name"`
}
func main() {
var fullURL string
var countryName string
app := cli.NewApp()
app.Name = "countries of the world CLI"
app.Version = "0.1.0"
app.Usage = "info about a given country"
// To display the country's population or language only in the output
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "population, p",
Usage: "Display the country's population only",
},
cli.BoolFlag{
Name: "language, l",
Usage: "Display the country's language only",
},
}
app.Action = func(c *cli.Context) error {
if c.NArg() == 0 {
return cli.NewExitError("Needs a country", 0)
} else if c.NArg() > 1 {
countryName = strings.Join(c.Args(), "%20")
fullURL = fmt.Sprintf("%s%s%s", baseURL, countryName, endOfURL)
} else {
countryName = c.Args()[0]
fullURL = fmt.Sprintf("%s%s%s", baseURL, countryName, endOfURL)
}
// Create a client to set a User-Agent
client := &http.Client{}
req, err := http.NewRequest("GET", fullURL, nil)
if err != nil {
fmt.Println("There was an issue completing this request")
log.Fatal(err)
}
req.Header.Set("User-Agent", "Terminal/1.0")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
// Mispelled country name or response code != 200
if resp.StatusCode != 200 {
return cli.NewExitError("Couldn't find any info", 0)
}
body, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
log.Fatal(readErr)
}
// The API returns an array of size 1
countries := make([]Country, 0)
json.Unmarshal(body, &countries)
// Popluation or Language flag has been enabled
if c.Bool("population") {
fmt.Println("Population:", countries[0].Population)
} else if c.Bool("language") {
fmt.Println("Language:", countries[0].Language[0].LanguageName)
} else {
fmt.Println("Capital:", countries[0].Capital)
fmt.Println("Region:", countries[0].Subregion)
fmt.Println("Population:", countries[0].Population)
fmt.Println("Currency:", countries[0].Currency[0].CurrencyName, countries[0].Currency[0].CurrencySymbol)
fmt.Println("Language:", countries[0].Language[0].LanguageName)
}
return nil
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}