diff --git a/sdk/unstructured/suite_test.go b/sdk/unstructured/suite_test.go new file mode 100644 index 0000000..d921348 --- /dev/null +++ b/sdk/unstructured/suite_test.go @@ -0,0 +1,13 @@ +package unstructured_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestSuite(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Unstructured Test Suite") +} diff --git a/sdk/unstructured/yaml.go b/sdk/unstructured/yaml.go index 5d829d5..638d019 100644 --- a/sdk/unstructured/yaml.go +++ b/sdk/unstructured/yaml.go @@ -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 } diff --git a/sdk/unstructured/yaml_test.go b/sdk/unstructured/yaml_test.go new file mode 100644 index 0000000..d6114a5 --- /dev/null +++ b/sdk/unstructured/yaml_test.go @@ -0,0 +1,105 @@ +package unstructured_test + +import ( + . "github.com/kairos-io/kairos/sdk/unstructured" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "gopkg.in/yaml.v3" +) + +var _ = Describe("unstructured", Label("unstructured-test"), func() { + var content []byte + var data map[string]interface{} + + BeforeEach(func() { + content = []byte(` +a: + b: + c: + d: 1 + e: "some_string" +`) + + data = map[string]interface{}{} + err := yaml.Unmarshal(content, &data) + Expect(err).ToNot(HaveOccurred()) + }) + + Describe("YAMLHasKey", func() { + It("returns true if the key exists in the yaml", func() { + r, err := YAMLHasKey("a.b", content) + Expect(err).ToNot(HaveOccurred()) + Expect(r).To(BeTrue()) + }) + + It("returns false if the key doesn't exist in the yaml", func() { + r, err := YAMLHasKey("a.z", content) + Expect(err).ToNot(HaveOccurred()) + Expect(r).To(BeFalse()) + }) + }) + + Describe("LookupString", func() { + When("key exists", func() { + When("key is a string", func() { + It("returns the value", func() { + r, err := LookupString(".a.b.c.e", data) + Expect(err).ToNot(HaveOccurred()) + Expect(r).To(Equal("some_string")) + }) + }) + + When("key isn't a string", func() { + It("returns an error", func() { + _, err := LookupString(".a.b.c.d", data) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(MatchRegexp("value is not a string")) + }) + }) + }) + When("key doesn't exist", func() { + It("returns an error", func() { + _, err := LookupString(".a.b.c.z", data) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(MatchRegexp("value is not a string")) + }) + }) + }) + + Describe("ReplaceValue", func() { + When("key exists", func() { + When("value is a map", func() { + It("can replace with a string", func() { + r, err := ReplaceValue(".a=\"test\"", data) + Expect(err).ToNot(HaveOccurred()) + Expect(r).To(Equal("a: test\n")) + }) + }) + When("value is a string", func() { + It("can replace with a string", func() { + r, err := ReplaceValue(".a.b.c.e=\"test\"", data) + Expect(err).ToNot(HaveOccurred()) + Expect(r).To(Equal(`a: + b: + c: + d: 1 + e: test +`)) + }) + }) + }) + When("key doesn't exist", func() { + It("creates the key", func() { + r, err := ReplaceValue(".a.b.c.z=\"test\"", data) + Expect(err).ToNot(HaveOccurred()) + Expect(r).To(Equal(`a: + b: + c: + d: 1 + e: some_string + z: test +`), r) + }) + }) + }) +})