forked from tidbyt/pixlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush.go
113 lines (95 loc) · 2.54 KB
/
push.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"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"github.com/spf13/cobra"
)
const (
TidbytAPIPush = "https://api.tidbyt.com/v0/devices/%s/push"
APITokenEnv = "TIDBYT_API_TOKEN"
)
var (
apiToken string
installationID string
background bool
)
type TidbytPushJSON struct {
DeviceID string `json:"deviceID"`
Image string `json:"image"`
InstallationID string `json:"installationID"`
Background bool `json:"background"`
}
func init() {
rootCmd.AddCommand(pushCmd)
pushCmd.Flags().StringVarP(&apiToken, "api-token", "t", "", "Tidbyt API token")
pushCmd.Flags().StringVarP(&installationID, "installation-id", "i", "", "Give your installation an ID to keep it in the rotation")
pushCmd.Flags().BoolVarP(&background, "background", "b", false, "Don't immediately show the image on the device")
}
var pushCmd = &cobra.Command{
Use: "push [device ID] [webp image] [installationID]",
Short: "Pushes a webp image to a Tidbyt device",
Args: cobra.MinimumNArgs(2),
Run: push,
}
func push(cmd *cobra.Command, args []string) {
deviceID := args[0]
image := args[1]
// TODO (mark): This is better served as a flag, but I don't want to break
// folks in the short term. We should consider dropping this as an arguement
// in a future release.
if len(args) == 3 {
installationID = args[2]
}
if apiToken == "" {
apiToken = os.Getenv(APITokenEnv)
}
if apiToken == "" {
fmt.Printf("blank Tidbyt API token (set $%s or pass with --api-token)\n", APITokenEnv)
os.Exit(1)
}
imageData, err := ioutil.ReadFile(image)
if err != nil {
fmt.Printf("failed to read file %s: %v\n", image, err)
os.Exit(1)
}
payload, err := json.Marshal(
TidbytPushJSON{
DeviceID: deviceID,
Image: base64.StdEncoding.EncodeToString(imageData),
InstallationID: installationID,
Background: background,
},
)
if err != nil {
fmt.Printf("failed to marshal json: %v\n", err)
os.Exit(1)
}
client := &http.Client{}
req, err := http.NewRequest(
"POST",
fmt.Sprintf(TidbytAPIPush, deviceID),
bytes.NewReader(payload),
)
if err != nil {
fmt.Printf("creating POST request: %v\n", err)
os.Exit(1)
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", apiToken))
resp, err := client.Do(req)
if err != nil {
fmt.Printf("pushing to API: %v\n", err)
os.Exit(1)
}
if resp.StatusCode != 200 {
fmt.Printf("Tidbyt API returned status %s\n", resp.Status)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
os.Exit(1)
}
os.Exit(0)
}