-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze.go
90 lines (71 loc) · 2.39 KB
/
analyze.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"log"
"fmt"
"io/ioutil"
"os"
"strings"
"gopkg.in/yaml.v3"
"github.com/shuffle/shuffle-shared"
)
// VerifyFolder checks a single folder for required files and structure
func VerifyFolder(folderPath string) error {
// Check that folder exists and is a directory
info, err := os.Stat(folderPath)
if err != nil || !info.IsDir() {
return fmt.Errorf("invalid folder: %w", err)
}
// File paths for api.yaml and app.py
apiFilePath := fmt.Sprintf("%s/api.yaml", folderPath)
pythonFilePath := fmt.Sprintf("%s/src/app.py", folderPath)
// Validate api.yaml contents
apiData, err := parseAPIYaml(apiFilePath)
if err != nil {
return fmt.Errorf("error parsing API YAML in %s: %v", apiFilePath, err)
}
// Check for discrepancies in name and version
if !strings.EqualFold(apiData.Name, folderPath) {
log.Printf("[ERROR] Bad name: '%s' vs '%s'\n", folderPath, apiData.Name)
}
if apiData.AppVersion != folderPath {
log.Printf("[ERROR] Bad version in %s: expected %s, found %s\n", folderPath, apiData.AppVersion, folderPath)
}
// Check unsupported large_image format
if strings.Contains(apiData.LargeImage, "svg") {
log.Printf("[ERROR] Unsupported large_image format in %s: svg\n", apiFilePath)
}
// Validate actions in app.py
if err := checkActionsInPython(apiData.Actions, pythonFilePath); err != nil {
log.Printf("[ERROR] Problem with python check: %s", err)
}
return nil
}
// parseAPIYaml loads the API data from api.yaml
func parseAPIYaml(apiFilePath string) (*shuffle.WorkflowApp, error) {
data, err := ioutil.ReadFile(apiFilePath)
if err != nil {
return nil, err
}
var apiData shuffle.WorkflowApp
if err := yaml.Unmarshal(data, &apiData); err != nil {
return nil, fmt.Errorf("YAML parsing error: %w", err)
}
return &apiData, nil
}
// checkActionsInPython verifies each action from api.yaml exists in app.py
func checkActionsInPython(actions []shuffle.WorkflowAppAction, pythonFilePath string) error {
pythonData, err := ioutil.ReadFile(pythonFilePath)
if err != nil {
return fmt.Errorf("Error reading Python file %s: %w", pythonFilePath, err)
}
missingActions := []string{}
for _, action := range actions {
if !strings.Contains(string(pythonData), action.Name) {
missingActions = append(missingActions, action.Name)
}
}
if len(missingActions) > 0 {
return fmt.Errorf("Missing actions in %s: %v", pythonFilePath, missingActions)
}
return nil
}