forked from genuinetools/weather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
165 lines (134 loc) · 4.23 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
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
package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"os"
"strings"
"github.com/genuinetools/pkg/cli"
"github.com/genuinetools/weather/forecast"
"github.com/genuinetools/weather/geocode"
"github.com/genuinetools/weather/version"
"github.com/mitchellh/colorstring"
)
const (
defaultServerURI string = "https://geocode.jessfraz.com"
)
var (
location string
units string
days int
ignoreAlerts bool
hideIcon bool
noForecast bool
jsonOut bool
server string
client bool
geo geocode.Geocode
)
//go:generate go run icons/generate.go
func main() {
// Create a new cli program.
p := cli.NewProgram()
p.Name = "weather"
p.Description = "Weather forecast via the command line"
// Set the GitCommit and Version.
p.GitCommit = version.GITCOMMIT
p.Version = version.VERSION
// Build the list of available commands.
p.Commands = []cli.Command{
&serverCommand{},
}
// Setup the global flags.
p.FlagSet = flag.NewFlagSet("global", flag.ExitOnError)
p.FlagSet.StringVar(&location, "location", "", "Location to get the weather")
p.FlagSet.StringVar(&location, "l", "", "Location to get the weather (shorthand)")
p.FlagSet.BoolVar(&client, "client", false, "Get location for the ssh client")
p.FlagSet.BoolVar(&client, "c", false, "Get location for the ssh client (shorthand)")
p.FlagSet.StringVar(&units, "units", "auto", "System of units (e.g. auto, us, si, ca, uk2)")
p.FlagSet.StringVar(&units, "u", "auto", "System of units (shorthand) (e.g. auto, us, si, ca, uk2)")
p.FlagSet.StringVar(&server, "server", defaultServerURI, "Weather API server uri")
p.FlagSet.StringVar(&server, "s", defaultServerURI, "Weather API server uri (shorthand)")
p.FlagSet.IntVar(&days, "days", 0, "No. of days to get forecast")
p.FlagSet.IntVar(&days, "d", 0, "No. of days to get forecast (shorthand)")
p.FlagSet.BoolVar(&ignoreAlerts, "ignore-alerts", false, "Ignore alerts in weather output")
p.FlagSet.BoolVar(&hideIcon, "hide-icon", false, "Hide the weather icons from being output")
p.FlagSet.BoolVar(&noForecast, "no-forecast", false, "Hide the forecast for the next 16 hours")
p.FlagSet.BoolVar(&jsonOut, "json", false, "Prints the raw JSON API response")
// Set the before function.
p.Before = func(ctx context.Context) error {
if len(server) < 1 {
return errors.New("please enter a weather API server uri or leave blank to use the default")
}
return nil
}
// Set the main program action.
p.Action = func(ctx context.Context, args []string) error {
var err error
if location == "" {
sshConn := os.Getenv("SSH_CONNECTION")
if client && len(sshConn) > 0 {
// use their ssh connection to locate them
ipports := strings.Split(sshConn, " ")
geo, err = geocode.IPLocate(ipports[0])
if err != nil {
printError(err)
}
} else {
// auto locate them
geo, err = geocode.Autolocate()
if err != nil {
printError(err)
}
if geo.Latitude == 0 || geo.Longitude == 0 {
printError(errors.New("latitude and longitude could not be determined from your IP so the weather will not be accurate\nTry: weather -l <your_zipcode> OR weather -l \"your city, state\""))
}
}
} else {
// get geolocation data for the given location
geo, err = geocode.Locate(location, server)
if err != nil {
printError(err)
}
}
if geo.Latitude == 0 || geo.Longitude == 0 {
printError(errors.New("latitude and longitude could not be determined so the weather will not be accurate"))
}
data := forecast.Request{
Latitude: geo.Latitude,
Longitude: geo.Longitude,
Units: units,
Exclude: []string{"minutely"},
}
if noForecast {
data.Exclude = append(data.Exclude, "hourly")
}
fc, err := forecast.Get(fmt.Sprintf("%s/forecast", server), data)
if err != nil {
printError(err)
}
if jsonOut {
jsn, err := json.Marshal(&fc)
if err != nil {
printError(err)
}
fmt.Println(string(jsn))
return nil
}
if err := forecast.PrintCurrent(fc, geo, ignoreAlerts, hideIcon); err != nil {
printError(err)
}
if days > 0 {
forecast.PrintDaily(fc, days)
}
return nil
}
// Run our program.
p.Run()
}
func printError(err error) {
fmt.Println(colorstring.Color("[red]" + err.Error()))
os.Exit(1)
}