From 03f810acbd4159c110df3b1f6516fc257359352f Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Tue, 6 Jun 2023 16:03:25 -0400 Subject: [PATCH] better error output and required flags --- Resources/blog/published/schema-bad.md | 4 ++++ cmd/obp/cmd/root.go | 10 +++++++--- cmd/obp/cmd/validate.go | 13 ++++++++++--- validate.go | 2 +- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/Resources/blog/published/schema-bad.md b/Resources/blog/published/schema-bad.md index 14e49f5..ab35bbb 100644 --- a/Resources/blog/published/schema-bad.md +++ b/Resources/blog/published/schema-bad.md @@ -1,5 +1,9 @@ --- aliases: [] +title: "" +description: 4 +tags: "fart" +draft: 3 --- ## Textual Cartography diff --git a/cmd/obp/cmd/root.go b/cmd/obp/cmd/root.go index 770e98e..57f8cda 100644 --- a/cmd/obp/cmd/root.go +++ b/cmd/obp/cmd/root.go @@ -12,7 +12,10 @@ import ( // "code.ndumas.com/ndumas/obsidian-pipeline/gloss" ) -var cfgFile string +var ( + vault string + cfgFile string +) // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ @@ -42,7 +45,9 @@ func init() { // Cobra supports persistent flags, which, if defined here, // will be global for your application. - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.obp.toml)") + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "~/.obp.toml", "config file") + rootCmd.PersistentFlags().StringVar(&vault, "vault", "", "vault root directory") + rootCmd.MarkPersistentFlagRequired("vault") // Cobra also supports local flags, which will only run // when this action is called directly. @@ -55,7 +60,6 @@ func init() { // initConfig reads in config file and ENV variables if set. func initConfig() { if cfgFile != "" { - // Use config file from the flag. viper.SetConfigFile(cfgFile) } else { // Find home directory. diff --git a/cmd/obp/cmd/validate.go b/cmd/obp/cmd/validate.go index d973cce..8a5383d 100644 --- a/cmd/obp/cmd/validate.go +++ b/cmd/obp/cmd/validate.go @@ -10,6 +10,7 @@ import ( "os" "path/filepath" + "github.com/santhosh-tekuri/jsonschema/v5" _ "github.com/santhosh-tekuri/jsonschema/v5/httploader" "github.com/spf13/cobra" @@ -44,7 +45,8 @@ var validateCmd = &cobra.Command{ schema := cmd.Flag("schema").Value.String() target := cmd.Flag("target").Value.String() root := os.DirFS(target) - return fs.WalkDir(root, ".", func(path string, d fs.DirEntry, err error) error { + + err := fs.WalkDir(root, ".", func(path string, d fs.DirEntry, err error) error { if err != nil { return err } @@ -63,9 +65,14 @@ var validateCmd = &cobra.Command{ return fmt.Errorf("could not open target file: %w", err) } defer target.Close() - return obp.Validate(schema, target) + err = obp.Validate(schema, target) + if err != nil { + log.Printf("error validating input file %q: %#+v\n", path, err.(*jsonschema.ValidationError).DetailedOutput()) + } + return nil }) - // return obp.Validate(schema, target) + + return err }, } diff --git a/validate.go b/validate.go index 2571932..1bc3649 100644 --- a/validate.go +++ b/validate.go @@ -24,7 +24,7 @@ func Validate(schemaURL string, r io.Reader) error { return fmt.Errorf("error compiling schema: %w", err) } if err := schema.Validate(m); err != nil { - return fmt.Errorf("error validating target: %w", err) + return err } return nil