Skip to content

Commit

Permalink
Upload validation is improved :)
Browse files Browse the repository at this point in the history
  • Loading branch information
frikky committed Jan 22, 2025
1 parent c999de4 commit 2d1d37f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 11 deletions.
11 changes: 11 additions & 0 deletions analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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") {
Expand Down
85 changes: 74 additions & 11 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)


Expand Down Expand Up @@ -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") {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2d1d37f

Please sign in to comment.