support reading data from stdin for validation
@@ -16,6 +16,7 @@ package cli
import (
"encoding/json"
"fmt"
+ "io"
"os"
"strings"
@@ -32,13 +33,26 @@ func configureSchemaValidateCommand(schema *fisk.CmdClause) {
c := &schemaValidateCmd{}
validate := schema.Command("validate", "Validates a JSON file against a schema").Alias("check").Action(c.validate)
validate.Arg("schema", "Schema ID to validate against").Required().StringVar(&c.schema)
- validate.Arg("file", "JSON data to validate").Required().StringVar(&c.file)
+ validate.Arg("file", "JSON data to validate (- for stdin)").Required().StringVar(&c.file)
validate.Flag("json", "Produce JSON format output").UnNegatableBoolVar(&c.json)
}
func (c *schemaValidateCmd) validate(_ *fisk.ParseContext) error {
- file, err := os.ReadFile(c.file)
+ var f io.ReadCloser
+ var err error
+
+ if c.file == "-" {
+ f = os.Stdin
+ } else {
+ f, err = os.Open(c.file)
+ if err != nil {
+ return err
+ }
+ defer f.Close()
+ }
+
+ file, err := io.ReadAll(f)
if err != nil {
return err
}
