Skip to content

Commit

Permalink
feat(roboll#344): add path: to make sub-helmfiles clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
sgandon committed May 4, 2019
1 parent 444d9d7 commit 5d54b18
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 37 deletions.
33 changes: 26 additions & 7 deletions state/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package state
import (
"fmt"
. "gotest.tools/assert"
"gotest.tools/assert/cmp"
"path/filepath"
"reflect"
"testing"
Expand Down Expand Up @@ -269,13 +270,23 @@ func TestReadFromYaml_Helmfiles_Selectors(t *testing.T) {
selectors: {}
- inherits/selector.yaml:
selectors: inherits
- path: path/prefix/selector.yaml
selectors:
- name=zorba
- path: path/prefix/empty/selector.yaml
selectors: {}
- path: path/prefix/inherits/selector.yaml
selectors: inherits
`),
wantErr: false,
helmfiles: []SubHelmfileSpec{{Path: "simple/helmfile.yaml"},
{Path: "simple/helmfile/with/semicolon.yaml"},
{Path: "two/selectors.yaml", Selectors: []string{"name=foo", "name=bar"}},
{Path: "empty/selector.yaml", Selectors: []string{}},
{Path: "inherits/selector.yaml", Inherits: true},
{Path: "simple/helmfile/with/semicolon.yaml", Selectors: nil, Inherits: false},
{Path: "two/selectors.yaml", Selectors: []string{"name=foo", "name=bar"}, Inherits: false},
{Path: "empty/selector.yaml", Selectors: []string{}, Inherits: false},
{Path: "inherits/selector.yaml", Selectors: nil, Inherits: true},
{Path: "path/prefix/selector.yaml", Selectors: []string{"name=zorba"}, Inherits: false},
{Path: "path/prefix/empty/selector.yaml", Selectors: []string{}, Inherits: false},
{Path: "path/prefix/inherits/selector.yaml", Selectors: nil, Inherits: true},
},
},
{
Expand All @@ -297,24 +308,32 @@ func TestReadFromYaml_Helmfiles_Selectors(t *testing.T) {
path: "failing3/selector",
content: []byte(`helmfiles:
- failing3/helmfile.yaml:
selector: foo
selectors: foo
`),
wantErr: true,
},
{
path: "failing4/selector",
content: []byte(`helmfiles:
- failing4/helmfile.yaml:
selector:
selectors:
`),
wantErr: true,
},
{
path: "failing4/selector",
content: []byte(`helmfiles:
- failing4/helmfile.yaml:
selector:
selectors:
- colon: not-authorized
`),
wantErr: true,
},
{
path: "failing5/selector",
content: []byte(`helmfiles:
- selectors:
- colon: not-authorized
`),
wantErr: true,
},
Expand Down
76 changes: 46 additions & 30 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1415,13 +1415,24 @@ func (hf *SubHelmfileSpec) UnmarshalYAML(unmarshal func(interface{}) error) erro
switch key := k.(type) {
case string:
//get the path
hf.Path = key
//get the selectors if something is specified
if v != nil {
if err := extractSelector(hf, v); err != nil {
if key == "path" {
hf.Path = v.(string)
} else if key == "selectors" {
if hf.Path == "" {
return fmt.Errorf("Can't use a 'selector' without an associated path'")
} //else get selector content
if err := extractSelectorContent(hf, v); err != nil {
return err
}
} //else it is a path with ending semi-colon without anything else below and it is fine
} else {
hf.Path = key
//get the selectors if something is specified
if v != nil { //we have a path, now compute the selector
if err := extractSelector(hf, v); err != nil {
return err
}
} //else it is a path with ending semi-colon without anything else below and it is fine
}
default:
return fmt.Errorf("Expecting a \"string\" scalar for the helmfile collection but got: %v", key)
}
Expand All @@ -1440,31 +1451,7 @@ func extractSelector(hf *SubHelmfileSpec, value interface{}) error {
switch key := k.(type) {
case string:
if key == "selectors" {
switch selectors := v.(type) {
case string: //check the string is `inherits` else error
if selectors == InheritsYamlValue {
hf.Inherits = true
} else {
return fmt.Errorf("Expecting a list of string, an empty {} or '%v' but got: %v", InheritsYamlValue, selectors)
}
case []interface{}: //expect an array if strings
for _, sel := range selectors {
switch selValue := sel.(type) {
case string:
hf.Selectors = append(hf.Selectors, selValue)
default:
return fmt.Errorf("Expecting a string, but got: %v", selValue)
}
}
case map[interface{}]interface{}:
if len(selectors) == 0 {
hf.Selectors = make([]string, 0) //allocate and non nil empty array
} else { //unexpected unempty map so error
return fmt.Errorf("unexpected unempty map in selector [-%v] but got: %v", hf.Path, selectors)
}
default:
return fmt.Errorf("Expecting list of strings or and empty {} mapping [-%v] but got: [%v] of type [%T] ", hf.Path, selectors, selectors)
}
return extractSelectorContent(hf, v)
} else { //not 'selectors' so error
return fmt.Errorf("Expecting a \"selectors\" mapping but got: %v", key)
}
Expand All @@ -1477,3 +1464,32 @@ func extractSelector(hf *SubHelmfileSpec, value interface{}) error {
}
return nil
}

func extractSelectorContent(hf *SubHelmfileSpec, value interface{}) error {
switch selectors := value.(type) {
case string: //check the string is `inherits` else error
if selectors == InheritsYamlValue {
hf.Inherits = true
} else {
return fmt.Errorf("Expecting a list of string, an empty {} or '%v' but got: %v", InheritsYamlValue, selectors)
}
case []interface{}: //expect an array if strings
for _, sel := range selectors {
switch selValue := sel.(type) {
case string:
hf.Selectors = append(hf.Selectors, selValue)
default:
return fmt.Errorf("Expecting a string, but got: %v", selValue)
}
}
case map[interface{}]interface{}:
if len(selectors) == 0 {
hf.Selectors = make([]string, 0) //allocate and non nil empty array
} else { //unexpected unempty map so error
return fmt.Errorf("unexpected unempty map in selector [-%v] but got: %v", hf.Path, selectors)
}
default:
return fmt.Errorf("Expecting list of strings or and empty {} mapping [-%v] but got: [%v] of type [%T] ", hf.Path, selectors, selectors)
}
return nil
}

0 comments on commit 5d54b18

Please sign in to comment.