-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsrk.go
331 lines (263 loc) Β· 7.58 KB
/
srk.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strings"
"time"
"github.com/briandowns/spinner"
v "github.com/sheetrocks/srk/values"
)
type FormulaBody struct {
Name string
Runtime string
Help string
ScriptText string
Dependencies string
}
type Asset struct {
Filename string
MimeType string
Content string
}
type Parameter struct {
Type string
Title string
}
type ParameterJSON struct {
Type v.ValueType
Title string
}
type ChartTypeSubmission struct {
Title string
Parameters []Parameter
Assets []Asset
Help string
}
func (p *Parameter) MarshalJSON() ([]byte, error) {
var t v.ValueType
switch p.Type {
case "number":
t = v.NUMBER
break
case "text":
t = v.TEXT
case "date":
t = v.DATE
case "boolean":
t = v.BOOLEAN
}
return json.Marshal(ParameterJSON{t, p.Title})
}
type ParametersConfig struct {
Version string
Parameters []Parameter
}
type ChartType struct {
ID string
Title string
}
type Config struct {
Name string
Runtime string
Formula string
Help string
Dependencies string
}
type Runtime struct {
Runtime string
}
type WebConfig struct {
Name string
Runtime string
Help string
Parameters []Parameter
Assets []string
}
func main() {
if len(os.Args) < 3 {
log.Fatal("Error: not enough arguments.\nExpected command format: './srk push ./path/to/config.json'")
}
command := os.Args[1]
filepath := os.Args[2]
token := os.Getenv("SRK_TOKEN")
baseUrl := os.Getenv("SRK_BASE_URL")
if baseUrl == "" {
baseUrl = "https://sheet.rocks/api/v1"
}
if token == "" {
log.Fatal("You must provide your API token in the SRK_TOKEN environment variable.")
}
if command != "push" {
log.Fatal("Command not found. Supported commands: push")
}
_, err := os.Stat(filepath)
if err != nil {
log.Fatal("Error: configuration file path was not recognized. Please check the file path.\n\n", err)
return
}
dir := path.Dir(filepath)
dat, err := ioutil.ReadFile(filepath)
if err != nil {
log.Fatal(err)
}
runtime := Runtime{}
err = json.Unmarshal(dat, &runtime)
if err != nil {
log.Fatal(err)
}
if runtime.Runtime == "" {
log.Fatal("Error: must specify runtime.")
} else if !(runtime.Runtime == "python" || runtime.Runtime == "web") {
log.Fatal(`Error: invalid runtime. Accepted runtimes: "python", "web"`)
}
if runtime.Runtime == "python" {
config := Config{}
err = json.Unmarshal(dat, &config)
if err != nil {
log.Fatal("Error: could not parse configuration file.\n\n", err)
}
if config.Name == "" {
log.Fatal("Error: config file must have name field.")
}
if config.Runtime != "python" {
log.Fatal(`Error: invalid runtime field specified. Accepted runtimes: "python"`)
}
scriptText, err := ioutil.ReadFile(path.Join(dir, config.Formula))
if err != nil {
log.Fatal("Error: could not find formula script located at %s\n", config.Formula)
}
helpText, err := ioutil.ReadFile(path.Join(dir, config.Help))
if err != nil {
log.Fatal("Error: could not find help file located at %s\n", config.Help)
}
dependenciesText, err := ioutil.ReadFile(path.Join(dir, config.Dependencies))
if err != nil {
log.Fatal("Error: could not find dependencies file located at %s\n", config.Dependencies)
}
s := spinner.New(spinner.CharSets[36], 100*time.Millisecond)
s.Color("fgBlack")
s.FinalMSG = ""
s.Start()
formulaBody := FormulaBody{Name: strings.ToUpper(config.Name), Runtime: config.Runtime, Help: string(helpText), ScriptText: string(scriptText), Dependencies: string(dependenciesText)}
body, _ := json.Marshal(formulaBody)
r := bytes.NewReader(body)
req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("%s/formula", baseUrl), r)
if err != nil {
log.Fatal(err)
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal("Unhandled error submitting to SheetRocks: ", err)
}
s.Stop()
if resp.StatusCode == 200 {
fmt.Printf("π Success! You have pushed your formula \"%s\" to SheetRocks π\nThe formula is available for immediate use in any SheetRocks workbook you've made. Please refresh your workbook to pick up the new formula.\n", formulaBody.Name)
} else {
bytes, _ := ioutil.ReadAll(resp.Body)
fmt.Println("SheetRocks error: ", string(bytes))
}
}
if runtime.Runtime == "web" {
webConfig := WebConfig{}
err = json.Unmarshal(dat, &webConfig)
if err != nil {
log.Fatal("Error: could not read configuration file for chart.")
}
if webConfig.Name == "" {
log.Fatal(`Error: configuration file must have a "name" field which specifies the name of the chart.`)
}
if webConfig.Help == "" {
log.Fatal("Error: must specify path of help file.")
}
helpText, err := ioutil.ReadFile(path.Join(dir, webConfig.Help))
if err != nil {
log.Fatal("Error: could not read help file: ", err)
}
hasIndex := false
for _, filename := range webConfig.Assets {
if filename == "index.html" {
hasIndex = true
}
}
if !hasIndex {
log.Fatal(`Error: there must be an "index.html" file listed in assets.`)
}
assets := []Asset{}
for _, assetPath := range webConfig.Assets {
assetDir := path.Dir(assetPath)
if assetDir != "." {
log.Fatal("Error: assets must be in same directory as config file. Asset not in config directory: ", assetPath)
}
dat, err := ioutil.ReadFile(path.Join(dir, assetPath))
if err != nil {
log.Fatal("Error: could not read asset located at ", assetPath)
}
mimeType := ""
switch path.Ext(assetPath) {
case ".html":
mimeType = "text/html"
break
case ".js":
mimeType = "application/javascript"
break
case ".css":
mimeType = "text/plain"
break
}
if mimeType != "" {
assets = append(assets, Asset{assetPath, mimeType, string(dat)})
}
}
chartSubmission := ChartTypeSubmission{Title: webConfig.Name, Help: string(helpText), Parameters: webConfig.Parameters}
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/chart-type", baseUrl), nil)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal("Unhandled error encountered while submitting to SheetRocks: ", err)
}
if resp.StatusCode == 200 {
chartTypes := []ChartType{}
json.NewDecoder(resp.Body).Decode(&chartTypes)
resp.Body.Close()
body, _ := json.Marshal(chartSubmission)
r := bytes.NewReader(body)
foundChartID := ""
for _, ct := range chartTypes {
if ct.Title == chartSubmission.Title {
foundChartID = ct.ID
}
}
var outputType string
if foundChartID == "" {
outputType = "added"
req, err = http.NewRequest(http.MethodPost, fmt.Sprintf("%s/chart-type", baseUrl), r)
} else {
outputType = "updated"
req, err = http.NewRequest(http.MethodPut, fmt.Sprintf("%s/chart-type/%s", baseUrl, foundChartID), r)
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal("Unhandled error encountered while submitting to SheetRocks: ", err)
}
if resp.StatusCode == 200 {
fmt.Printf("π Successfully %s new chart \"%s\" π\n", outputType, chartSubmission.Title)
} else {
fmt.Printf("Encountered unexpected status code: %d\n", resp.StatusCode)
}
}
}
}