From e3364037aa377eba95c418e003a8390438d66264 Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Fri, 9 Jun 2023 10:39:12 -0400 Subject: [PATCH] Cleaning up flag structure Things were confusing, the root command had required flags that made it impossible to view the help output, it was wild. Now it feels a little more approachable. --- cmd/obp/cmd/root.go | 26 -------------------------- cmd/obp/cmd/validate.go | 37 +++++++++++++++++++++---------------- 2 files changed, 21 insertions(+), 42 deletions(-) diff --git a/cmd/obp/cmd/root.go b/cmd/obp/cmd/root.go index 36a2184..e23151b 100644 --- a/cmd/obp/cmd/root.go +++ b/cmd/obp/cmd/root.go @@ -5,7 +5,6 @@ package cmd import ( "fmt" - "log" "os" "github.com/spf13/cobra" @@ -43,31 +42,6 @@ func init() { rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "~/.obp.toml", "config file") - rootCmd.PersistentFlags().StringVar(&vault, "vault", "", "vault root directory") - - 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]") - - 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") - } - - 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/cmd/obp/cmd/validate.go b/cmd/obp/cmd/validate.go index 501c5c7..f2b5c2c 100644 --- a/cmd/obp/cmd/validate.go +++ b/cmd/obp/cmd/validate.go @@ -6,6 +6,7 @@ package cmd import ( "fmt" "io/fs" + "log" "os" "path/filepath" @@ -23,23 +24,12 @@ var validateCmd = &cobra.Command{ Short: "loads a note and ensures its frontmatter follows the provided protobuf schema", Long: `Validate YAML frontmatter with jsonschema `, - // Uncomment the following line if your bare application - // has an action associated with it: - PreRunE: func(cmd *cobra.Command, args []string) error { - target := cmd.Flag("target").Value.String() - if len(target) == 0 { - return fmt.Errorf("Please profide a target filename") - } - root := os.DirFS(target) - _, err := root.Open(".") - if err != nil { - return fmt.Errorf("cannot open provided vault root %q: %w", target, err) - } - return nil - }, RunE: func(cmd *cobra.Command, args []string) error { schema := viper.GetString("schema") target := viper.GetString("target") + if target == "" { + return fmt.Errorf("target flag must not be empty") + } root := os.DirFS(target) err := fs.WalkDir(root, ".", func(path string, d fs.DirEntry, err error) error { @@ -82,7 +72,22 @@ func init() { validateCmd.Flags().StringP("schema", "s", "base.schema", "path to protobuf file") validateCmd.Flags().StringP("target", "t", "", "directory containing validation targets") validateCmd.MarkFlagsRequiredTogether("schema", "target") + validateCmd.PersistentFlags().StringVar(&format, "format", "markdown", "output format [markdown, json, csv]") rootCmd.AddCommand(validateCmd) - viper.BindPFlag("schema", validateCmd.Flags().Lookup("schema")) - viper.BindPFlag("target", validateCmd.Flags().Lookup("target")) + + err := viper.BindPFlag("schema", validateCmd.Flags().Lookup("schema")) + if err != nil { + log.Panicln("error binding viper to schema flag:", err) + } + + err = viper.BindPFlag("target", validateCmd.Flags().Lookup("target")) + if err != nil { + log.Panicln("error binding viper to target flag:", err) + } + + err = viper.BindPFlag("format", validateCmd.PersistentFlags().Lookup("format")) + if err != nil { + log.Panicln("error binding viper to format flag:", err) + } + }