diff --git a/cmd/yamlchk/main.go b/cmd/yamlchk/main.go index 713ef22..aeda33f 100644 --- a/cmd/yamlchk/main.go +++ b/cmd/yamlchk/main.go @@ -24,12 +24,12 @@ func main() { if len(files) == 0 { flag.Usage() - log.Fatalln("no YAML files to check") + log.Fatal("no YAML files to check") } p, err := filepath.Abs(schemaFile) if err != nil { - log.Panicln(err) + log.Panic(err) } refURL := &url.URL{Scheme: "file", Path: p} @@ -40,23 +40,24 @@ func main() { d, err := os.ReadFile(file) if err != nil { - log.Panicln(err) + log.Panic(err) } m := make(map[string]interface{}) if err := yaml.Unmarshal(d, &m); err != nil { - log.Panicln(err) + log.Panic(err) } ret, err := validator.Validate(schemaLoader, validator.NewGoLoader(m)) if err != nil { - log.Panicln(err) + log.Panic(err) } - if !ret.Valid() { - for _, err := range ret.Errors() { - log.Printf("%s\n", err) - } - log.Panicln(fmt.Sprintf("invalid file: %s", file)) + if ret.Valid() { + continue } + for _, err := range ret.Errors() { + log.Printf("%s\n", err) + } + log.Panic(fmt.Sprintf("invalid file: %s", file)) } } diff --git a/go.mod b/go.mod index b867385..601ee7e 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/anqur/yasch go 1.20 require ( + github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 github.com/invopop/jsonschema v0.7.0 github.com/xeipuuv/gojsonschema v1.2.0 gopkg.in/yaml.v3 v3.0.1 diff --git a/go.sum b/go.sum index 2c1229a..d92962c 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 h1:s6gZFSlWYmbqAuRjVTiNNhvNRfY2Wxp9nhfyel4rklc= +github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 h1:i462o439ZjprVSFSZLZxcsoAe592sZB1rci2Z8j4wdk= @@ -8,8 +10,9 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709 h1:Ko2LQMrRU+Oy/+EDBwX7eZ2jp3C47eDBB8EIhKTun+I= github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0= @@ -18,5 +21,7 @@ github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17 github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/types/type.go b/pkg/types/type.go new file mode 100644 index 0000000..56f837c --- /dev/null +++ b/pkg/types/type.go @@ -0,0 +1,54 @@ +package types + +import ( + "time" + + "github.com/alecthomas/units" + "github.com/invopop/jsonschema" +) + +type Duration struct{ Dur time.Duration } + +func (Duration) JSONSchema() *jsonschema.Schema { + return &jsonschema.Schema{ + Type: "string", + Title: "Duration", + Description: "Go-compatible duration", + } +} + +func (d Duration) UnmarshalYAML(unmarshal func(interface{}) error) error { + var s string + if err := unmarshal(&s); err != nil { + return err + } + dur, err := time.ParseDuration(s) + if err != nil { + return err + } + d.Dur = dur + return nil +} + +type Size struct{ Size units.Base2Bytes } + +func (Size) JSONSchema() *jsonschema.Schema { + return &jsonschema.Schema{ + Type: "string", + Title: "Size", + Description: "Go-compatible data size", + } +} + +func (s Size) UnmarshalYAML(unmarshal func(interface{}) error) error { + var r string + if err := unmarshal(&r); err != nil { + return err + } + size, err := units.ParseBase2Bytes(r) + if err != nil { + return err + } + s.Size = size + return nil +}