@@ -7,14 +7,26 @@ import (
77 "github.com/spf13/cobra"
88 "gopkg.in/yaml.v2"
99 "io/ioutil"
10+ "net/http"
11+ "net/url"
1012 "os"
1113 "strings"
1214 "text/tabwriter"
1315)
1416
1517func commandInit (cmd * cobra.Command , command SchemaCommand ) {
1618 for _ , flag := range command .Flags {
17- cmd .Flags ().StringP (flag .Name , "" , "" , flag .Usage )
19+ if flag .Array {
20+ var defaultValue []string
21+ if flag .Default != "" {
22+ defaultValue = append (defaultValue , flag .Default )
23+ }
24+ cmd .Flags ().StringArrayP (flag .Name , "" , defaultValue , flag .Usage )
25+ } else if flag .Bool {
26+ cmd .Flags ().BoolP (flag .Name , "" , flag .Default != "" , flag .Usage )
27+ } else {
28+ cmd .Flags ().StringP (flag .Name , "" , flag .Default , flag .Usage )
29+ }
1830 if flag .Required {
1931 cmd .MarkFlagRequired (flag .Name )
2032 }
@@ -35,10 +47,14 @@ func commandRun(cmd *cobra.Command, command SchemaCommand) {
3547}
3648
3749func commandRunGetList (cmd * cobra.Command , command SchemaCommand ) {
38- if resp , err := resty .R ().
50+ get_url := fmt .Sprintf ("%s%s" , apiServer , command .Run .Path )
51+ if dryrun {
52+ fmt .Printf ("\n GET %s\n " , get_url )
53+ os .Exit (exitCodeDryrun )
54+ } else if resp , err := resty .R ().
3955 SetHeader ("AuthClientId" , apiClientid ).
4056 SetHeader ("AuthSecret" , apiSecret ).
41- Get (fmt . Sprintf ( "%s%s" , apiServer , command . Run . Path ) );
57+ Get (get_url );
4258 err != nil {
4359 fmt .Println (err .Error ())
4460 os .Exit (exitCodeUnexpected )
@@ -90,9 +106,127 @@ func commandRunGetList(cmd *cobra.Command, command SchemaCommand) {
90106 }
91107}
92108
109+ func commandExitErrorResponse (body []byte ) {
110+ var errorResponse map [string ]interface {}
111+ if err := json .Unmarshal (body , & errorResponse ); err != nil {
112+ fmt .Println (string (body ))
113+ fmt .Println ("Failed to parse server error response" )
114+ os .Exit (exitCodeInvalidResponse )
115+ } else {
116+ var message string ;
117+ for k , v := range errorResponse {
118+ if k == "message" {
119+ message = v .(string );
120+ }
121+ }
122+ if format == "" && message != "" {
123+ fmt .Println (message )
124+ } else {
125+ var d []byte
126+ var err error
127+ if format == "yaml" {
128+ d , err = yaml .Marshal (& errorResponse )
129+ } else {
130+ d , err = json .Marshal (& errorResponse )
131+ }
132+ if err != nil {
133+ fmt .Println (string (body ))
134+ fmt .Println ("Invalid response from server" )
135+ os .Exit (exitCodeInvalidResponse )
136+ } else {
137+ fmt .Println (string (d ))
138+ }
139+ }
140+ os .Exit (exitCodeInvalidStatus )
141+ }
142+ }
143+
93144func commandRunPost (cmd * cobra.Command , command SchemaCommand ) {
94- fmt .Println ("post" )
95- os .Exit (exitCodeUnexpected )
145+ var qs []string
146+ for _ , field := range command .Run .Fields {
147+ var value string ;
148+ if field .Array {
149+ arrayValue , _ := cmd .Flags ().GetStringArray (field .Flag )
150+ value = strings .Join (arrayValue , " " )
151+ } else if field .Bool {
152+ if boolValue , _ := cmd .Flags ().GetBool (field .Flag ); boolValue {
153+ value = "true"
154+ } else {
155+ value = ""
156+ }
157+ } else {
158+ value , _ = cmd .Flags ().GetString (field .Flag )
159+ }
160+ escapedValue := url .PathEscape (value )
161+ if (debug ) {
162+ fmt .Printf ("\n field %s=%s / urlpart %s=%s" , field .Flag , value , field .Name , escapedValue )
163+ }
164+ qs = append (qs , fmt .Sprintf ("%s=%s" , field .Name , escapedValue ))
165+ }
166+ payload := strings .Join (qs , "&" )
167+ post_url := fmt .Sprintf ("%s%s" , apiServer , command .Run .Path )
168+ if dryrun {
169+ fmt .Printf ("\n POST %s\n " , post_url )
170+ fmt .Printf ("%s\n \n " , payload )
171+ os .Exit (exitCodeDryrun )
172+ } else {
173+ if req , err := http .NewRequest ("POST" , post_url , strings .NewReader (payload )); err != nil {
174+ fmt .Println ("Failed to create POST request" )
175+ os .Exit (exitCodeUnexpected )
176+ } else {
177+ req .Header .Add ("Content-Type" , "application/x-www-form-urlencoded" )
178+ req .Header .Add ("AuthClientId" , apiClientid )
179+ req .Header .Add ("AuthSecret" , apiSecret )
180+ if r , err := http .DefaultClient .Do (req ); err != nil {
181+ fmt .Println ("Failed to send POST request" )
182+ os .Exit (exitCodeUnexpected )
183+ } else if body , err := ioutil .ReadAll (r .Body ); err != nil {
184+ fmt .Println ("Failed to read POST response body" )
185+ os .Exit (exitCodeInvalidResponse )
186+ } else if r .StatusCode != 200 {
187+ commandExitErrorResponse (body )
188+ } else {
189+ var commandIds []string ;
190+ if err := json .Unmarshal (body , & commandIds ); err != nil {
191+ fmt .Println (string (body ))
192+ fmt .Println ("Failed to parse response" )
193+ os .Exit (exitCodeInvalidResponse )
194+ }
195+ if len (commandIds ) == 0 {
196+ fmt .Println ("Unexpected command failure" )
197+ os .Exit (exitCodeUnexpected );
198+ }
199+ if format == "json" || format == "yaml" {
200+ parsedResponse := make (map [string ][]string );
201+ parsedResponse ["command_ids" ] = commandIds ;
202+ var d []byte
203+ var err error
204+ if format == "yaml" {
205+ d , err = yaml .Marshal (& parsedResponse )
206+ } else {
207+ d , err = json .Marshal (& parsedResponse )
208+ }
209+ if err != nil {
210+ fmt .Println (string (body ))
211+ fmt .Println ("Invalid response from server" )
212+ os .Exit (exitCodeInvalidResponse )
213+ } else {
214+ fmt .Println (string (d ))
215+ os .Exit (0 )
216+ }
217+ } else if len (commandIds ) == 1 {
218+ fmt .Printf ("Command ID: %s\n " , commandIds [0 ])
219+ os .Exit (0 )
220+ } else {
221+ fmt .Println ("Command IDs:" )
222+ for _ , commandId := range commandIds {
223+ fmt .Printf ("%s\n " , commandId )
224+ }
225+ os .Exit (0 )
226+ }
227+ }
228+ }
229+ }
96230}
97231
98232func commandInitGetListOfLists (cmd * cobra.Command , command SchemaCommand ) {
0 commit comments