mirror of
https://github.com/opencontainers/runtime-spec.git
synced 2026-02-05 18:45:18 +01:00
Merge pull request #482 from wking/validate-stdin
schema/validate: Support reading documents via stdin
This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
@@ -9,8 +10,9 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args[1:]) != 2 {
|
||||
fmt.Printf("ERROR: usage is: %s <schema.json> <config.json>\n", os.Args[0])
|
||||
nargs := len(os.Args[1:])
|
||||
if nargs == 0 || nargs > 2 {
|
||||
fmt.Printf("ERROR: usage is: %s <schema.json> [<document.json>]\n", os.Args[0])
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
@@ -19,14 +21,25 @@ func main() {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
documentPath, err := filepath.Abs(os.Args[2])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
schemaLoader := gojsonschema.NewReferenceLoader("file://" + schemaPath)
|
||||
documentLoader := gojsonschema.NewReferenceLoader("file://" + documentPath)
|
||||
var documentLoader gojsonschema.JSONLoader
|
||||
|
||||
if nargs > 1 {
|
||||
documentPath, err := filepath.Abs(os.Args[2])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
documentLoader = gojsonschema.NewReferenceLoader("file://" + documentPath)
|
||||
} else {
|
||||
documentBytes, err := ioutil.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
documentString := string(documentBytes)
|
||||
documentLoader = gojsonschema.NewStringLoader(documentString)
|
||||
}
|
||||
|
||||
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user