-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (108 loc) · 3 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
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/mitchellh/colorstring"
"github.com/smalldave/weather/forecast"
"github.com/smalldave/weather/geocode"
"github.com/smalldave/weather/version"
)
const (
defaultServerURI string = "https://geocode.jessfraz.com"
)
var (
location string
units string
days int
ignoreAlerts bool
showIcon bool
server string
vrsn bool
client bool
geo geocode.Geocode
)
func printError(err error) {
fmt.Println(colorstring.Color("[red]" + err.Error()))
os.Exit(1)
}
func init() {
// parse flags
flag.BoolVar(&vrsn, "version", false, "print version and exit")
flag.BoolVar(&vrsn, "v", false, "print version and exit (shorthand)")
flag.StringVar(&location, "location", "", "Location to get the weather")
flag.StringVar(&location, "l", "", "Location to get the weather (shorthand)")
flag.BoolVar(&client, "client", false, "Get location for the ssh client")
flag.BoolVar(&client, "c", false, "Get location for the ssh client (shorthand)")
flag.StringVar(&units, "units", "auto", "System of units")
flag.StringVar(&units, "u", "auto", "System of units (shorthand)")
flag.StringVar(&server, "server", defaultServerURI, "Weather API server uri")
flag.StringVar(&server, "s", defaultServerURI, "Weather API server uri (shorthand)")
flag.IntVar(&days, "days", 0, "No. of days to get forecast")
flag.IntVar(&days, "d", 0, "No. of days to get forecast (shorthand)")
flag.BoolVar(&ignoreAlerts, "ignore-alerts", false, "Ignore alerts in weather output")
flag.Usage = func() {
flag.PrintDefaults()
}
flag.Parse()
if server == "" {
usageAndExit("Please enter a Weather API server uri or leave blank to use the default.", 0)
}
}
//go:generate go run icons/generate.go
func main() {
if vrsn {
fmt.Printf("weather version %s, build %s", version.VERSION, version.GITCOMMIT)
return
}
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)
}
}
} else {
// get geolocation data for the given location
geo, err = geocode.Locate(location, server)
if err != nil {
printError(err)
}
}
data := forecast.Request{
Latitude: geo.Latitude,
Longitude: geo.Longitude,
Units: units,
Exclude: []string{"hourly", "minutely"},
}
fc, err := forecast.Get(fmt.Sprintf("%s/forecast", server), data)
if err != nil {
printError(err)
}
if err := forecast.PrintCurrent(fc, geo, ignoreAlerts); err != nil {
printError(err)
}
if days > 1 {
forecast.PrintDaily(fc, days)
}
}
func usageAndExit(message string, exitCode int) {
if message != "" {
fmt.Fprintf(os.Stderr, message)
fmt.Fprintf(os.Stderr, "\n\n")
}
flag.Usage()
fmt.Fprintf(os.Stderr, "\n")
os.Exit(exitCode)
}