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.
main
Nick Dumas 2 years ago
parent 26697d0b89
commit e3364037aa

@ -5,7 +5,6 @@ package cmd
import ( import (
"fmt" "fmt"
"log"
"os" "os"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -43,31 +42,6 @@ func init() {
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "~/.obp.toml", "config file") 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.SetHelpFunc(gloss.CharmHelp)
// rootCmd.SetUsageFunc(gloss.CharmUsage) // rootCmd.SetUsageFunc(gloss.CharmUsage)
} }

@ -6,6 +6,7 @@ package cmd
import ( import (
"fmt" "fmt"
"io/fs" "io/fs"
"log"
"os" "os"
"path/filepath" "path/filepath"
@ -23,23 +24,12 @@ var validateCmd = &cobra.Command{
Short: "loads a note and ensures its frontmatter follows the provided protobuf schema", Short: "loads a note and ensures its frontmatter follows the provided protobuf schema",
Long: `Validate YAML frontmatter with jsonschema 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 { RunE: func(cmd *cobra.Command, args []string) error {
schema := viper.GetString("schema") schema := viper.GetString("schema")
target := viper.GetString("target") target := viper.GetString("target")
if target == "" {
return fmt.Errorf("target flag must not be empty")
}
root := os.DirFS(target) root := os.DirFS(target)
err := 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 {
@ -82,7 +72,22 @@ func init() {
validateCmd.Flags().StringP("schema", "s", "base.schema", "path to protobuf file") validateCmd.Flags().StringP("schema", "s", "base.schema", "path to protobuf file")
validateCmd.Flags().StringP("target", "t", "", "directory containing validation targets") validateCmd.Flags().StringP("target", "t", "", "directory containing validation targets")
validateCmd.MarkFlagsRequiredTogether("schema", "target") validateCmd.MarkFlagsRequiredTogether("schema", "target")
validateCmd.PersistentFlags().StringVar(&format, "format", "markdown", "output format [markdown, json, csv]")
rootCmd.AddCommand(validateCmd) 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)
}
} }

Loading…
Cancel
Save