-
Notifications
You must be signed in to change notification settings - Fork 0
/
sweater.go
137 lines (111 loc) · 3.07 KB
/
sweater.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
import "github.com/TwiN/go-color"
type WeatherResponse struct {
Main struct {
Temp float64
}
}
type Config struct {
APIKey string `json:"api_key"`
Town string `json:"town"`
Country string `json:"country"`
}
const baseURL = "https://api.openweathermap.org"
const configFileName = "config.json"
func main() {
apiKeyPointer := flag.String("apikey", "", "API Key for OpenWeatherMap")
townPointer := flag.String("town", "", "Town for the weather forecast")
countryPointer := flag.String("country", "", "Country for the weather forecast")
flag.Parse()
config := readConfig()
if *apiKeyPointer != "" {
config.APIKey = *apiKeyPointer
}
if *townPointer != "" {
config.Town = *townPointer
}
if *countryPointer != "" {
config.Country = *countryPointer
}
saveConfig(config)
if config.APIKey == "" {
log.Fatal("Please provide an Open Weather Map API key using the -APIKey flag")
}
if config.Town == "" {
log.Fatal("Please provide a town using the -town flag")
}
if config.Country == "" {
log.Fatal("Please provide a country using the -country flag")
}
URL := fmt.Sprintf("%s/data/2.5/weather?q=%s,%s&appid=%s&units=metric", baseURL, config.Town, config.Country, config.APIKey)
response, err := http.Get(URL)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
if response.StatusCode != http.StatusOK {
log.Fatalf("Unexpected status code : %d", response.StatusCode)
}
var weatherResponse WeatherResponse
err = json.Unmarshal(body, &weatherResponse)
if err != nil {
log.Fatal(err)
}
config.Town = strings.ToUpper(config.Town)
roundedTemp := int(weatherResponse.Main.Temp)
location := color.With(color.Cyan, "LOCATION:")
temperature := color.With(color.Red, "TEMPERATURE:")
wear := color.With(color.Blue, "WEAR:")
fmt.Println("-------------------------------------")
fmt.Printf("%s %s, %s \n", location, config.Town, config.Country)
fmt.Printf("%s %d DEGREES CELSIUS \n", temperature, roundedTemp)
if weatherResponse.Main.Temp < 8 {
fmt.Println(wear, "A WARM COAT")
} else if weatherResponse.Main.Temp < 12 {
fmt.Println(wear, "LIGHT JACKET")
} else if weatherResponse.Main.Temp < 16 {
fmt.Println(wear, "SWEATER")
} else {
fmt.Println(wear, "T-SHIRT")
}
fmt.Println("-------------------------------------")
}
func saveConfig(config Config) {
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
log.Fatalf("Error marshalling config: %v", err)
}
err = ioutil.WriteFile(configFileName, data, 0644)
if err != nil {
log.Fatalf("Error saving config to %s: %v", configFileName, err)
}
}
func readConfig() Config {
data, err := ioutil.ReadFile(configFileName)
if err != nil {
if !os.IsNotExist(err) {
log.Fatalf("Error reading config from %s: %v", configFileName, err)
}
return Config{}
}
var config Config
err = json.Unmarshal(data, &config)
if err != nil {
log.Fatalf("Error unmarshalling config: %v", err)
}
return config
}