From 2a99d4681e05d47582506bfa0e347edee91b7d88 Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Tue, 6 Jun 2023 18:14:49 -0400 Subject: [PATCH] using strings for format, trying to use viper --- cmd/obp/cmd/root.go | 6 ++++++ cmd/obp/cmd/validate.go | 9 ++++++--- validate.go | 23 ++++++++++++----------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/cmd/obp/cmd/root.go b/cmd/obp/cmd/root.go index 57f8cda..2c0adc8 100644 --- a/cmd/obp/cmd/root.go +++ b/cmd/obp/cmd/root.go @@ -15,6 +15,7 @@ import ( var ( vault string cfgFile string + format string ) // rootCmd represents the base command when called without any subcommands @@ -48,6 +49,11 @@ func init() { rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "~/.obp.toml", "config file") rootCmd.PersistentFlags().StringVar(&vault, "vault", "", "vault root directory") rootCmd.MarkPersistentFlagRequired("vault") + rootCmd.PersistentFlags().StringVar(&format, "format", "markdown", "output format [markdown, json, csv]") + rootCmd.MarkPersistentFlagRequired("format") + + viper.BindPFlag("format", validateCmd.Flags().Lookup("format")) + viper.BindPFlag("vault", validateCmd.Flags().Lookup("vault")) // Cobra also supports local flags, which will only run // when this action is called directly. diff --git a/cmd/obp/cmd/validate.go b/cmd/obp/cmd/validate.go index 323d2d8..0dfaa7c 100644 --- a/cmd/obp/cmd/validate.go +++ b/cmd/obp/cmd/validate.go @@ -12,6 +12,7 @@ import ( "github.com/santhosh-tekuri/jsonschema/v5" _ "github.com/santhosh-tekuri/jsonschema/v5/httploader" "github.com/spf13/cobra" + "github.com/spf13/viper" "code.ndumas.com/ndumas/obsidian-pipeline" ) @@ -37,8 +38,8 @@ var validateCmd = &cobra.Command{ return nil }, RunE: func(cmd *cobra.Command, args []string) error { - schema := cmd.Flag("schema").Value.String() - target := cmd.Flag("target").Value.String() + schema := viper.GetString("schema") + target := viper.GetString("target") root := os.DirFS(target) err := fs.WalkDir(root, ".", func(path string, d fs.DirEntry, err error) error { @@ -62,7 +63,7 @@ var validateCmd = &cobra.Command{ err = obp.Validate(schema, target) if err != nil { details := err.(*jsonschema.ValidationError).DetailedOutput() - obp.PrettyDetails(cmd.OutOrStdout(), obp.JSON, details) + obp.PrettyDetails(cmd.OutOrStdout(), viper.GetString("format"), details) } return nil }) @@ -79,4 +80,6 @@ func init() { validateCmd.Flags().StringP("target", "t", "", "directory containing validation targets") validateCmd.MarkFlagsRequiredTogether("schema", "target") rootCmd.AddCommand(validateCmd) + viper.BindPFlag("schema", validateCmd.Flags().Lookup("schema")) + viper.BindPFlag("target", validateCmd.Flags().Lookup("target")) } diff --git a/validate.go b/validate.go index bb2ceab..58ceeb8 100644 --- a/validate.go +++ b/validate.go @@ -4,20 +4,13 @@ import ( "encoding/json" "fmt" "io" + "log" "github.com/santhosh-tekuri/jsonschema/v5" _ "github.com/santhosh-tekuri/jsonschema/v5/httploader" "gopkg.in/yaml.v3" ) -type PrettyDetailFormat int - -const ( - JSON = iota - Markdown - CSV -) - func Validate(schemaURL string, r io.Reader) error { var m interface{} @@ -48,16 +41,24 @@ func recurseDetails(detailed jsonschema.Detailed, acc []jsonschema.Detailed) []j return acc } -func PrettyDetails(w io.Writer, format PrettyDetailFormat, details jsonschema.Detailed) error { +func PrettyDetails(w io.Writer, format string, details jsonschema.Detailed) error { + log.Printf("received format %q\n", format) acc := make([]jsonschema.Detailed, 0) errors := recurseDetails(details, acc) switch format { - case JSON: + case "json": enc := json.NewEncoder(w) - err := enc.Encode(errors) + err := enc.Encode(details) if err != nil { return fmt.Errorf("error writing JSON payload to provided writer: %w", err) } + case "markdown": + fmt.Fprintf(w, "# Validation errors for \n") + fmt.Fprintf(w, "Valid|KeywordLocation|AbsoluteKeywordLocation|Instance Location|Error") + fmt.Fprintf(w, "---|---|---|---|---") + for _, e := range errors { + fmt.Fprintf(w, "%t|%s|%s|%s|%s\n", e.Valid, e.KeywordLocation, e.AbsoluteKeywordLocation, e.InstanceLocation, e.Error) + } default: return fmt.Errorf("unknown format")