Refactoring validation code out of cmd/

main
Nick Dumas 2 years ago
parent 5b93acf9ab
commit d421ca4995

@ -1,4 +1,4 @@
package obspipeline
package obp
func (p *Pipeline) FindAttachments() error {

@ -1,4 +1,4 @@
package obspipeline
package obp
import (
"fmt"

@ -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)
},
}

@ -1,4 +1,4 @@
package obspipeline
package obp
func (p *Pipeline) FindNotes() error {
return nil

@ -1,4 +1,4 @@
package obspipeline
package obp
import (
"go.uber.org/zap"

@ -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
}
Loading…
Cancel
Save