Make resource selector args case insensitive

So that `<kind>/<name>` flags can be supplied as:

* `secret/foo`
* `Secret/foo`
* `SeCrEt/foo`

But result in: `Secret/foo`.

Signed-off-by: Hidde Beydals <hello@hidde.co>
pull/605/head
Hidde Beydals 4 years ago
parent 567acb6291
commit 1b8e980519

@ -20,8 +20,9 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/fluxcd/flux2/internal/utils"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1" sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
"github.com/fluxcd/flux2/internal/utils"
) )
var supportedHelmChartSourceKinds = []string{sourcev1.HelmRepositoryKind, sourcev1.GitRepositoryKind, sourcev1.BucketKind} var supportedHelmChartSourceKinds = []string{sourcev1.HelmRepositoryKind, sourcev1.GitRepositoryKind, sourcev1.BucketKind}
@ -48,13 +49,14 @@ func (s *HelmChartSource) Set(str string) error {
if sourceKind == "" || sourceName == "" { if sourceKind == "" || sourceName == "" {
return fmt.Errorf("invalid helm chart source '%s', must be in format <kind>/<name>", str) return fmt.Errorf("invalid helm chart source '%s', must be in format <kind>/<name>", str)
} }
if !utils.ContainsItemString(supportedHelmChartSourceKinds, sourceKind) { cleanSourceKind, ok := utils.ContainsEqualFoldItemString(supportedHelmChartSourceKinds, sourceKind)
if !ok {
return fmt.Errorf("source kind '%s' is not supported, must be one of: %s", return fmt.Errorf("source kind '%s' is not supported, must be one of: %s",
sourceKind, strings.Join(supportedHelmChartSourceKinds, ", ")) sourceKind, strings.Join(supportedHelmChartSourceKinds, ", "))
} }
s.Name = sourceName s.Name = sourceName
s.Kind = sourceKind s.Kind = cleanSourceKind
return nil return nil
} }

@ -31,6 +31,7 @@ func TestHelmChartSource_Set(t *testing.T) {
expectErr bool expectErr bool
}{ }{
{"supported", fmt.Sprintf("%s/foo", sourcev1.HelmRepositoryKind), fmt.Sprintf("%s/foo", sourcev1.HelmRepositoryKind), false}, {"supported", fmt.Sprintf("%s/foo", sourcev1.HelmRepositoryKind), fmt.Sprintf("%s/foo", sourcev1.HelmRepositoryKind), false},
{"lower case kind", "helmrepository/foo", fmt.Sprintf("%s/foo", sourcev1.HelmRepositoryKind), false},
{"unsupported", "Unsupported/kind", "", true}, {"unsupported", "Unsupported/kind", "", true},
{"invalid format", sourcev1.HelmRepositoryKind, "", true}, {"invalid format", sourcev1.HelmRepositoryKind, "", true},
{"missing name", fmt.Sprintf("%s/", sourcev1.HelmRepositoryKind), "", true}, {"missing name", fmt.Sprintf("%s/", sourcev1.HelmRepositoryKind), "", true},

@ -47,13 +47,14 @@ func (v *HelmReleaseValuesFrom) Set(str string) error {
if sourceKind == "" { if sourceKind == "" {
return fmt.Errorf("invalid Kubernetes object reference '%s', must be in format <kind>/<name>", str) return fmt.Errorf("invalid Kubernetes object reference '%s', must be in format <kind>/<name>", str)
} }
if !utils.ContainsItemString(supportedHelmReleaseValuesFromKinds, sourceKind) { cleanSourceKind, ok := utils.ContainsEqualFoldItemString(supportedHelmReleaseValuesFromKinds, sourceKind)
if !ok {
return fmt.Errorf("reference kind '%s' is not supported, must be one of: %s", return fmt.Errorf("reference kind '%s' is not supported, must be one of: %s",
sourceKind, strings.Join(supportedHelmReleaseValuesFromKinds, ", ")) sourceKind, strings.Join(supportedHelmReleaseValuesFromKinds, ", "))
} }
v.Name = sourceName v.Name = sourceName
v.Kind = sourceKind v.Kind = cleanSourceKind
return nil return nil
} }

