/
command_Post.go
201 lines (198 loc) · 5.92 KB
/
command_Post.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package cmd
import (
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
)
type ServerIdsDryrunResponse struct {
Dryrun bool `json:"dryrun"`
ServerNames []string `json:"server-names"`
}
func commandRunPost(cmd *cobra.Command, command SchemaCommand) {
if b, _ := cmd.Flags().GetBool("interactive"); b && command.Interactive {
commandRunPostInteractive(cmd, command)
}
var qs []string
hasDryrunFlag := false
for _, field := range command.Run.Fields {
if field.Flag == "dryrun" {
hasDryrunFlag = true
if dryrun {
qs = append(qs, "dryrun=true")
}
continue
}
var value string
if field.Array {
arrayValue, _ := cmd.Flags().GetStringArray(field.Flag)
value = strings.Join(arrayValue, " ")
} else if field.Bool {
if boolValue, _ := cmd.Flags().GetBool(field.Flag); boolValue {
value = "true"
} else {
value = ""
}
} else {
value, _ = cmd.Flags().GetString(field.Flag)
if field.FromFile && value != "" {
fileBytes, err := ioutil.ReadFile(value)
if err == nil {
value = string(fileBytes)
} else {
fmt.Printf("%s\n", err.Error())
os.Exit(exitCodeInvalidFlags)
}
}
}
escapedValue := url.QueryEscape(value)
if (debug) {
fmt.Printf("field %s=%s / urlpart %s=%s\n", field.Flag, value, field.Name, escapedValue)
}
qs = append(qs, fmt.Sprintf("%s=%s", field.Name, escapedValue))
}
payload := strings.Join(qs, "&")
post_url := fmt.Sprintf("%s%s", apiServer, command.Run.Path)
if dryrun && ! hasDryrunFlag {
fmt.Printf("\nPOST %s\n", post_url)
fmt.Printf("%s\n\n", payload)
os.Exit(exitCodeDryrun)
} else {
if req, err := http.NewRequest("POST", post_url, strings.NewReader(payload)); err != nil {
fmt.Println("Failed to create POST request")
os.Exit(exitCodeUnexpected)
} else {
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("AuthClientId", apiClientid)
req.Header.Add("AuthSecret", apiSecret)
if r, err := http.DefaultClient.Do(req); err != nil {
fmt.Println("Failed to send POST request")
os.Exit(exitCodeUnexpected)
} else if body, err := ioutil.ReadAll(r.Body); err != nil {
fmt.Println("Failed to read POST response body")
os.Exit(exitCodeInvalidResponse)
} else if r.StatusCode != 200 {
commandExitErrorResponse(body, command)
} else if command.Run.ServerMethod == "GET" {
returnGetCommandListResponse(
getCommandOutputFormat("", command, "human"),
false, body, command, false, cmd,
)
} else if command.Run.Method == "sshServer" {
commandRunSsh(cmd, command, body, "")
os.Exit(exitCodeUnexpected)
} else if command.Run.Method == "sshServerKey" {
publicKey, _ := cmd.Flags().GetString("public-key")
if publicKey == "" {
fmt.Printf("--public-key argument is required\n")
os.Exit(exitCodeUnexpected)
}
commandRunSsh(cmd, command, body, publicKey)
os.Exit(exitCodeUnexpected)
} else if command.Run.SimpleJsonServerResponse {
if format == "json" {
fmt.Println(string(body))
os.Exit(0)
} else {
var response map[string]string
if err := json.Unmarshal(body, &response); err != nil {
fmt.Println(string(body))
fmt.Println("Failed to parse server response")
os.Exit(exitCodeInvalidResponse)
} else if format == "yaml" {
var d []byte
d, err = yaml.Marshal(&response)
if err != nil {
fmt.Println(string(body))
fmt.Println("Invalid response from server")
os.Exit(exitCodeInvalidResponse)
} else {
fmt.Println(string(d))
os.Exit(0)
}
} else {
message, hasMessage := response["message"]
if hasMessage {
fmt.Println(message)
os.Exit(0)
}
}
}
} else {
var commandIds []string;
if err := json.Unmarshal(body, &commandIds); err != nil {
if dryrun && hasDryrunFlag {
var dryrunRes ServerIdsDryrunResponse
if err := json.Unmarshal(body, &dryrunRes); err != nil {
fmt.Println(string(body))
fmt.Println("Failed to parse dryrun response")
os.Exit(exitCodeInvalidResponse)
} else if format == "yaml" || format == "json" {
var d []byte
var err error
if format == "yaml" {
d, err = yaml.Marshal(&dryrunRes)
} else {
d, err = json.Marshal(&dryrunRes)
}
if err != nil {
fmt.Println(string(body))
fmt.Println("Invalid response from server")
os.Exit(exitCodeInvalidResponse)
} else {
fmt.Println(string(d))
os.Exit(0)
}
} else {
fmt.Printf("server names to delete: %s\n", strings.Join(dryrunRes.ServerNames, ", "))
os.Exit(0)
}
} else {
fmt.Println(string(body))
fmt.Println("Failed to parse response")
os.Exit(exitCodeInvalidResponse)
}
}
if len(commandIds) == 0 {
fmt.Println("Unexpected command failure")
os.Exit(exitCodeUnexpected);
}
if format == "json" || format == "yaml" {
parsedResponse := make(map[string][]string);
parsedResponse["command_ids"] = commandIds;
var d []byte
var err error
if format == "yaml" {
d, err = yaml.Marshal(&parsedResponse)
} else {
d, err = json.Marshal(&parsedResponse)
}
if err != nil {
fmt.Println(string(body))
fmt.Println("Invalid response from server")
os.Exit(exitCodeInvalidResponse)
} else {
fmt.Println(string(d))
os.Exit(0)
}
} else if len(commandIds) == 1 {
fmt.Printf("Command ID: %s\n", commandIds[0])
waitForCommandIds(cmd, command, commandIds, getCommandOutputFormat("", command, "human"), false)
os.Exit(0)
} else {
fmt.Println("Command IDs:")
for _, commandId := range commandIds {
fmt.Printf("%s\n", commandId)
}
waitForCommandIds(cmd, command, commandIds, getCommandOutputFormat("", command, "human"), false)
os.Exit(0)
}
}
}
}
}