From 2d1d37fb42fd7218fda19aa072321a3593ae44d1 Mon Sep 17 00:00:00 2001 From: Frikky Date: Thu, 23 Jan 2025 00:48:03 +0100 Subject: [PATCH] Upload validation is improved :) --- analyze.go | 11 +++++++ cli.go | 85 +++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 85 insertions(+), 11 deletions(-) diff --git a/analyze.go b/analyze.go index 0ae94c6..719f4a7 100644 --- a/analyze.go +++ b/analyze.go @@ -39,6 +39,7 @@ func VerifyFolder(folderPath string) ([]string, error) { } // Check for discrepancies in name and version + /* if !strings.EqualFold(apiData.Name, folderPath) { log.Printf("[ERROR] Bad name: '%s' vs '%s' in api.yaml\n", folderPath, apiData.Name) errors = append(errors, "appname") @@ -49,6 +50,16 @@ func VerifyFolder(folderPath string) ([]string, error) { log.Printf("[ERROR] Bad version in %s: expected %s, found %s\n", folderPath, apiData.AppVersion, folderVersion) errors = append(errors, "folder version") } + */ + if len(apiData.Name) == 0 { + log.Printf("[ERROR] Empty appname in %s\n", apiFilePath) + errors = append(errors, "appname") + } + + if len(apiData.AppVersion) == 0 { + log.Printf("[ERROR] Empty appversion in %s\n", apiFilePath) + errors = append(errors, "appversion") + } // Check unsupported large_image format if strings.Contains(apiData.LargeImage, "svg") { diff --git a/cli.go b/cli.go index 3345676..317c488 100644 --- a/cli.go +++ b/cli.go @@ -7,15 +7,19 @@ import ( "io" "time" "bytes" + "context" "os/exec" "strings" - "io/ioutil" "net/http" + "io/ioutil" "archive/zip" + "path/filepath" "mime/multipart" - "path/filepath" "github.com/spf13/cobra" + + //"encoding/json" + //"github.com/shuffle/shuffle-shared" ) @@ -84,6 +88,11 @@ var versionCmd = &cobra.Command{ func TestApp(cmd *cobra.Command, args []string) { log.Printf("[DEBUG] Testing app config: %s", args) + if len(args) <= 0 { + log.Printf("[ERROR] No directory provided. Use the absolute path to the app directory.") + return + } + err := runUploadValidation(args) if err != nil { if strings.Contains(err.Error(), "no such file") { @@ -102,7 +111,7 @@ func TestApp(cmd *cobra.Command, args []string) { return } - log.Printf("[INFO] App validated successfully. Upload it with shufflecli app upload %s", args[0]) + log.Printf("[INFO] App validated successfully. Upload it with command: \n'shufflecli app upload %s'", args[0]) } // Example command: Greet the user @@ -220,32 +229,61 @@ func validatePythonfile(filepath string) error { // Clear buffers pythonCommand := fmt.Sprintf("python3 %s", copyFilepath) - log.Printf("[DEBUG] Validating python file by running '%s'", pythonCommand) - cmd = exec.Command("python3", copyFilepath) + + timeout := 3 * time.Second + log.Printf("[DEBUG] Validating python file by running '%s' for up to %d seconds.", pythonCommand, int(timeout)/1000000000) + + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() // Ensure resources are released + + // Run for maximum 5 seconds + //cmd = exec.Command("python3", copyFilepath) + cmd = exec.CommandContext(ctx, "python3", copyFilepath) cmd.Stdout = &stdoutBuffer cmd.Stderr = &stderrBuffer - err = cmd.Run() + if ctx.Err() == context.DeadlineExceeded { + fmt.Println("Command timed out") + } + if err != nil { - log.Printf("[ERROR] Local run of python file: %s", err) + if strings.Contains(err.Error(), "signal: killed") { + err = nil + } + + if err != nil { + log.Printf("[ERROR] Local run of python file: %s", err) + } stdout := stdoutBuffer.String() if len(stdout) > 0 { - //log.Printf("\n\nPython run Output: %s\n\n", stdout) + log.Printf("\n\n===== Python run (stdout) ===== \n") + for _, line := range strings.Split(stdout, "\n") { - if strings.Contains(strings.ToLower(line), "traceback") { + if strings.Contains(strings.ToLower(line), "traceback") && !strings.Contains(strings.ToLower(line), "Bad resp") { log.Printf("[ERROR] Python run Error: %s", line) } else if strings.Contains(strings.ToLower(line), "already satisfied") { continue } else { - log.Printf(line) + fmt.Println(line) } } } stderr := stderrBuffer.String() if len(stderr) > 0 { - log.Printf("\n\n===== Python run Error ===== \n%s\n\n", stderr) + log.Printf("\n\n===== Python run (stderr) ===== \n") + + for _, line := range strings.Split(stdout, "\n") { + if strings.Contains(strings.ToLower(line), "traceback") { + log.Printf("[ERROR] Python run Error: %s", line) + } 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]") { + continue + } else { + fmt.Println(line) + } + } + } return err @@ -469,6 +507,21 @@ func UploadAppFromRepo(folderpath string) error { return err } + /* + mappedValue := shuffle.RequestResponse{} + unmarshalErr := json.Unmarshal(outputBody, &mappedValue) + if unmarshalErr != nil { + log.Printf("[ERROR] Problem unmarshalling response: %s", unmarshalErr) + //return unmarshalErr + } else { + outputBody = []byte(fmt.Sprintf("Raw output: %s", mappedValue.Details)) + } + + if len(mappedValue.Details) > 0 { + log.Printf("[INFO] Upload Details: %s", mappedValue.Details) + } + */ + if resp.StatusCode != http.StatusOK { return fmt.Errorf("Bad status: %s. Raw: %s", resp.Status, string(outputBody)) } @@ -482,6 +535,11 @@ var runParameter = &cobra.Command{ Use: "run", Short: "Run a python script as if it is in the Shuffle UI", Run: func(cmd *cobra.Command, args []string) { + if len(args) <= 0 { + log.Println("[ERROR] No URL provided. Use the URL from the Shuffle UI.") + return + } + if len(apikey) <= 0 { fmt.Println("Please set the SHUFFLE_APIKEY or SHUFFLE_AUTHORIZATION environment variables to help with upload/download.") os.Exit(1) @@ -653,6 +711,11 @@ var uploadApp = &cobra.Command{ Use: "upload", Short: "Uploads and app from a directory containing the api.yaml", Run: func(cmd *cobra.Command, args []string) { + if len(args) <= 0 { + log.Println("[ERROR] No directory provided. Use the absolute path to the app directory.") + return + } + if len(apikey) <= 0 { fmt.Println("Please set the SHUFFLE_APIKEY or SHUFFLE_AUTHORIZATION environment variables to help with upload/download.") os.Exit(1)