/
main.go
80 lines (69 loc) · 1.49 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"gopkg.in/urfave/cli.v1"
)
var (
addCommand = cli.Command{
Name: "add",
Usage: "add some cool",
Action: func(c *cli.Context) {
cool := c.Args().First()
authToken := c.GlobalString("auth")
endpoint := c.GlobalString("endpoint")
AddCool(endpoint, authToken, cool)
},
}
)
type AddRequest struct {
AuthToken string `json:"authToken"`
CoolThing string `json:"coolThing"`
}
type AddResponse struct {
Success bool `json:"success"`
}
func AddCool(endpoint, auth, cool string) {
jsonB := []byte(fmt.Sprintf(`{"authToken": "%s", "coolThing": "%s"}`, auth, cool))
resp, err := http.Post(endpoint, "application/json", bytes.NewBuffer(jsonB))
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
return
}
// b, err := ioutil.ReadAll(resp.Body)
// if err != nil {
// fmt.Println(err.Error())
// }
// fmt.Println(string(b))
dec := json.NewDecoder(resp.Body)
var data AddResponse
err = dec.Decode(&data)
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
return
}
if data.Success {
fmt.Printf("Success :)\n")
} else {
fmt.Printf("Failure :(\n")
}
}
func main() {
app := cli.NewApp()
app.Author = "wercker"
app.Email = "pleasemailus@wercker.com"
app.Name = "cooltime"
app.Usage = "add cool stuff"
app.Version = FullVersion()
app.Commands = []cli.Command{
addCommand,
}
app.Flags = []cli.Flag{
cli.StringFlag{Name: "auth"},
cli.StringFlag{Name: "endpoint"},
}
app.Run(os.Args)
}