From d24ceeac69563d749724cb326f3a6544feb217dc Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Wed, 7 Jun 2023 18:31:19 -0400 Subject: [PATCH] Code cleanup for public release --- cmd/obp/cmd/root.go | 35 +++++++++++++++++++++-------------- validate.go | 8 ++++++++ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/cmd/obp/cmd/root.go b/cmd/obp/cmd/root.go index b85962d..36a2184 100644 --- a/cmd/obp/cmd/root.go +++ b/cmd/obp/cmd/root.go @@ -5,6 +5,7 @@ package cmd import ( "fmt" + "log" "os" "github.com/spf13/cobra" @@ -18,7 +19,6 @@ var ( format string ) -// rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ TraverseChildren: true, Use: "obp", @@ -33,8 +33,7 @@ var rootCmd = &cobra.Command{ // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { - err := rootCmd.Execute() - if err != nil { + if err := rootCmd.Execute(); err != nil { os.Exit(1) } } @@ -42,24 +41,32 @@ func Execute() { func init() { cobra.OnInitialize(initConfig) - // Here you will define your flags and configuration settings. - // Cobra supports persistent flags, which, if defined here, - // will be global for your application. - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "~/.obp.toml", "config file") rootCmd.PersistentFlags().StringVar(&vault, "vault", "", "vault root directory") - rootCmd.MarkPersistentFlagRequired("vault") + + err := rootCmd.MarkPersistentFlagRequired("vault") + + if err != nil { + log.Panicln("error setting vault flag as required") + } rootCmd.PersistentFlags().StringVar(&format, "format", "markdown", "output format [markdown, json, csv]") - rootCmd.MarkPersistentFlagRequired("format") - viper.BindPFlag("format", rootCmd.PersistentFlags().Lookup("format")) - viper.BindPFlag("vault", rootCmd.PersistentFlags().Lookup("vault")) + err = rootCmd.MarkPersistentFlagRequired("format") + if err != nil { + log.Panicln("error setting format flag as required") + } + + err = viper.BindPFlag("format", rootCmd.PersistentFlags().Lookup("format")) + if err != nil { + log.Panicln("error binding viper to format flag") + } - // Cobra also supports local flags, which will only run - // when this action is called directly. - // rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + err = viper.BindPFlag("vault", rootCmd.PersistentFlags().Lookup("vault")) + if err != nil { + log.Panicln("error binding viper to vault flag") + } // rootCmd.SetHelpFunc(gloss.CharmHelp) // rootCmd.SetUsageFunc(gloss.CharmUsage) diff --git a/validate.go b/validate.go index 8410cdd..5c574b2 100644 --- a/validate.go +++ b/validate.go @@ -10,6 +10,10 @@ import ( "gopkg.in/yaml.v3" ) +// Validate accepts a Markdown file as input via the Reader +// and parses the frontmatter present, if any. It then +// applies the schema fetched from schemaURL against the +// decoded YAML. func Validate(schemaURL string, r io.Reader) error { var m interface{} @@ -42,6 +46,10 @@ func recurseDetails(detailed jsonschema.Detailed, acc map[string]jsonschema.Deta return acc } +// PrettyDetails takes error output from jsonschema.Validate +// and pretty-prints it to stdout. +// +// Supported formats are: JSON, Markdown func PrettyDetails(w io.Writer, format string, details jsonschema.Detailed, filename string) error { // acc := make([]jsonschema.Detailed, 0) acc := make(map[string]jsonschema.Detailed)