feat: implement app-json-is-valid plugin trigger

This commit is contained in:
Jose Diaz-Gonzalez
2025-06-19 02:19:08 -04:00
parent ed851fe399
commit 4ae9cca6f1
9 changed files with 87 additions and 49 deletions

View File

@@ -200,21 +200,9 @@ func GetAppJSON(appName string) (AppJSON, error) {
return AppJSON{}, nil
}
b, err := os.ReadFile(getProcessSpecificAppJSONPath(appName))
if err != nil {
return AppJSON{}, fmt.Errorf("Cannot read app.json file: %v", err)
}
appJSONPath := getProcessSpecificAppJSONPath(appName)
if strings.TrimSpace(string(b)) == "" {
return AppJSON{}, nil
}
var appJSON AppJSON
if err = json.Unmarshal(b, &appJSON); err != nil {
return AppJSON{}, fmt.Errorf("Cannot parse app.json: %v", err)
}
return appJSON, nil
return ReadAppJSON(appJSONPath)
}
func GetAutoscalingConfig(appName string, processType string, replicas int) (FormationAutoscaling, bool, error) {
@@ -256,3 +244,22 @@ func GetAutoscalingConfig(appName string, processType string, replicas int) (For
return autoscaling, true, nil
}
// ReadAppJSON reads an app.json file from a given path
func ReadAppJSON(path string) (AppJSON, error) {
b, err := os.ReadFile(path)
if err != nil {
return AppJSON{}, fmt.Errorf("Cannot read app.json file: %v", err)
}
if strings.TrimSpace(string(b)) == "" {
return AppJSON{}, nil
}
var appJSON AppJSON
if err = json.Unmarshal(b, &appJSON); err != nil {
return AppJSON{}, fmt.Errorf("Cannot parse app.json: %v", err)
}
return appJSON, nil
}

View File

@@ -1,7 +1,7 @@
package appjson
import (
"encoding/json"
"errors"
"fmt"
"os"
"path"
@@ -142,31 +142,16 @@ func TriggerCorePostExtract(appName string, sourceWorkDir string) error {
}
if hasAppJSON(appName) {
b, err := os.ReadFile(processSpecificAppJSON)
result, err := common.CallPlugnTrigger(common.PlugnTriggerInput{
Trigger: "app-json-is-valid",
Args: []string{appName, processSpecificAppJSON},
})
if err != nil {
return err
}
content := strings.TrimSpace(string(b))
if content == "" {
return nil
}
var appJSON AppJSON
if err := json.Unmarshal([]byte(content), &appJSON); err != nil {
return fmt.Errorf("Unable to unmarshal app.json: %v", err.Error())
}
if appJSON.Cron != nil {
for i, c := range appJSON.Cron {
if c.Command == "" {
return fmt.Errorf("Missing cron command for app %s (index %d)", appName, i)
}
if c.Schedule == "" {
return fmt.Errorf("Missing cron schedule for app %s (index %d)", appName, i)
}
if result.StderrContents() != "" {
return errors.New(result.StderrContents())
}
return err
}
}