|
| 1 | +package sync |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 7 | +) |
| 8 | + |
| 9 | +func NewExprs() map[string]interface{} { |
| 10 | + return map[string]interface{}{ |
| 11 | + "GetInfoItem": func(app map[string]interface{}, name string) string { |
| 12 | + res, err := getInfoItem(app, name) |
| 13 | + if err != nil { |
| 14 | + panic(err) |
| 15 | + } |
| 16 | + return res |
| 17 | + }, |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +func getInfoItem(app map[string]interface{}, name string) (string, error) { |
| 22 | + un := unstructured.Unstructured{Object: app} |
| 23 | + operation, ok, _ := unstructured.NestedMap(app, "operation") |
| 24 | + if !ok { |
| 25 | + operation, ok, _ = unstructured.NestedMap(app, "status", "operationState", "operation") |
| 26 | + } |
| 27 | + if !ok { |
| 28 | + return "", fmt.Errorf("application '%s' has no operation", un.GetName()) |
| 29 | + } |
| 30 | + |
| 31 | + infoItems, ok := operation["info"].([]interface{}) |
| 32 | + if !ok { |
| 33 | + return "", fmt.Errorf("application '%s' has no info items", un.GetName()) |
| 34 | + } |
| 35 | + for _, infoItem := range infoItems { |
| 36 | + item, ok := infoItem.(map[string]interface{}) |
| 37 | + if !ok { |
| 38 | + continue |
| 39 | + } |
| 40 | + if item["name"] == name { |
| 41 | + res, ok := item["value"].(string) |
| 42 | + if !ok { |
| 43 | + return "", fmt.Errorf("application '%s' has invalid value of info item '%s'", un.GetName(), name) |
| 44 | + } |
| 45 | + return res, nil |
| 46 | + } |
| 47 | + } |
| 48 | + return "", fmt.Errorf("application '%s' has no info item with name '%s'", un.GetName(), name) |
| 49 | +} |
0 commit comments