kairos-sdk/sdk/unstructured/yaml.go
Ettore Di Giacinto b99d55b900 seedling: Glob all configs found (#502)
* 🌱 Glob all configs found

This allows to read all the configs found during scanning - this is especially useful in first boot as it allows
to have separate config file logic split into several files

Signed-off-by: Ettore Di Giacinto <mudler@users.noreply.github.com>

* 🌱 Simplify config merge logic

Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>

* WIP

Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>

* 🌱 Make the function work

Signed-off-by: Ettore Di Giacinto <mudler@users.noreply.github.com>
Signed-off-by: mudler <mudler@kairos.io>

Signed-off-by: Ettore Di Giacinto <mudler@users.noreply.github.com>
Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
Signed-off-by: mudler <mudler@kairos.io>
Co-authored-by: Dimitris Karakasilis <dimitris@karakasilis.me>
2022-11-28 15:37:45 +01:00

102 lines
2.0 KiB
Go

package unstructured
import (
"errors"
"fmt"
"github.com/hashicorp/go-multierror"
"github.com/itchyny/gojq"
"gopkg.in/yaml.v3"
)
func YAMLHasKey(query string, content []byte) (bool, error) {
data := map[string]interface{}{}
err := yaml.Unmarshal([]byte(content), &data)
if err != nil {
return false, err
}
c, err := gojq.Parse(fmt.Sprintf(".%s | ..", query))
if err != nil {
return false, err
}
code, err := gojq.Compile(c)
if err != nil {
return false, err
}
iter := code.Run(data)
v, _ := iter.Next()
if err, ok := v.(error); ok {
return false, err
}
if v == nil {
return false, fmt.Errorf("not found")
}
return true, nil
}
func jq(command string, data map[string]interface{}) (map[string]interface{}, error) {
query, err := gojq.Parse(command)
if err != nil {
return nil, err
}
code, err := gojq.Compile(query)
if err != nil {
return nil, err
}
iter := code.Run(data)
v, ok := iter.Next()
if !ok {
return nil, errors.New("failed getting result from gojq")
}
if err, ok := v.(error); ok {
return nil, err
}
if t, ok := v.(map[string]interface{}); ok {
return t, nil
}
return make(map[string]interface{}), nil
}
func ToYAML(v map[string]interface{}) ([]byte, error) {
data := map[string]interface{}{}
var errs error
for k, value := range v {
tmpl := ".%s=\"%s\""
// support boolean types
if value == "true" || value == "false" {
tmpl = ".%s=%s"
}
newData, err := jq(fmt.Sprintf(tmpl, k, value), data)
if err != nil {
errs = multierror.Append(errs, err)
continue
}
data = newData
}
out, err := yaml.Marshal(&data)
if err != nil {
errs = multierror.Append(errs, err)
}
return out, errs
}
// ToYAMLMap turns a map string interface which describes a yaml file in 'dot.yaml' format to a fully deep marshalled yaml.
func ToYAMLMap(v map[string]interface{}) (map[string]interface{}, error) {
result := map[string]interface{}{}
tempData, err := ToYAML(v)
if err != nil {
return result, err
}
err = yaml.Unmarshal(tempData, &result)
return result, err
}