-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
188 lines (165 loc) · 4.82 KB
/
cmd.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package weather
import (
"errors"
"fmt"
"strings"
"github.com/rwx-yxu/weather/region"
"github.com/rwx-yxu/weather/site"
Z "github.com/rwxrob/bonzai/z"
"github.com/rwxrob/help"
"github.com/rwxrob/vars"
)
func init() {
Z.Vars.SoftInit()
}
var Cmd = &Z.Cmd{
Name: `weather`,
Summary: `a command that prints out current day routine schedule`,
Version: `v0.4.1`,
Copyright: `Copyright 2023 Yongle Xu`,
License: `Apache-2.0`,
Site: `yonglexu.dev`,
Source: `[email protected]:rwx-yxu/routine.git`,
Issues: `github.com/rwx-yxu/routine/issues`,
Commands: []*Z.Cmd{
nowCmd, sitesCmd,
// standard external branch imports (see rwxrob/{help,conf,vars})
help.Cmd, vars.Cmd,
},
// Add custom BonzaiMark template extensions (or overwrite existing ones).
Description: `
{{cmd .Name}} is a tool that queries the Open weather map API for current weather information.
`,
}
var nowCmd = &Z.Cmd{
Name: `now`,
Summary: `print current weather conditions to standard output (default)`,
Commands: []*Z.Cmd{help.Cmd},
Call: func(x *Z.Cmd, _ ...string) error {
fmt.Println("Hello world")
return nil
},
}
var sitesCmd = &Z.Cmd{
Name: `site`,
Summary: `site commands that will output met office sites`,
Commands: []*Z.Cmd{help.Cmd, siteListCmd, siteFindCmd, siteSetCmd, siteForecastCmd},
}
var siteListCmd = &Z.Cmd{
Name: `list`,
Summary: `prints all met office site locations to standard output (default)`,
Commands: []*Z.Cmd{help.Cmd},
Call: func(x *Z.Cmd, _ ...string) error {
APIKey := Z.Vars.Get(`.apikey`)
if APIKey == "" {
return errors.New("API Key not set. Please use the command 'weather var set apikey'")
}
sites, err := site.GetList(APIKey)
if err != nil {
return err
}
fmt.Println("Available sites:")
for _, s := range sites {
fmt.Printf("Name: %s, Region: %s, Area: %s\n", s.Name, s.Region, s.Area)
}
return nil
},
}
var siteFindCmd = &Z.Cmd{
Name: `find`,
Summary: `filter sites from the sites list`,
Commands: []*Z.Cmd{help.Cmd},
Call: func(x *Z.Cmd, args ...string) error {
APIKey := Z.Vars.Get(`.apikey`)
if APIKey == "" {
return errors.New("API Key not set. Please use the command 'weather var set apikey'")
}
sites, err := site.GetList(APIKey)
if err != nil {
return err
}
for _, s := range sites {
out := fmt.Sprintf("Name: %s, Region: %s, Area: %s", s.Name, s.Region, s.Area)
if strings.Contains(out, args[0]) {
fmt.Println(out)
}
}
return nil
},
}
var siteSetCmd = &Z.Cmd{
Name: `set`,
Summary: `set the met office site location as the default for weather commands`,
Commands: []*Z.Cmd{help.Cmd},
Call: func(x *Z.Cmd, args ...string) error {
APIKey := Z.Vars.Get(`.apikey`)
if APIKey == "" {
return errors.New("API Key not set. Please use the command 'weather var set apikey'")
}
sites, err := site.GetList(APIKey)
if err != nil {
return err
}
for _, s := range sites {
if s.Name == args[0] {
err := Z.Vars.Set("locationID", s.Id)
if err != nil {
return err
}
regions, err := region.GetList(APIKey)
if err != nil {
return err
}
for _, r := range regions {
if r.Name == s.Region {
err := Z.Vars.Set("regionID", r.Id)
if err != nil {
return err
}
}
}
return nil
}
}
return errors.New("site is not in list. Please use weather site list to find list all sites")
},
}
var siteForecastCmd = &Z.Cmd{
Name: `forecast`,
Summary: `get the met office forecast for today using the set location id.`,
Commands: []*Z.Cmd{help.Cmd},
Call: func(x *Z.Cmd, args ...string) error {
APIKey := Z.Vars.Get(`.apikey`)
if APIKey == "" {
return errors.New("API Key not set. Please use the command 'weather var set apikey'")
}
siteID := Z.Vars.Get(`locationID`)
if siteID == "" {
return errors.New("site id is not set. Please use the command 'weather site set LOCATION' first. Or, use the command 'weather site find LOCATION' to find a specific location")
}
forecast, err := site.GetTodayForecast(APIKey, siteID)
if err != nil {
return err
}
fmt.Printf("Day %s°C - %s | Night %s °C - %s\n", forecast.Day.Temp, forecast.Day.Description, forecast.Night.Temp, forecast.Night.Description)
regionID := Z.Vars.Get(`regionID`)
if siteID == "" {
return errors.New("region id is not set. Please use the command 'weather site set LOCATION' first. Or, use the command 'weather site find LOCATION' to find a specific location")
}
regionForecast, err := region.GetForecast(APIKey, regionID)
if err != nil {
return err
}
if len(regionForecast) == 0 {
return errors.New("no forecasts found for region")
}
for _, f := range regionForecast {
if f.Title != "Headline:" {
fmt.Printf("%v\n\n", f.Content)
continue
}
fmt.Printf("%v %v\n", f.Title, f.Content)
}
return nil
},
}