Skip to content

Commit a362432

Browse files
committed
add quick cli. Closes #2
1 parent c707e08 commit a362432

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

cmd/polls/main.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"log"
8+
"net/http"
9+
"os"
10+
11+
"github.com/tj/kingpin"
12+
)
13+
14+
// version of polls.
15+
var version = "0.0.1"
16+
17+
// endpoint for polls.
18+
var endpoint = "https://m131jyck4m.execute-api.us-west-2.amazonaws.com/prod"
19+
20+
// TODO: move this stuff to some client lib.
21+
type input struct {
22+
Options []string `json:"body"`
23+
}
24+
25+
type output struct {
26+
ID string `json:"id"`
27+
}
28+
29+
func main() {
30+
app := kingpin.New("polls", "GitHub polls.")
31+
app.Version(version)
32+
33+
create := app.Command("new", "Create a new poll.")
34+
options := create.Arg("options", "Poll options.").Required().Strings()
35+
create.Example(`polls new Tobi Loki Jane`, "Create a new poll for who is the best ferret.")
36+
create.Example(`polls new "Cats are better" "Ferrets are better"`, "Create a new poll with larger options.")
37+
38+
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
39+
case create.FullCommand():
40+
b, err := json.Marshal(input{Options: *options})
41+
if err != nil {
42+
log.Fatalf("error marshaling: %s", err)
43+
}
44+
45+
res, err := http.Post(endpoint+"/poll", "application/json", bytes.NewReader(b))
46+
if err != nil {
47+
log.Fatalf("error requesting: %s", err)
48+
}
49+
defer res.Body.Close()
50+
51+
var out output
52+
if err := json.NewDecoder(res.Body).Decode(&out); err != nil {
53+
log.Fatalf("error unmarshaling resonse: %s", err)
54+
}
55+
56+
fmt.Println(out.ID)
57+
}
58+
}

0 commit comments

Comments
 (0)