-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
84 lines (67 loc) · 1.7 KB
/
cli.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
package main
import (
"flag"
"log"
"os"
"github.com/njason/shouldwater"
"gopkg.in/yaml.v3"
)
type Config struct {
TomorrowioApiKey string `yaml:"tomorrowioApiKey"`
WeatherApiKey string `yaml:"weatherApiKey"`
MailChimp struct {
ApiKey string `yaml:"apiKey"`
TemplateId uint `yaml:"templateId"`
ListId string `yaml:"listId"`
}
Lat float64 `yaml:"lat"`
Lng float64 `yaml:"lng"`
}
const FiveGallonBucket = 18.93 // in liters
func main() {
var testMode = flag.Bool("test", false, "Will not send notifications")
flag.Parse()
config, err := loadConfig()
if err != nil {
log.Fatalln(err)
}
historicalRecords, err := getHistoricalRecords(config.WeatherApiKey, config.Lat, config.Lng)
if err != nil {
panic(err)
}
forecastRecords, err := getForecastRecords(config.TomorrowioApiKey, config.Lat, config.Lng)
if err != nil {
panic(err)
}
amountToWater, err := shouldwater.ShouldWater(historicalRecords, forecastRecords)
if err != nil {
log.Fatalln(err)
}
// less than a quarter of a bucket is not worth watering
if amountToWater >= FiveGallonBucket/4 {
log.Printf("Should water %f buckets", amountToWater/FiveGallonBucket)
if !*testMode {
log.Printf("Sending out watering prompt via MailChimp")
err = createAndSendCampaign(config.MailChimp.ApiKey, config.MailChimp.TemplateId, config.MailChimp.ListId)
if err != nil {
panic(err)
}
}
} else {
log.Println("Should not water")
}
}
func loadConfig() (Config, error) {
f, err := os.Open("config.yaml")
if err != nil {
return Config{}, err
}
defer f.Close()
var config Config
decoder := yaml.NewDecoder(f)
err = decoder.Decode(&config)
if err != nil {
return Config{}, err
}
return config, nil
}