Commit 3b1680f8 authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Add json validation command

parent 9495ed51
module git.scc.kit.edu/feudal/feudalClient
require (
git.scc.kit.edu/feudal/feudalScripts v1.1.0
git.scc.kit.edu/feudal/feudalScripts/v2 v2.0.0
github.com/alecthomas/jsonschema v0.0.0-20190122210438-a6952de1bbe6
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/streadway/amqp v0.0.0-20181205114330-a314942b2fd9
github.com/stretchr/testify v1.3.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.1.0
gopkg.in/alecthomas/kingpin.v2 v2.2.6
)
git.scc.kit.edu/feudal/feudalClient v0.1.0/go.mod h1:0h97fa5a+3UqI6rGnkcUhM4DyFvBt4zAXIja+EJbXls=
git.scc.kit.edu/feudal/feudalScripts v1.0.0/go.mod h1:5+SmQtrtjjLAI5rgZXVVRNvLfwlKw0uRDwWkFq8HUJo=
git.scc.kit.edu/feudal/feudalScripts v1.1.0 h1:/j1FUlvwT6gOyjIS7+NnpDEcRE/QZfxA0JX7TIfFNc8=
git.scc.kit.edu/feudal/feudalScripts v1.1.0/go.mod h1:IJmsus8xLMsKeb+n8Gqg2J4s6fRtiNwxXNyD+sC9OJU=
git.scc.kit.edu/feudal/feudalScripts/v2 v2.0.0 h1:3907HJ9XEZSuEZ0oAuijssG9SGFjAdmyDt8P7SlFFZY=
git.scc.kit.edu/feudal/feudalScripts/v2 v2.0.0/go.mod h1:EVXrOHOlnw3VZ02LZQDSN7bxldpJ7p31tfKZ5zqAEXA=
github.com/alecthomas/jsonschema v0.0.0-20190122210438-a6952de1bbe6 h1:xadBCbc8D9mmkaNfCsEBHbIoCjbayJXJNsY1JjPjNio=
......@@ -12,10 +16,17 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/streadway/amqp v0.0.0-20181107104731-27835f1a64e9/go.mod h1:1WNBiOZtZQLpVAyu0iTduoJL9hEsMloAK5XWrtW0xdY=
github.com/streadway/amqp v0.0.0-20181205114330-a314942b2fd9 h1:37QTz/gdHBLQcsmgMTnQDSWCtKzJ7YnfI2M2yTdr4BQ=
github.com/streadway/amqp v0.0.0-20181205114330-a314942b2fd9/go.mod h1:1WNBiOZtZQLpVAyu0iTduoJL9hEsMloAK5XWrtW0xdY=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/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.1.0 h1:ngVtJC9TY/lg0AA/1k48FYhBrhRoFlEmWzsehpNAaZg=
github.com/xeipuuv/gojsonschema v1.1.0/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
......@@ -15,8 +13,6 @@ import (
"git.scc.kit.edu/feudal/feudalClient/source"
"git.scc.kit.edu/feudal/feudalClient/source/amqp"
"git.scc.kit.edu/feudal/feudalClient/source/rest"
scripts "git.scc.kit.edu/feudal/feudalScripts/v2"
"github.com/alecthomas/jsonschema"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
......@@ -29,10 +25,12 @@ var (
).Version(
"v2.1.0",
)
cmdStart = app.Command("start", "Starts the client in its normal operation mode.").Default()
cmdDeregister = app.Command("deregister", "Before disabling the client: Use deregister to inform the backend that the client ceases operation.")
cmdSchema = app.Command("schema", "Can be used to generate JSON schemas (Draft 4, see json-schema.org) for the validation of script inputs and outputs.")
schemaSelector = cmdSchema.Arg("selector", "Select the schema to generate.").Required().Enum("input", "output", "config")
cmdStart = app.Command("start", "Starts the client in its normal operation mode.").Default()
cmdDeregister = app.Command("deregister", "Before disabling the client: Use deregister to inform the backend that the client ceases operation.")
cmdSchema = app.Command("schema", "Can be used to generate a JSON schema (Draft 4, see json-schema.org) for the validation of script inputs, outputs and the config of this client.")
printSelector = cmdSchema.Arg("selector", "Select the schema to generate. Options: input, output, config").Required().Enum("input", "output", "config")
cmdValidate = app.Command("validate", "Validate a input/output/config json. The json needs to be passed via stdin.")
validateSelector = cmdValidate.Arg("selector", "Select the schema to validate against. Options: input, output, config").Required().Enum("input", "output", "config")
configFile = app.Flag("config", "Config file to use.").Short('c').String()
scriptDebugging = app.Flag("debug-scripts", "Display debugging info concerning executed scripts").Bool()
......@@ -112,43 +110,7 @@ func start(conf *config.Config) {
}
}
func printSchema(i interface{}) {
schema := jsonschema.Reflect(i)
bs, err := json.MarshalIndent(schema, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("%s", bs)
}
func main() {
var err error
// get arguments
cmd := kingpin.MustParse(app.Parse(os.Args[1:]))
// Do things that need no config or debugging
switch cmd {
case "schema":
switch *schemaSelector {
case "input":
printSchema(scripts.Input{})
case "output":
printSchema(scripts.Output{})
case "config":
printSchema(config.Config{})
}
return
}
// read the config file
if *configFile == "" {
log.Fatalf("No config file. Unable to proceed. Provide the --config flag")
}
conf, err := config.ReadConfig(*configFile)
if err != nil {
log.Fatalf("[Conf] %s", err)
}
func prepareDebugging(conf *config.Config) {
// Override the debug configuration with parameters
if *debugAll {
......@@ -174,6 +136,35 @@ func main() {
if conf.Debug.Sequential {
log.Printf("[Debug] Executing tasks sequentially")
}
}
func main() {
var err error
// get arguments
cmd := kingpin.MustParse(app.Parse(os.Args[1:]))
// Execute commands that don't need the config
switch cmd {
case "schema":
printSchema(*printSelector)
return
case "validate":
result, err := validateJSON(*validateSelector, os.Stdin)
if err != nil {
log.Fatal(err)
}
printValidationResult(result)
return
}
// read the config file
conf, err := config.ReadConfig(*configFile)
if err != nil {
log.Fatalf("[Conf] %s", err)
}
prepareDebugging(conf)
switch cmd {
case "start":
......
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"git.scc.kit.edu/feudal/feudalClient/config"
scripts "git.scc.kit.edu/feudal/feudalScripts"
"github.com/alecthomas/jsonschema"
"github.com/xeipuuv/gojsonschema"
)
func generateSchema(selector string) (schemaBytes []byte) {
var schema *jsonschema.Schema
switch selector {
case "input":
schema = jsonschema.Reflect(scripts.Input{})
case "output":
schema = jsonschema.Reflect(scripts.Output{})
case "config":
schema = jsonschema.Reflect(config.Config{})
}
schemaBytes, err := json.Marshal(schema)
if err != nil {
panic(err)
}
return
}
func printSchema(schemaSelector string) {
fmt.Printf("%s", generateSchema(schemaSelector))
}
func validateJSON(schemaSelector string, reader io.Reader) (result *gojsonschema.Result, err error) {
// first find and prepare our schema
schemaLoader := gojsonschema.NewBytesLoader(generateSchema(schemaSelector))
// second prepare the json which we are validating
var doc []byte
doc, err = ioutil.ReadAll(reader)
if err != nil {
err = fmt.Errorf("Error reading json for validation: %s", err)
return
}
docLoader := gojsonschema.NewBytesLoader(doc)
// proceed with the validation and print errors if they occur
result, err = gojsonschema.Validate(schemaLoader, docLoader)
return
}
func printValidationResult(result *gojsonschema.Result) {
if result.Valid() {
log.Println("Validation successful")
return
}
log.Printf("Validation errors")
for _, err := range result.Errors() {
fmt.Println(err)
}
os.Exit(1)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment