From d421ca49958c29886de32ee0871575a2c32a38c7 Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Wed, 17 May 2023 13:15:40 -0400 Subject: [PATCH] Refactoring validation code out of cmd/ --- attachments.go | 2 +- blerp.go | 2 +- cmd/obp/cmd/validate.go | 34 +++++----------------------------- notes.go | 2 +- pipeline.go | 2 +- validate.go | 37 +++++++++++++++++++++++++++++++++++++ 6 files changed, 46 insertions(+), 33 deletions(-) create mode 100644 validate.go diff --git a/attachments.go b/attachments.go index b44dde7..29fae06 100644 --- a/attachments.go +++ b/attachments.go @@ -1,4 +1,4 @@ -package obspipeline +package obp func (p *Pipeline) FindAttachments() error { diff --git a/blerp.go b/blerp.go index aef9407..11ba685 100644 --- a/blerp.go +++ b/blerp.go @@ -1,4 +1,4 @@ -package obspipeline +package obp import ( "fmt" diff --git a/cmd/obp/cmd/validate.go b/cmd/obp/cmd/validate.go index d8dcdf1..eec470f 100644 --- a/cmd/obp/cmd/validate.go +++ b/cmd/obp/cmd/validate.go @@ -5,13 +5,11 @@ package cmd import ( "fmt" - "log" - "os" - "github.com/santhosh-tekuri/jsonschema/v5" _ "github.com/santhosh-tekuri/jsonschema/v5/httploader" "github.com/spf13/cobra" - "gopkg.in/yaml.v3" + + "code.ndumas.com/ndumas/obsidian-pipeline" ) // rootCmd represents the base command when called without any subcommands @@ -34,31 +32,9 @@ var validateCmd = &cobra.Command{ return nil }, Run: func(cmd *cobra.Command, args []string) { - var m interface{} - - schemaURL := cmd.Flag("schema").Value.String() - - // err := yaml.Unmarshal([]byte(yamlText), &m) - targetFilename := cmd.Flag("target").Value.String() - target, err := os.Open(targetFilename) - if err != nil { - log.Fatalf("could not open target file: %s\n", err) - } - dec := yaml.NewDecoder(target) - err = dec.Decode(&m) - if err != nil { - log.Fatalf("error decoding YAML: %s\n", err) - } - - compiler := jsonschema.NewCompiler() - schema, err := compiler.Compile(schemaURL) - if err != nil { - log.Fatalf("error compiling schema: %s\n", err) - } - if err := schema.Validate(m); err != nil { - log.Fatalf("error validating: %#v\n", err) - } - fmt.Println("validation successfull") + schema := cmd.Flag("schema").Value.String() + target := cmd.Flag("target").Value.String() + obp.Validate(schema, target) }, } diff --git a/notes.go b/notes.go index b12dc49..1144f74 100644 --- a/notes.go +++ b/notes.go @@ -1,4 +1,4 @@ -package obspipeline +package obp func (p *Pipeline) FindNotes() error { return nil diff --git a/pipeline.go b/pipeline.go index f8817bd..fce6712 100644 --- a/pipeline.go +++ b/pipeline.go @@ -1,4 +1,4 @@ -package obspipeline +package obp import ( "go.uber.org/zap" diff --git a/validate.go b/validate.go new file mode 100644 index 0000000..a49caf0 --- /dev/null +++ b/validate.go @@ -0,0 +1,37 @@ +package obp + +import ( + "fmt" + "log" + "os" + + "github.com/santhosh-tekuri/jsonschema/v5" + _ "github.com/santhosh-tekuri/jsonschema/v5/httploader" + "gopkg.in/yaml.v3" +) + +func Validate(schemaURL, filename string) error { + var m interface{} + + target, err := os.Open(filename) + if err != nil { + log.Fatalf("could not open target file: %s\n", err) + } + dec := yaml.NewDecoder(target) + err = dec.Decode(&m) + if err != nil { + log.Fatalf("error decoding YAML: %s\n", err) + } + + compiler := jsonschema.NewCompiler() + schema, err := compiler.Compile(schemaURL) + if err != nil { + log.Fatalf("error compiling schema: %s\n", err) + } + if err := schema.Validate(m); err != nil { + log.Fatalf("error validating: %#v\n", err) + } + fmt.Println("validation successfull") + + return nil +}