@ -28,6 +28,7 @@ func TestHelmReleaseValuesFrom_Set(t *testing.T) {
expectErr bool expectErr bool
}{ }{
{"supported", "Secret/foo", "Secret/foo", false}, {"supported", "Secret/foo", "Secret/foo", false},
{"lower case kind", "secret/foo", "Secret/foo", false},
{"unsupported", "Unsupported/kind", "", true}, {"unsupported", "Unsupported/kind", "", true},
{"invalid format", "Secret", "", true}, {"invalid format", "Secret", "", true},
{"empty", "", "", true}, {"empty", "", "", true},

@ -20,8 +20,9 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/fluxcd/flux2/internal/utils"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1" sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
"github.com/fluxcd/flux2/internal/utils"
) )
var supportedKustomizationSourceKinds = []string{sourcev1.GitRepositoryKind, sourcev1.BucketKind} var supportedKustomizationSourceKinds = []string{sourcev1.GitRepositoryKind, sourcev1.BucketKind}
@ -54,13 +55,14 @@ func (s *KustomizationSource) Set(str string) error {
} }
sourceKind = sourcev1.GitRepositoryKind sourceKind = sourcev1.GitRepositoryKind
} }
if !utils.ContainsItemString(supportedKustomizationSourceKinds, sourceKind) { cleanSourceKind, ok := utils.ContainsEqualFoldItemString(supportedKustomizationSourceKinds, sourceKind)
if !ok {
return fmt.Errorf("source kind '%s' is not supported, must be one of: %s", return fmt.Errorf("source kind '%s' is not supported, must be one of: %s",
sourceKind, strings.Join(supportedKustomizationSourceKinds, ", ")) sourceKind, strings.Join(supportedKustomizationSourceKinds, ", "))
} }
s.Name = sourceName s.Name = sourceName
s.Kind = sourceKind s.Kind = cleanSourceKind
return nil return nil
} }

@ -32,6 +32,7 @@ func TestKustomizationSource_Set(t *testing.T) {
}{ }{
{"supported", fmt.Sprintf("%s/foo", sourcev1.GitRepositoryKind), fmt.Sprintf("%s/foo", sourcev1.GitRepositoryKind), false}, {"supported", fmt.Sprintf("%s/foo", sourcev1.GitRepositoryKind), fmt.Sprintf("%s/foo", sourcev1.GitRepositoryKind), false},
{"default kind", "foo", fmt.Sprintf("%s/foo", sourcev1.GitRepositoryKind), false}, {"default kind", "foo", fmt.Sprintf("%s/foo", sourcev1.GitRepositoryKind), false},
{"lower case kind", "gitrepository/foo", fmt.Sprintf("%s/foo", sourcev1.GitRepositoryKind), false},
{"unsupported", "Unsupported/kind", "", true}, {"unsupported", "Unsupported/kind", "", true},
{"missing name", fmt.Sprintf("%s/", sourcev1.GitRepositoryKind), "", true}, {"missing name", fmt.Sprintf("%s/", sourcev1.GitRepositoryKind), "", true},
{"empty", "", "", true}, {"empty", "", "", true},

@ -230,6 +230,15 @@ func ContainsItemString(s []string, e string) bool {
return false return false
} }
func ContainsEqualFoldItemString(s []string, e string) (string, bool) {
for _, a := range s {
if strings.EqualFold(a, e) {
return a, true
}
}
return "", false
}
func ParseObjectKindName(input string) (string, string) { func ParseObjectKindName(input string) (string, string) {
kind := "" kind := ""
name := input name := input

Loading…
Cancel
Save