Skip to content

Commit 2d1d37f

Browse files
committed
Upload validation is improved :)
1 parent c999de4 commit 2d1d37f

File tree

2 files changed

+85
-11
lines changed

2 files changed

+85
-11
lines changed

analyze.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func VerifyFolder(folderPath string) ([]string, error) {
3939
}
4040

4141
// Check for discrepancies in name and version
42+
/*
4243
if !strings.EqualFold(apiData.Name, folderPath) {
4344
log.Printf("[ERROR] Bad name: '%s' vs '%s' in api.yaml\n", folderPath, apiData.Name)
4445
errors = append(errors, "appname")
@@ -49,6 +50,16 @@ func VerifyFolder(folderPath string) ([]string, error) {
4950
log.Printf("[ERROR] Bad version in %s: expected %s, found %s\n", folderPath, apiData.AppVersion, folderVersion)
5051
errors = append(errors, "folder version")
5152
}
53+
*/
54+
if len(apiData.Name) == 0 {
55+
log.Printf("[ERROR] Empty appname in %s\n", apiFilePath)
56+
errors = append(errors, "appname")
57+
}
58+
59+
if len(apiData.AppVersion) == 0 {
60+
log.Printf("[ERROR] Empty appversion in %s\n", apiFilePath)
61+
errors = append(errors, "appversion")
62+
}
5263

5364
// Check unsupported large_image format
5465
if strings.Contains(apiData.LargeImage, "svg") {

cli.go

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@ import (
77
"io"
88
"time"
99
"bytes"
10+
"context"
1011
"os/exec"
1112
"strings"
12-
"io/ioutil"
1313
"net/http"
14+
"io/ioutil"
1415
"archive/zip"
16+
"path/filepath"
1517
"mime/multipart"
1618

17-
"path/filepath"
1819
"github.com/spf13/cobra"
20+
21+
//"encoding/json"
22+
//"github.com/shuffle/shuffle-shared"
1923
)
2024

2125

@@ -84,6 +88,11 @@ var versionCmd = &cobra.Command{
8488
func TestApp(cmd *cobra.Command, args []string) {
8589
log.Printf("[DEBUG] Testing app config: %s", args)
8690

91+
if len(args) <= 0 {
92+
log.Printf("[ERROR] No directory provided. Use the absolute path to the app directory.")
93+
return
94+
}
95+
8796
err := runUploadValidation(args)
8897
if err != nil {
8998
if strings.Contains(err.Error(), "no such file") {
@@ -102,7 +111,7 @@ func TestApp(cmd *cobra.Command, args []string) {
102111
return
103112
}
104113

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])
106115
}
107116

108117
// Example command: Greet the user
@@ -220,32 +229,61 @@ func validatePythonfile(filepath string) error {
220229
// Clear buffers
221230

222231
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)
225242
cmd.Stdout = &stdoutBuffer
226243
cmd.Stderr = &stderrBuffer
227-
228244
err = cmd.Run()
245+
if ctx.Err() == context.DeadlineExceeded {
246+
fmt.Println("Command timed out")
247+
}
248+
229249
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+
}
231257

232258
stdout := stdoutBuffer.String()
233259
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+
235262
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") {
237264
log.Printf("[ERROR] Python run Error: %s", line)
238265
} else if strings.Contains(strings.ToLower(line), "already satisfied") {
239266
continue
240267
} else {
241-
log.Printf(line)
268+
fmt.Println(line)
242269
}
243270
}
244271
}
245272

246273
stderr := stderrBuffer.String()
247274
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+
249287
}
250288

251289
return err
@@ -469,6 +507,21 @@ func UploadAppFromRepo(folderpath string) error {
469507
return err
470508
}
471509

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+
472525
if resp.StatusCode != http.StatusOK {
473526
return fmt.Errorf("Bad status: %s. Raw: %s", resp.Status, string(outputBody))
474527
}
@@ -482,6 +535,11 @@ var runParameter = &cobra.Command{
482535
Use: "run",
483536
Short: "Run a python script as if it is in the Shuffle UI",
484537
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+
485543
if len(apikey) <= 0 {
486544
fmt.Println("Please set the SHUFFLE_APIKEY or SHUFFLE_AUTHORIZATION environment variables to help with upload/download.")
487545
os.Exit(1)
@@ -653,6 +711,11 @@ var uploadApp = &cobra.Command{
653711
Use: "upload",
654712
Short: "Uploads and app from a directory containing the api.yaml",
655713
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+
656719
if len(apikey) <= 0 {
657720
fmt.Println("Please set the SHUFFLE_APIKEY or SHUFFLE_AUTHORIZATION environment variables to help with upload/download.")
658721
os.Exit(1)

0 commit comments

Comments
 (0)