From 0dda41e3f80d88f68679ec156b451e54f583ad61 Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Thu, 22 Jun 2023 14:34:45 -0400 Subject: [PATCH] Namespacing your viper binds is important --- cmd/obp/cmd/hugo.go | 19 ++++--------------- cmd/obp/cmd/validate.go | 8 ++++---- hugo.go | 5 +---- 3 files changed, 9 insertions(+), 23 deletions(-) diff --git a/cmd/obp/cmd/hugo.go b/cmd/obp/cmd/hugo.go index bba1c8d..f3aa0a4 100644 --- a/cmd/obp/cmd/hugo.go +++ b/cmd/obp/cmd/hugo.go @@ -22,19 +22,8 @@ var hugoCmd = &cobra.Command{ return nil }, RunE: func(cmd *cobra.Command, args []string) error { - cmd.DebugFlags() - - source := viper.GetString("source") - log.Printf("viper source: %q\n", source) - cobraSource, _ := cmd.Flags().GetString("source") - log.Printf("cobra source: %q\n", cobraSource) - - target := viper.GetString("target") - log.Printf("viper target: %q\n", target) - cobraTarget, _ := cmd.Flags().GetString("target") - log.Printf("cobra target: %q\n", cobraTarget) - - return nil + source := viper.GetString("hugo.source") + target := viper.GetString("hugo.target") err := obp.CopyPosts(source, target) if err != nil { @@ -56,13 +45,13 @@ var hugoCmd = &cobra.Command{ func init() { hugoCmd.Flags().StringP("source", "s", "", "path to vault directory containing hugo posts") - err := viper.BindPFlag("source", hugoCmd.Flags().Lookup("source")) + err := viper.BindPFlag("hugo.source", hugoCmd.Flags().Lookup("source")) if err != nil { log.Panicln("error binding viper to source flag:", err) } hugoCmd.Flags().StringP("target", "t", "", "hugo content/ directory") - err = viper.BindPFlag("target", hugoCmd.Flags().Lookup("target")) + err = viper.BindPFlag("hugo.target", hugoCmd.Flags().Lookup("target")) if err != nil { log.Panicln("error binding viper to target flag:", err) } diff --git a/cmd/obp/cmd/validate.go b/cmd/obp/cmd/validate.go index 8ae2617..faa70b7 100644 --- a/cmd/obp/cmd/validate.go +++ b/cmd/obp/cmd/validate.go @@ -25,8 +25,8 @@ var validateCmd = &cobra.Command{ Long: `Validate YAML frontmatter with jsonschema `, RunE: func(cmd *cobra.Command, args []string) error { - schema := viper.GetString("schema") - target := viper.GetString("target") + schema := viper.GetString("validate.schema") + target := viper.GetString("validate.target") if target == "" { return fmt.Errorf("target flag must not be empty") } @@ -81,12 +81,12 @@ func init() { log.Panicln("error binding viper to schema flag:", err) } - err = viper.BindPFlag("target", validateCmd.Flags().Lookup("target")) + err = viper.BindPFlag("validate.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")) + err = viper.BindPFlag("validate.format", validateCmd.PersistentFlags().Lookup("format")) if err != nil { log.Panicln("error binding viper to format flag:", err) } diff --git a/hugo.go b/hugo.go index b5d3f2b..a51dac4 100644 --- a/hugo.go +++ b/hugo.go @@ -3,7 +3,7 @@ package obp import ( "fmt" "io/fs" - "log" + // "log" "os" "path/filepath" "strings" @@ -40,10 +40,7 @@ func CopyPosts(src, dst string) error { postName := strings.Join(splitPostName[:len(splitPostName)-1], ".") - log.Printf("provided dst: %q\n", dst) postDir := filepath.Join(dst, postName) - log.Printf("postDir: %q\n", postDir) - return nil err := os.MkdirAll(postDir, 0777)