forked from mdb/seaweed-cli
/
seaweed.go
113 lines (98 loc) · 2.44 KB
/
seaweed.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
package main
import (
"bytes"
"fmt"
"os"
"strconv"
"time"
"github.com/codegangsta/cli"
"github.com/crackcomm/go-clitable"
"github.com/mdb/seaweed"
)
func main() {
app := cli.NewApp()
app.Name = "seaweed-cli"
app.Usage = "Should I go surfing?"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "apiKey",
Usage: "Magic Seaweed API key",
EnvVar: "MAGIC_SEAWEED_API_KEY",
},
}
app.Commands = []cli.Command{
{
Name: "forecast",
Usage: "forcast <spotId>",
Description: "View the forecast for a spot",
Action: forecast,
},
{
Name: "today",
Usage: "today <spotId>",
Description: "View today's forecast for a spot",
Action: today,
},
{
Name: "tomorrow",
Usage: "tomorrow <spotId>",
Description: "View tomorrow's forecast for a spot",
Action: tomorrow,
},
}
app.Run(os.Args)
}
func forecast(c *cli.Context) {
client := client(c)
spot := c.Args().First()
forecast, err := client.Forecast(spot)
if err != nil {
panic(err)
}
printForecasts(spot, forecast)
}
func today(c *cli.Context) {
client := client(c)
spot := c.Args().First()
forecast, err := client.Today(spot)
if err != nil {
panic(err)
}
printForecasts(spot, forecast)
}
func tomorrow(c *cli.Context) {
client := client(c)
spot := c.Args().First()
forecast, err := client.Tomorrow(spot)
if err != nil {
panic(err)
}
printForecasts(spot, forecast)
}
func printForecasts(spot string, forecasts []seaweed.Forecast) {
s := []map[string]interface{}{}
for _, each := range forecasts {
m := map[string]interface{}{}
m["Date"] = time.Unix(each.LocalTimestamp, 0).Format("Mon 01/02 03:04 pm")
m["Solid Rating"] = each.SolidRating
m["Faded Rating"] = each.FadedRating
m["Primary Swell Height"] = concat([]string{strconv.FormatFloat(each.Swell.Components.Primary.Height, 'f', 2, 64), each.Swell.Unit})
m["Wind"] = concat([]string{strconv.Itoa(each.Wind.Speed), " ", each.Wind.Unit, " ", each.Wind.CompassDirection})
s = append(s, m)
}
if len(s) != 0 {
clitable.PrintTable([]string{"Date", "Primary Swell Height", "Wind", "Solid Rating", "Faded Rating"}, s)
} else {
fmt.Printf("No forecast found for spot: %s\n", spot)
}
}
func client(c *cli.Context) *seaweed.Client {
return seaweed.NewClient(os.Getenv("MAGIC_SEAWEED_API_KEY"))
}
func concat(arr []string) string {
var buff bytes.Buffer
for _, elem := range arr {
buff.WriteString(elem)
}
return buff.String()
}