Namespacing your viper binds is important

main
Nick Dumas 2 years ago
parent b631b717b6
commit 0dda41e3f8

@ -22,19 +22,8 @@ var hugoCmd = &cobra.Command{
return nil return nil
}, },
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
cmd.DebugFlags() source := viper.GetString("hugo.source")
target := viper.GetString("hugo.target")
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
err := obp.CopyPosts(source, target) err := obp.CopyPosts(source, target)
if err != nil { if err != nil {
@ -56,13 +45,13 @@ var hugoCmd = &cobra.Command{
func init() { func init() {
hugoCmd.Flags().StringP("source", "s", "", "path to vault directory containing hugo posts") 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 { if err != nil {
log.Panicln("error binding viper to source flag:", err) log.Panicln("error binding viper to source flag:", err)
} }
hugoCmd.Flags().StringP("target", "t", "", "hugo content/ directory") 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 { if err != nil {
log.Panicln("error binding viper to target flag:", err) log.Panicln("error binding viper to target flag:", err)
} }

@ -25,8 +25,8 @@ var validateCmd = &cobra.Command{
Long: `Validate YAML frontmatter with jsonschema Long: `Validate YAML frontmatter with jsonschema
`, `,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
schema := viper.GetString("schema") schema := viper.GetString("validate.schema")
target := viper.GetString("target") target := viper.GetString("validate.target")
if target == "" { if target == "" {
return fmt.Errorf("target flag must not be empty") return fmt.Errorf("target flag must not be empty")
} }
@ -81,12 +81,12 @@ func init() {
log.Panicln("error binding viper to schema flag:", err) 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 { if err != nil {
log.Panicln("error binding viper to target flag:", err) 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 { if err != nil {
log.Panicln("error binding viper to format flag:", err) log.Panicln("error binding viper to format flag:", err)
} }

@ -3,7 +3,7 @@ package obp
import ( import (
"fmt" "fmt"
"io/fs" "io/fs"
"log" // "log"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -40,10 +40,7 @@ func CopyPosts(src, dst string) error {
postName := strings.Join(splitPostName[:len(splitPostName)-1], ".") postName := strings.Join(splitPostName[:len(splitPostName)-1], ".")
log.Printf("provided dst: %q\n", dst)
postDir := filepath.Join(dst, postName) postDir := filepath.Join(dst, postName)
log.Printf("postDir: %q\n", postDir)
return nil
err := os.MkdirAll(postDir, 0777) err := os.MkdirAll(postDir, 0777)

Loading…
Cancel
Save