-
Notifications
You must be signed in to change notification settings - Fork 10
/
displayer.go
140 lines (131 loc) · 3.18 KB
/
displayer.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/rightscale/go-jsonselect"
)
// Displayer provides helper methods to display command responses back to the user
// This includes optionally extracting values with JSON:select and pretty-printing
type Displayer struct {
response *http.Response
body string
RawOutput interface{}
prettify bool
}
// NewDisplayer creates a new displayer using the response body.
func NewDisplayer(resp *http.Response) (*Displayer, error) {
defer resp.Body.Close()
js, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Failed to read response (%s)", err)
}
disp := Displayer{response: resp, body: string(js)}
if len(js) > 2 {
err = json.Unmarshal(js, &disp.RawOutput)
if err != nil {
disp.RawOutput = string(js)
}
}
return &disp, nil
}
// ApplySingleExtract applies the given JSON selector and returns the results.
// It's an error if the selector yields more than one value.
func (d *Displayer) ApplySingleExtract(extract string) error {
if err := d.ApplyExtract(extract, true); err != nil {
return err
}
outputs := d.RawOutput.([]interface{})
if len(outputs) != 1 {
d.RawOutput = nil
return fmt.Errorf("JSON selector '%s' returned %d instead of one value",
extract, len(outputs))
}
if len(outputs) == 0 {
d.RawOutput = ""
} else {
switch v := outputs[0].(type) {
case nil:
d.RawOutput = ""
case float64, bool:
d.RawOutput = fmt.Sprint(v)
case string:
d.RawOutput = v
default:
d.RawOutput = v
}
d.RawOutput = outputs[0]
}
return nil
}
// ApplyExtract applies selector to js.
func (d *Displayer) ApplyExtract(selector string, js bool) error {
parser, err := jsonselect.CreateParserFromString(d.body)
if err != nil {
return fmt.Errorf("Failed to load response JSON: %s, JSON was:\n%s", err, d.body)
}
outputs, err := parser.GetValues(selector)
if !js {
out := ""
for _, o := range outputs {
b, _ := json.Marshal(o)
out += string(b) + "\n"
}
d.RawOutput = out
} else {
d.RawOutput = outputs
}
if err != nil {
return fmt.Errorf("Failed to apply JSON selector '%s' to response: %s, JSON was:\n%s",
selector, err, d.body)
}
return nil
}
// ApplyHeaderExtract reads the value of the given header.
func (d *Displayer) ApplyHeaderExtract(header string) error {
d.RawOutput = d.response.Header.Get(header)
if d.RawOutput == "" {
return fmt.Errorf("Response does not contain the '%s' header", header)
}
return nil
}
// Pretty switches the display mode to produce human friendly output.
func (d *Displayer) Pretty() {
d.prettify = true
}
// Output returns the current output.
func (d *Displayer) Output() string {
output := d.RawOutput
if output == nil {
return ""
}
if outputStr, ok := d.RawOutput.(string); ok {
suffix := ""
if d.prettify {
suffix = "\n"
}
return outputStr + suffix
}
var out string
var err error
if d.prettify {
var b []byte
b, err = json.MarshalIndent(output, "", " ")
if err == nil {
out = string(b) + "\n"
}
} else {
var b []byte
b, err = json.Marshal(output)
out = string(b)
}
if err != nil {
fm := "%v"
if d.prettify {
fm += "\n"
}
return fmt.Sprintf(fm, output)
}
return out
}