Merge pull request #552 from x1022as/validate
improve validate usage message
This commit is contained in:
commit
7c1a16b54a
|
@ -10,30 +10,53 @@ import (
|
||||||
"github.com/xeipuuv/gojsonschema"
|
"github.com/xeipuuv/gojsonschema"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const usage = `Validate is used to check document with specified schema.
|
||||||
|
You can use validate in following ways:
|
||||||
|
|
||||||
|
1.specify document file as an argument
|
||||||
|
validate <schema.json> <document.json>
|
||||||
|
|
||||||
|
2.pass document content through a pipe
|
||||||
|
cat <document.json> | validate <schema.json>
|
||||||
|
|
||||||
|
3.input document content manually, ended with ctrl+d(or your self-defined EOF keys)
|
||||||
|
validate <schema.json>
|
||||||
|
[INPUT DOCUMENT CONTENT HERE]
|
||||||
|
`
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
nargs := len(os.Args[1:])
|
nargs := len(os.Args[1:])
|
||||||
if nargs == 0 || nargs > 2 {
|
if nargs == 0 || nargs > 2 {
|
||||||
fmt.Printf("ERROR: usage is: %s <schema.json> [<document.json>]\n", os.Args[0])
|
fmt.Printf("ERROR: invalid arguments number\n\n%s\n", usage)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if os.Args[1] == "help" ||
|
||||||
|
os.Args[1] == "--help" ||
|
||||||
|
os.Args[1] == "-h" {
|
||||||
|
fmt.Printf("%s\n", usage)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
schemaPath := os.Args[1]
|
schemaPath := os.Args[1]
|
||||||
if !strings.Contains(schemaPath, "://") {
|
if !strings.Contains(schemaPath, "://") {
|
||||||
schemaPath, err := filepath.Abs(schemaPath)
|
var err error
|
||||||
|
schemaPath, err = formatFilePath(schemaPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Printf("ERROR: invalid schema-file path: %s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
schemaPath = "file://" + schemaPath
|
schemaPath = "file://" + schemaPath
|
||||||
}
|
}
|
||||||
|
|
||||||
schemaLoader := gojsonschema.NewReferenceLoader(schemaPath)
|
schemaLoader := gojsonschema.NewReferenceLoader(schemaPath)
|
||||||
|
|
||||||
var documentLoader gojsonschema.JSONLoader
|
var documentLoader gojsonschema.JSONLoader
|
||||||
|
|
||||||
if nargs > 1 {
|
if nargs > 1 {
|
||||||
documentPath, err := filepath.Abs(os.Args[2])
|
documentPath, err := formatFilePath(os.Args[2])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Printf("ERROR: invalid document-file path: %s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
documentLoader = gojsonschema.NewReferenceLoader("file://" + documentPath)
|
documentLoader = gojsonschema.NewReferenceLoader("file://" + documentPath)
|
||||||
|
@ -49,7 +72,8 @@ func main() {
|
||||||
|
|
||||||
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
|
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if result.Valid() {
|
if result.Valid() {
|
||||||
|
@ -62,3 +86,15 @@ func main() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func formatFilePath(path string) (string, error) {
|
||||||
|
if _, err := os.Stat(path); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
absPath, err := filepath.Abs(path)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return absPath, nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue