diff --git a/.github/README.md b/.github/README.md new file mode 100644 index 0000000..830f5b3 --- /dev/null +++ b/.github/README.md @@ -0,0 +1,10 @@ +# yasch + +Some YAML utilities, including: + +* Converts any Go structs into a JSON Schema file +* A command line tool `yamlchk` for validating YAML files with a local JSON Schema file + +## License + +MIT diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..662c840 --- /dev/null +++ b/.gitignore @@ -0,0 +1,23 @@ +.DS_Store +/.idea/ +/.vscode/ + +### Go ### +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6c70264 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 ANQUR + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/cmd/yamlchk/main.go b/cmd/yamlchk/main.go new file mode 100644 index 0000000..713ef22 --- /dev/null +++ b/cmd/yamlchk/main.go @@ -0,0 +1,62 @@ +// Package main checks YAML files for a given JSON schema. +package main + +import ( + "flag" + "fmt" + "log" + "net/url" + "os" + "path/filepath" + + validator "github.com/xeipuuv/gojsonschema" + "gopkg.in/yaml.v3" +) + +func main() { + var ( + schemaFile string + files []string + ) + flag.StringVar(&schemaFile, "s", "", "schema file path") + flag.Parse() + files = flag.Args() + + if len(files) == 0 { + flag.Usage() + log.Fatalln("no YAML files to check") + } + + p, err := filepath.Abs(schemaFile) + if err != nil { + log.Panicln(err) + } + + refURL := &url.URL{Scheme: "file", Path: p} + schemaLoader := validator.NewReferenceLoader(refURL.String()) + + for _, file := range files { + log.Println("checking", file) + + d, err := os.ReadFile(file) + if err != nil { + log.Panicln(err) + } + + m := make(map[string]interface{}) + if err := yaml.Unmarshal(d, &m); err != nil { + log.Panicln(err) + } + + ret, err := validator.Validate(schemaLoader, validator.NewGoLoader(m)) + if err != nil { + log.Panicln(err) + } + if !ret.Valid() { + for _, err := range ret.Errors() { + log.Printf("%s\n", err) + } + log.Panicln(fmt.Sprintf("invalid file: %s", file)) + } + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..96aea95 --- /dev/null +++ b/go.mod @@ -0,0 +1,15 @@ +module github.com/anqur/yasch + +go 1.20 + +require ( + github.com/alecthomas/jsonschema v0.0.0-20220216202328-9eeeec9d044b + github.com/xeipuuv/gojsonschema v1.2.0 + gopkg.in/yaml.v3 v3.0.1 +) + +require ( + github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 // indirect + github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect + github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ad576e3 --- /dev/null +++ b/go.sum @@ -0,0 +1,22 @@ +github.com/alecthomas/jsonschema v0.0.0-20220216202328-9eeeec9d044b h1:doCpXjVwui6HUN+xgNsNS3SZ0/jUZ68Eb+mJRNOZfog= +github.com/alecthomas/jsonschema v0.0.0-20220216202328-9eeeec9d044b/go.mod h1:/n6+1/DWPltRLWL/VKyUxg6tzsl5kHUCcraimt4vr60= +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= +github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +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/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= +github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ= +github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74= +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.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..451b7ae --- /dev/null +++ b/main.go @@ -0,0 +1,18 @@ +package yasch + +import ( + "os" + + "github.com/alecthomas/jsonschema" +) + +// WriteFile Write the structure definition to a JSON schema file. +func WriteFile(data interface{}, path string) { + d, err := jsonschema.Reflect(data).MarshalJSON() + if err != nil { + panic(err) + } + if err := os.WriteFile(path, d, 0644); err != nil { + panic(err) + } +}