Use TextVar instead

This commit is contained in:
Yann Hamon 2023-07-09 14:33:40 +02:00
parent a819ea12ad
commit 0927e3b717
3 changed files with 21 additions and 10 deletions

View file

@ -8,20 +8,20 @@ local-test:
go test -race ./...
local-build:
go build -o bin/ ./...
go build -buildvcs=false -o bin/ ./...
local-build-static:
CGO_ENABLED=0 GOFLAGS=-mod=vendor GOOS=linux GOARCH=amd64 GO111MODULE=on go build -trimpath -tags=netgo -ldflags "-extldflags=\"-static\"" -a -o bin/ ./...
# These only used for development. Release artifacts and docker images are produced by goreleaser.
docker-test:
docker run -t -v $$PWD:/go/src/github.com/yannh/kubeconform -w /go/src/github.com/yannh/kubeconform golang:1.17 make local-test
docker run -t -v $$PWD:/go/src/github.com/yannh/kubeconform -w /go/src/github.com/yannh/kubeconform golang:1.20 make local-test
docker-build:
docker run -t -v $$PWD:/go/src/github.com/yannh/kubeconform -w /go/src/github.com/yannh/kubeconform golang:1.17 make local-build
docker run -t -v $$PWD:/go/src/github.com/yannh/kubeconform -w /go/src/github.com/yannh/kubeconform golang:1.20 make local-build
docker-build-static:
docker run -t -v $$PWD:/go/src/github.com/yannh/kubeconform -w /go/src/github.com/yannh/kubeconform golang:1.17 make local-build-static
docker run -t -v $$PWD:/go/src/github.com/yannh/kubeconform -w /go/src/github.com/yannh/kubeconform golang:1.20 make local-build-static
build-bats:
docker build -t bats -f Dockerfile.bats .

View file

@ -180,6 +180,13 @@ resetCacheFolder() {
[ "$status" -eq 1 ]
}
@test "Fail early when passing a non valid -kubernetes-version" {
run bin/kubeconform -kubernetes-version 1.25 fixtures/valid.yaml
[[ "$output" == "invalid value "1.25" for flag -kubernetes-version: 1.25 is not a valid version. Valid values are 'master' (default) or full version x.y.z (e.g. '1.27.2')"* ]]
[[ `echo "$output" | wc -l` -eq 1 ]]
[ "$status" -eq 1 ]
}
@test "Pass with a valid input when validating against openshift manifests" {
run bin/kubeconform -kubernetes-version 3.8.0 -schema-location 'https://raw.githubusercontent.com/garethr/openshift-json-schema/master/{{ .NormalizedKubernetesVersion }}-standalone{{ .StrictSuffix }}/{{ .ResourceKind }}.json' -summary fixtures/valid.yaml
[ "$status" -eq 0 ]

View file

@ -46,11 +46,15 @@ func (kv *k8sVersionValue) String() string {
return string(*kv)
}
func (kv *k8sVersionValue) Set(value string) error {
if ok, _ := regexp.MatchString(`^(master|\d+\.\d+\.\d+)$`, value); ok != true {
return fmt.Errorf("%v is not a valid version. Valid values are 'master' (default) or full version x.y.z (e.g. '1.27.2')", value)
func (kv k8sVersionValue) MarshalText() ([]byte, error) {
return []byte(kv), nil
}
func (kv *k8sVersionValue) UnmarshalText(v []byte) error {
if ok, _ := regexp.MatchString(`^(master|\d+\.\d+\.\d+)$`, string(v)); ok != true {
return fmt.Errorf("%v is not a valid version. Valid values are 'master' (default) or full version x.y.z (e.g. '1.27.2')", kv.String())
}
*kv = k8sVersionValue(value)
*kv = k8sVersionValue(v)
return nil
}
@ -75,10 +79,10 @@ func FromFlags(progName string, args []string) (Config, string, error) {
var buf bytes.Buffer
flags.SetOutput(&buf)
c := Config{KubernetesVersion: "master"}
c := Config{}
c.Files = []string{}
flags.Var(&c.KubernetesVersion, "kubernetes-version", "version of Kubernetes to validate against, e.g.: 1.18.0")
flags.TextVar(&c.KubernetesVersion, "kubernetes-version", k8sVersionValue("master"), "version of Kubernetes to validate against, e.g.: 1.18.0")
flags.Var(&schemaLocationsParam, "schema-location", "override schemas location search path (can be specified multiple times)")
flags.StringVar(&skipKindsCSV, "skip", "", "comma-separated list of kinds or GVKs to ignore")
flags.StringVar(&rejectKindsCSV, "reject", "", "comma-separated list of kinds or GVKs to reject")