Skip to content

Commit

Permalink
🐛 Fix agent not able to get subkeys (#1299)
Browse files Browse the repository at this point in the history
Signed-off-by: Itxaka <[email protected]>
  • Loading branch information
Itxaka authored Apr 15, 2023
1 parent 40075e8 commit 3980bf1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
16 changes: 14 additions & 2 deletions pkg/config/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package collector

import (
"encoding/json"
"fmt"
"io"
"net/http"
Expand All @@ -17,7 +18,7 @@ import (
"github.com/avast/retry-go"
"github.com/imdario/mergo"
"github.com/itchyny/gojq"
"gopkg.in/yaml.v1"
"gopkg.in/yaml.v3"
)

const DefaultHeader = "#cloud-config"
Expand Down Expand Up @@ -290,12 +291,23 @@ func (c Config) Query(s string) (res string, err error) {
s = fmt.Sprintf(".%s", s)

var dat map[string]interface{}
var dat1 map[string]interface{}

yamlStr, err := c.String()
if err != nil {
panic(err)
}
if err := yaml.Unmarshal([]byte(yamlStr), &dat); err != nil {
// Marshall it so it removes the first line which cannot be parsed
err = yaml.Unmarshal([]byte(yamlStr), &dat1)
if err != nil {
panic(err)
}
// Transform it to json so its parsed correctly by gojq
b, err := json.Marshal(dat1)
if err != nil {
panic(err)
}
if err := json.Unmarshal(b, &dat); err != nil {
panic(err)
}

Expand Down
11 changes: 7 additions & 4 deletions pkg/config/collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,12 @@ options:
k = (*c)["remote_key_4"].(string)
Expect(k).To(Equal("remote_value_4"))

options := (*c)["options"].(map[interface{}]interface{})
options := (*c)["options"].(Config)
Expect(options["foo"]).To(Equal("bar"))
Expect(options["remote_option_1"]).To(Equal("remote_option_value_1"))
Expect(options["remote_option_2"]).To(Equal("remote_option_value_2"))

player := (*c)["player"].(map[interface{}]interface{})
player := (*c)["player"].(Config)
Expect(player["name"]).NotTo(Equal("Toad"))
Expect(player["surname"]).To(Equal("Bros"))
})
Expand Down Expand Up @@ -436,8 +436,11 @@ some:
Expect(v).To(Equal("local_value_1\n"))
v, err = c.Query("some")
Expect(err).ToNot(HaveOccurred())
// TODO: there's a bug when trying to dig some.other.key, so making the test pass this way for now, since that was not tested before
Expect(v).To(Equal("other:\n key: 3\n"))
Expect(v).To(Equal("other:\n key: 3\n"))
v, err = c.Query("some.other")
Expect(v).To(Equal("key: 3\n"))
v, err = c.Query("some.other.key")
Expect(v).To(Equal("3\n"))
Expect(c.Query("options")).To(Equal("foo: bar\n"))
})
})
Expand Down

0 comments on commit 3980bf1

Please sign in to comment.