forked from flexera-public/rsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
170 lines (157 loc) · 4.29 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
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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/rightscale/rsc/cmd"
"github.com/rightscale/rsc/httpclient"
"github.com/rightscale/rsc/log"
"gopkg.in/alecthomas/kingpin.v2"
// phoney reference to make Godep pull this in for the code generators
_ "bitbucket.org/pkg/inflect"
)
// Command line client entry point.
func main() {
app := kingpin.New("rsc", "A RightScale API client")
app.Writer(os.Stdout)
app.Version(VV)
cmdLine, err := ParseCommandLine(app)
if err != nil {
line := strings.Join(os.Args, " ")
PrintFatal("%s: %s", line, err.Error())
}
resp, err := ExecuteCommand(app, cmdLine)
if err != nil {
PrintFatal("%s", err.Error())
}
if resp == nil {
return // No results, just exit (e.g. setup, printed help...)
}
var notExactlyOneError bool
displayer, err := NewDisplayer(resp)
if err != nil {
PrintFatal("%s", err.Error())
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
// Let user know if something went wrong
fmt.Fprintln(errOut, resp.Status)
if len(displayer.body) > 0 {
fmt.Fprintln(errOut, displayer.body)
}
} else if cmdLine.ExtractOneSelect != "" {
err = displayer.ApplySingleExtract(cmdLine.ExtractOneSelect)
if err != nil {
notExactlyOneError = strings.Contains(err.Error(),
"instead of one value") // Ugh, there has to be a better way
PrintError(err.Error())
}
fmt.Fprint(out, displayer.Output())
} else {
if cmdLine.ExtractSelector != "" {
err = displayer.ApplyExtract(cmdLine.ExtractSelector, false)
} else if cmdLine.ExtractSelectorJSON != "" {
err = displayer.ApplyExtract(cmdLine.ExtractSelectorJSON, true)
} else if cmdLine.ExtractHeader != "" {
err = displayer.ApplyHeaderExtract(cmdLine.ExtractHeader)
}
if err != nil {
PrintFatal("%s", err.Error())
} else if cmdLine.Pretty {
displayer.Pretty()
}
fmt.Fprint(out, displayer.Output())
}
// Figure out exit code
exitStatus := 0
switch {
case notExactlyOneError:
exitStatus = 6
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
}
//fmt.Fprintf(os.Stderr, "exitStatus=%d\n", exitStatus)
osExit(exitStatus)
}
// Helper that runs command line with give command client
func runCommand(client cmd.CommandClient, cmdLine *cmd.CommandLine) (resp *http.Response, err error) {
cmds := strings.Split(cmdLine.Command, " ")
if cmdLine.ShowHelp {
err = client.ShowCommandHelp(cmdLine.Command)
} else if len(cmds) > 1 && cmds[1] == "actions" {
err = client.ShowAPIActions(cmdLine.Command)
} else {
resp, err = client.RunCommand(cmdLine.Command)
}
return
}
func ExecuteCommand(app *kingpin.Application, cmdLine *cmd.CommandLine) (resp *http.Response, err error) {
app.Writer(errOut)
log.Interactive()
topCommand := strings.Split(cmdLine.Command, " ")[0]
switch topCommand {
case "setup":
err = CreateConfig(cmdLine.ConfigPath)
case "json":
var b []byte
b, err = ioutil.ReadAll(os.Stdin)
if err == nil {
resp = CreateJSONResponse(b)
}
default:
// retry any failed API response as specified by the retry flag
for i := 0; i < cmdLine.Retry+1; i++ {
resp, err = doAPIRequest(topCommand, cmdLine)
if !shouldRetry(resp, err) {
break
}
}
}
return resp, err
}
// Constructs an http response from JSON input from Stdin
func CreateJSONResponse(b []byte) (resp *http.Response) {
// Remove UTF-8 Byte Order Mark if it exists
b = bytes.TrimPrefix(b, []byte{0xef, 0xbb, 0xbf})
resp = &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBuffer(b)),
}
return resp
}
func shouldRetry(resp *http.Response, err error) bool {
if err != nil {
if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
return true
}
}
if resp != nil {
if resp.StatusCode == 500 || resp.StatusCode == 503 {
return true
}
}
return false
}
var doAPIRequest = func(command string, cmdLine *cmd.CommandLine) (resp *http.Response, err error) {
httpclient.ResponseHeaderTimeout = time.Duration(cmdLine.Timeout) * time.Second
client, err := APIClient(command, cmdLine)
if err == nil {
resp, err = runCommand(client, cmdLine)
if err == nil {
return resp, err
}
}
return nil, err
}