mirror of
https://github.com/kairos-io/kairos-sdk.git
synced 2025-09-03 16:24:57 +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
13
sdk/unstructured/suite_test.go
Normal file
13
sdk/unstructured/suite_test.go
Normal file
@@ -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")
|
||||
}
|
@@ -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
|
||||
}
|
||||
|
105
sdk/unstructured/yaml_test.go
Normal file
105
sdk/unstructured/yaml_test.go
Normal file
@@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user