diff --git a/internal/pkg/gitops/version.go b/internal/pkg/gitops/version.go index c21403f..9e38b48 100644 --- a/internal/pkg/gitops/version.go +++ b/internal/pkg/gitops/version.go @@ -49,14 +49,14 @@ func unwrapYaml(yaml map[string]interface{}, notion string) (string, error) { if r.MatchString(k) { idx := r.FindString(k)[:1] - if len(idx) > 0 { + if len(idx) >= 0 { i, err := strconv.ParseInt(idx, 10, 8) if err != nil { return "", err } - k = k[:len(k)-int(i)] + k = k[:len(k)-len(fmt.Sprintf("[%d]", i))] if a, ok := d[k].([]interface{}); ok { if b, ok := a[i].(map[string]interface{}); ok { diff --git a/internal/pkg/gitops/version_test.go b/internal/pkg/gitops/version_test.go index bccf74a..0c54c50 100644 --- a/internal/pkg/gitops/version_test.go +++ b/internal/pkg/gitops/version_test.go @@ -30,7 +30,7 @@ func TestReadCurrentVersion(t *testing.T) { { name: "invalid yaml", args: args{ - f: []byte("wibble&&3..\nfff"), + f: []byte("wibble&&3..\nfff"), notation: "image.tag", }, want: "", @@ -39,7 +39,7 @@ func TestReadCurrentVersion(t *testing.T) { { name: "simple yaml", args: args{ - f: createSimpleYaml("v1.2.99"), + f: createSimpleYaml("v1.2.99"), notation: "image.tag", }, want: "v1.2.99", @@ -48,7 +48,7 @@ func TestReadCurrentVersion(t *testing.T) { { name: "invalid notation to yaml", args: args{ - f: createSimpleYaml("v1.2.99"), + f: createSimpleYaml("v1.2.99"), notation: "image.nope", }, want: "", @@ -57,12 +57,21 @@ func TestReadCurrentVersion(t *testing.T) { { name: "array notation", args: args{ - f: createSimpleArrayYaml("v1.9"), + f: createSimpleArrayYaml("v1.9"), notation: "images[3].tag", }, want: "v1.9", wantErr: false, }, + { + name: "helm example", + args: args{ + f: createHelmYaml("v1.9"), + notation: "dependencies[0].version", + }, + want: "v1.9", + wantErr: false, + }, } fmt.Println(string(createSimpleArrayYaml("v1.2.99"))) @@ -106,7 +115,30 @@ func createSimpleArrayYaml(t string) []byte { marshal, err := yaml.Marshal(&struct { Images []tag `yaml:"images"` }{ - Images: []tag{{Tag: "v1"},{Tag: "v2"},{Tag: "v3"},{Tag: t}}, + Images: []tag{{Tag: "v1"}, {Tag: "v2"}, {Tag: "v3"}, {Tag: t}}, + }) + if err != nil { + return nil + } + + return marshal +} + +func createHelmYaml(t string) []byte { + type dependencies struct { + Name string `yaml:"name"` + Version string `yaml:"version"` + Repository string `yaml:"repository"` + } + + marshal, err := yaml.Marshal(&struct { + Dependencies []dependencies `yaml:"dependencies"` + }{ + Dependencies: []dependencies{{ + Name: "example-test", + Version: t, + Repository: "gsdevme/test", + }}, }) if err != nil { return nil