forked from goadesign/goa
/
cli.go
96 lines (90 loc) · 2.02 KB
/
cli.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
package client
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"golang.org/x/net/websocket"
)
// HandleResponse logs the response details and exits the process with a status computed from
// the response status code. The mapping of response status code to exit status is as follows:
//
// 401: 1
// 402 to 500 (other than 403 and 404): 2
// 403: 3
// 404: 4
// 500+: 5
func HandleResponse(c *Client, resp *http.Response, pretty bool) {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to read body: %s", err)
os.Exit(-1)
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
// Let user know if something went wrong
var sbody string
if len(body) > 0 {
sbody = ": " + string(body)
}
fmt.Printf("error: %d%s", resp.StatusCode, sbody)
} else if !c.Dump && len(body) > 0 {
var out string
if pretty {
var jbody interface{}
err = json.Unmarshal(body, &jbody)
if err != nil {
out = string(body)
} else {
var b []byte
b, err = json.MarshalIndent(jbody, "", " ")
if err == nil {
out = string(b)
} else {
out = string(body)
}
}
} else {
out = string(body)
}
fmt.Print(out)
}
// Figure out exit code
exitStatus := 0
switch {
case resp.StatusCode == 401:
exitStatus = 1
case resp.StatusCode == 403:
exitStatus = 3
case resp.StatusCode == 404:
exitStatus = 4
case resp.StatusCode > 399 && resp.StatusCode < 500:
exitStatus = 2
case resp.StatusCode > 499:
exitStatus = 5
}
os.Exit(exitStatus)
}
// WSWrite sends STDIN lines to a websocket server.
func WSWrite(ws *websocket.Conn) {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
t := scanner.Text()
ws.Write([]byte(t))
fmt.Printf(">> %s\n", t)
}
}
// WSRead reads from a websocket and print the read messages to STDOUT.
func WSRead(ws *websocket.Conn) {
msg := make([]byte, 512)
for {
n, err := ws.Read(msg)
if err != nil {
log.Fatal(err)
}
fmt.Printf("<< %s\n", msg[:n])
}
}