using strings for format, trying to use viper

main
Nick Dumas 2 years ago
parent eff142bf03
commit 2a99d4681e

@ -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.

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

@ -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")

Loading…
Cancel
Save