@@ -7,15 +7,19 @@ import (
7
7
"io"
8
8
"time"
9
9
"bytes"
10
+ "context"
10
11
"os/exec"
11
12
"strings"
12
- "io/ioutil"
13
13
"net/http"
14
+ "io/ioutil"
14
15
"archive/zip"
16
+ "path/filepath"
15
17
"mime/multipart"
16
18
17
- "path/filepath"
18
19
"github.com/spf13/cobra"
20
+
21
+ //"encoding/json"
22
+ //"github.com/shuffle/shuffle-shared"
19
23
)
20
24
21
25
@@ -84,6 +88,11 @@ var versionCmd = &cobra.Command{
84
88
func TestApp (cmd * cobra.Command , args []string ) {
85
89
log .Printf ("[DEBUG] Testing app config: %s" , args )
86
90
91
+ if len (args ) <= 0 {
92
+ log .Printf ("[ERROR] No directory provided. Use the absolute path to the app directory." )
93
+ return
94
+ }
95
+
87
96
err := runUploadValidation (args )
88
97
if err != nil {
89
98
if strings .Contains (err .Error (), "no such file" ) {
@@ -102,7 +111,7 @@ func TestApp(cmd *cobra.Command, args []string) {
102
111
return
103
112
}
104
113
105
- log .Printf ("[INFO] App validated successfully. Upload it with shufflecli app upload %s" , args [0 ])
114
+ log .Printf ("[INFO] App validated successfully. Upload it with command: \n ' shufflecli app upload %s' " , args [0 ])
106
115
}
107
116
108
117
// Example command: Greet the user
@@ -220,32 +229,61 @@ func validatePythonfile(filepath string) error {
220
229
// Clear buffers
221
230
222
231
pythonCommand := fmt .Sprintf ("python3 %s" , copyFilepath )
223
- log .Printf ("[DEBUG] Validating python file by running '%s'" , pythonCommand )
224
- cmd = exec .Command ("python3" , copyFilepath )
232
+
233
+ timeout := 3 * time .Second
234
+ log .Printf ("[DEBUG] Validating python file by running '%s' for up to %d seconds." , pythonCommand , int (timeout )/ 1000000000 )
235
+
236
+ ctx , cancel := context .WithTimeout (context .Background (), timeout )
237
+ defer cancel () // Ensure resources are released
238
+
239
+ // Run for maximum 5 seconds
240
+ //cmd = exec.Command("python3", copyFilepath)
241
+ cmd = exec .CommandContext (ctx , "python3" , copyFilepath )
225
242
cmd .Stdout = & stdoutBuffer
226
243
cmd .Stderr = & stderrBuffer
227
-
228
244
err = cmd .Run ()
245
+ if ctx .Err () == context .DeadlineExceeded {
246
+ fmt .Println ("Command timed out" )
247
+ }
248
+
229
249
if err != nil {
230
- log .Printf ("[ERROR] Local run of python file: %s" , err )
250
+ if strings .Contains (err .Error (), "signal: killed" ) {
251
+ err = nil
252
+ }
253
+
254
+ if err != nil {
255
+ log .Printf ("[ERROR] Local run of python file: %s" , err )
256
+ }
231
257
232
258
stdout := stdoutBuffer .String ()
233
259
if len (stdout ) > 0 {
234
- //log.Printf("\n\nPython run Output: %s\n\n", stdout)
260
+ log .Printf ("\n \n ===== Python run (stdout) ===== \n " )
261
+
235
262
for _ , line := range strings .Split (stdout , "\n " ) {
236
- if strings .Contains (strings .ToLower (line ), "traceback" ) {
263
+ if strings .Contains (strings .ToLower (line ), "traceback" ) && ! strings . Contains ( strings . ToLower ( line ), "Bad resp" ) {
237
264
log .Printf ("[ERROR] Python run Error: %s" , line )
238
265
} else if strings .Contains (strings .ToLower (line ), "already satisfied" ) {
239
266
continue
240
267
} else {
241
- log . Printf (line )
268
+ fmt . Println (line )
242
269
}
243
270
}
244
271
}
245
272
246
273
stderr := stderrBuffer .String ()
247
274
if len (stderr ) > 0 {
248
- log .Printf ("\n \n ===== Python run Error ===== \n %s\n \n " , stderr )
275
+ log .Printf ("\n \n ===== Python run (stderr) ===== \n " )
276
+
277
+ for _ , line := range strings .Split (stdout , "\n " ) {
278
+ if strings .Contains (strings .ToLower (line ), "traceback" ) {
279
+ log .Printf ("[ERROR] Python run Error: %s" , line )
280
+ } else if strings .Contains (strings .ToLower (line ), "already satisfied" ) || strings .Contains (strings .ToLower (line ), "[ERROR]" ) || strings .Contains (strings .ToLower (line ), "[WARNING]" ) || strings .Contains (strings .ToLower (line ), "[INFO]" ) || strings .Contains (strings .ToLower (line ), "[DEBUG]" ) {
281
+ continue
282
+ } else {
283
+ fmt .Println (line )
284
+ }
285
+ }
286
+
249
287
}
250
288
251
289
return err
@@ -469,6 +507,21 @@ func UploadAppFromRepo(folderpath string) error {
469
507
return err
470
508
}
471
509
510
+ /*
511
+ mappedValue := shuffle.RequestResponse{}
512
+ unmarshalErr := json.Unmarshal(outputBody, &mappedValue)
513
+ if unmarshalErr != nil {
514
+ log.Printf("[ERROR] Problem unmarshalling response: %s", unmarshalErr)
515
+ //return unmarshalErr
516
+ } else {
517
+ outputBody = []byte(fmt.Sprintf("Raw output: %s", mappedValue.Details))
518
+ }
519
+
520
+ if len(mappedValue.Details) > 0 {
521
+ log.Printf("[INFO] Upload Details: %s", mappedValue.Details)
522
+ }
523
+ */
524
+
472
525
if resp .StatusCode != http .StatusOK {
473
526
return fmt .Errorf ("Bad status: %s. Raw: %s" , resp .Status , string (outputBody ))
474
527
}
@@ -482,6 +535,11 @@ var runParameter = &cobra.Command{
482
535
Use : "run" ,
483
536
Short : "Run a python script as if it is in the Shuffle UI" ,
484
537
Run : func (cmd * cobra.Command , args []string ) {
538
+ if len (args ) <= 0 {
539
+ log .Println ("[ERROR] No URL provided. Use the URL from the Shuffle UI." )
540
+ return
541
+ }
542
+
485
543
if len (apikey ) <= 0 {
486
544
fmt .Println ("Please set the SHUFFLE_APIKEY or SHUFFLE_AUTHORIZATION environment variables to help with upload/download." )
487
545
os .Exit (1 )
@@ -653,6 +711,11 @@ var uploadApp = &cobra.Command{
653
711
Use : "upload" ,
654
712
Short : "Uploads and app from a directory containing the api.yaml" ,
655
713
Run : func (cmd * cobra.Command , args []string ) {
714
+ if len (args ) <= 0 {
715
+ log .Println ("[ERROR] No directory provided. Use the absolute path to the app directory." )
716
+ return
717
+ }
718
+
656
719
if len (apikey ) <= 0 {
657
720
fmt .Println ("Please set the SHUFFLE_APIKEY or SHUFFLE_AUTHORIZATION environment variables to help with upload/download." )
658
721
os .Exit (1 )
0 commit comments