mirror of
https://github.com/kairos-io/kairos-sdk.git
synced 2025-09-09 19:19:30 +00:00
Add helper function to unstructured (#527)
* Add helper function to unstructured to be used in: https://github.com/spectrocloud/kubesplit/tree/remove-dependencies Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me> * Add tests Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me> Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
This commit is contained in:
committed by
Itxaka
parent
b99d55b900
commit
8fbc72447e
@@ -33,12 +33,12 @@ func YAMLHasKey(query string, content []byte) (bool, error) {
|
||||
return false, err
|
||||
}
|
||||
if v == nil {
|
||||
return false, fmt.Errorf("not found")
|
||||
return false, nil
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func jq(command string, data map[string]interface{}) (map[string]interface{}, error) {
|
||||
func lookupKey(command string, data map[string]interface{}) (interface{}, error) {
|
||||
query, err := gojq.Parse(command)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -53,6 +53,47 @@ func jq(command string, data map[string]interface{}) (map[string]interface{}, er
|
||||
if !ok {
|
||||
return nil, errors.New("failed getting result from gojq")
|
||||
}
|
||||
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func LookupString(command string, data map[string]interface{}) (string, error) {
|
||||
v, err := lookupKey(command, data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if t, ok := v.(string); ok {
|
||||
return t, nil
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("value is not a string: %T", v)
|
||||
}
|
||||
|
||||
func ReplaceValue(command string, data map[string]interface{}) (string, error) {
|
||||
v, err := lookupKey(command, data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if _, ok := v.(map[string]interface{}); ok {
|
||||
b, err := yaml.Marshal(v)
|
||||
return string(b), err
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("unexpected type: %T", v)
|
||||
}
|
||||
|
||||
// func ReplaceKeyValue(command string, data interface{}) (string, error) {
|
||||
// LookupString(commd
|
||||
// }
|
||||
|
||||
func jq(command string, data map[string]interface{}) (map[string]interface{}, error) {
|
||||
v, err := lookupKey(command, data)
|
||||
if err != nil {
|
||||
return make(map[string]interface{}), err
|
||||
}
|
||||
|
||||
if err, ok := v.(error); ok {
|
||||
return nil, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user