better error output and required flags

main
Nick Dumas 2 years ago
parent dc453d7539
commit 03f810acbd

@ -1,5 +1,9 @@
--- ---
aliases: [] aliases: []
title: ""
description: 4
tags: "fart"
draft: 3
--- ---
## Textual Cartography ## Textual Cartography

@ -12,7 +12,10 @@ import (
// "code.ndumas.com/ndumas/obsidian-pipeline/gloss" // "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 // rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
@ -42,7 +45,9 @@ func init() {
// Cobra supports persistent flags, which, if defined here, // Cobra supports persistent flags, which, if defined here,
// will be global for your application. // 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 // Cobra also supports local flags, which will only run
// when this action is called directly. // when this action is called directly.
@ -55,7 +60,6 @@ func init() {
// initConfig reads in config file and ENV variables if set. // initConfig reads in config file and ENV variables if set.
func initConfig() { func initConfig() {
if cfgFile != "" { if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile) viper.SetConfigFile(cfgFile)
} else { } else {
// Find home directory. // Find home directory.

@ -10,6 +10,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"github.com/santhosh-tekuri/jsonschema/v5"
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader" _ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -44,7 +45,8 @@ var validateCmd = &cobra.Command{
schema := cmd.Flag("schema").Value.String() schema := cmd.Flag("schema").Value.String()
target := cmd.Flag("target").Value.String() target := cmd.Flag("target").Value.String()
root := os.DirFS(target) 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 { if err != nil {
return err return err
} }
@ -63,9 +65,14 @@ var validateCmd = &cobra.Command{
return fmt.Errorf("could not open target file: %w", err) return fmt.Errorf("could not open target file: %w", err)
} }
defer target.Close() 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
}, },
} }

@ -24,7 +24,7 @@ func Validate(schemaURL string, r io.Reader) error {
return fmt.Errorf("error compiling schema: %w", err) return fmt.Errorf("error compiling schema: %w", err)
} }
if err := schema.Validate(m); err != nil { if err := schema.Validate(m); err != nil {
return fmt.Errorf("error validating target: %w", err) return err
} }
return nil return nil

Loading…
Cancel
Save