|  | 
|  | 1 | +/* | 
|  | 2 | + * Original code from: https://github.com/opencontainers/runtime-spec/blob/643c1429d905bba70fe977bae274f367ad101e73/schema/validate.go | 
|  | 3 | + * Changes: | 
|  | 4 | + *  - Output errors to stderr | 
|  | 5 | + *  - Refactored to use package-internal validation library | 
|  | 6 | + * | 
|  | 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 8 | + * you may not use this file except in compliance with the License. | 
|  | 9 | + * You may obtain a copy of the License at | 
|  | 10 | + * | 
|  | 11 | + *     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 12 | + * | 
|  | 13 | + * Unless required by applicable law or agreed to in writing, software | 
|  | 14 | + * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 16 | + * See the License for the specific language governing permissions and | 
|  | 17 | + * limitations under the License. | 
|  | 18 | + */ | 
|  | 19 | + | 
|  | 20 | +package main | 
|  | 21 | + | 
|  | 22 | +import ( | 
|  | 23 | +	"flag" | 
|  | 24 | +	"fmt" | 
|  | 25 | +	"io/ioutil" | 
|  | 26 | +	"os" | 
|  | 27 | + | 
|  | 28 | +	"github.com/container-orchestrated-devices/container-device-interface/schema" | 
|  | 29 | +) | 
|  | 30 | + | 
|  | 31 | +const usage = `Validate is used to check document with specified schema. | 
|  | 32 | +You can use validate in following ways: | 
|  | 33 | +
 | 
|  | 34 | +   1.specify document file as an argument | 
|  | 35 | +      validate --schema <schema.json> <document.json> | 
|  | 36 | +
 | 
|  | 37 | +   2.pass document content through a pipe | 
|  | 38 | +      cat <document.json> | validate --schema <schema.json> | 
|  | 39 | +
 | 
|  | 40 | +   3.input document content manually, ended with ctrl+d(or your self-defined EOF keys) | 
|  | 41 | +      validate --schema <schema.json> | 
|  | 42 | +      [INPUT DOCUMENT CONTENT HERE] | 
|  | 43 | +` | 
|  | 44 | + | 
|  | 45 | +func main() { | 
|  | 46 | +	var ( | 
|  | 47 | +		schemaFile string | 
|  | 48 | +		docFile    string | 
|  | 49 | +		docData    []byte | 
|  | 50 | +		err        error | 
|  | 51 | +		exitCode   int | 
|  | 52 | +	) | 
|  | 53 | + | 
|  | 54 | +	flag.Usage = func() { | 
|  | 55 | +		fmt.Fprintf(flag.CommandLine.Output(), "%s\n", usage) | 
|  | 56 | +		fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0]) | 
|  | 57 | +		flag.PrintDefaults() | 
|  | 58 | +	} | 
|  | 59 | + | 
|  | 60 | +	flag.StringVar(&schemaFile, "schema", "builtin", "JSON Schema to validate against") | 
|  | 61 | +	flag.Parse() | 
|  | 62 | + | 
|  | 63 | +	if schemaFile != "" { | 
|  | 64 | +		scm, err := schema.Load(schemaFile) | 
|  | 65 | +		if err != nil { | 
|  | 66 | +			fmt.Fprintf(os.Stderr, "failed to load schema %s: %v\n", schemaFile, err) | 
|  | 67 | +			os.Exit(1) | 
|  | 68 | +		} | 
|  | 69 | +		schema.Set(scm) | 
|  | 70 | +		fmt.Printf("Validating against JSON schema %s...\n", schemaFile) | 
|  | 71 | +	} else { | 
|  | 72 | +		fmt.Printf("Validating against builtin JSON schema...\n") | 
|  | 73 | +	} | 
|  | 74 | + | 
|  | 75 | +	docs := flag.Args() | 
|  | 76 | +	if len(docs) == 0 { | 
|  | 77 | +		docs = []string{"-"} | 
|  | 78 | +	} | 
|  | 79 | + | 
|  | 80 | +	for _, docFile = range docs { | 
|  | 81 | +		if docFile == "" || docFile == "-" { | 
|  | 82 | +			docFile = "<stdin>" | 
|  | 83 | +			docData, err = ioutil.ReadAll(os.Stdin) | 
|  | 84 | +			if err != nil { | 
|  | 85 | +				fmt.Fprintf(os.Stderr, "failed to read document data from stdin: %v\n", err) | 
|  | 86 | +				os.Exit(1) | 
|  | 87 | +			} | 
|  | 88 | +			err = schema.ValidateData(docData) | 
|  | 89 | +		} else { | 
|  | 90 | +			err = schema.ValidateFile(docFile) | 
|  | 91 | +		} | 
|  | 92 | + | 
|  | 93 | +		if err != nil { | 
|  | 94 | +			fmt.Fprintf(os.Stderr, "%s: validation failed:\n    %v\n", docFile, err) | 
|  | 95 | +			exitCode = 1 | 
|  | 96 | +		} else { | 
|  | 97 | +			fmt.Printf("%s: document is valid.\n", docFile) | 
|  | 98 | +		} | 
|  | 99 | +	} | 
|  | 100 | + | 
|  | 101 | +	os.Exit(exitCode) | 
|  | 102 | +} | 
0 commit comments