diff --git a/cmd/root.go b/cmd/root.go index efab895..a82b85d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -6,6 +6,7 @@ import ( "os" "github.com/Yakiyo/tilde/cache" + "github.com/Yakiyo/tilde/config" "github.com/Yakiyo/tilde/utils" "github.com/Yakiyo/tilde/where" "github.com/charmbracelet/log" @@ -23,9 +24,7 @@ var rootCmd = &cobra.Command{ View community driven and simplified man pages in your terminal`, Version: "0.1.0", PreRunE: func(cmd *cobra.Command, args []string) error { - logLvl := utils.Must(cmd.Flags().GetString("log-level")) - log.SetLevel(log.ParseLevel(logLvl)) - log.Info(fmt.Sprintf("Setting log level to %v", logLvl)) + config.BindFlags(cmd) return nil }, Run: func(cmd *cobra.Command, args []string) { @@ -77,7 +76,7 @@ func init() { f.StringP("render", "f", "", "Render a local file") // command related flags - f.BoolP("raw", "r", false, "Print raw markdown without formatting") + f.StringP("style", "s", "fancy", "Set output style [fancy, plain, raw]") f.StringP("language", "L", "en", "Override language") f.StringP("platform", "p", utils.Platform(), "Override operating system") } diff --git a/config/init.go b/config/init.go index 2cff391..9ebae58 100644 --- a/config/init.go +++ b/config/init.go @@ -2,6 +2,7 @@ package config import ( "github.com/Yakiyo/tilde/where" + "github.com/spf13/cobra" v "github.com/spf13/viper" ) @@ -9,14 +10,18 @@ func init() { v.SetConfigType("toml") v.SetConfigName("tilde") - // to be used when setting values from cli - v.RegisterAlias("tilde_dir", "dir") - v.SetDefault("auto_update", true) v.SetDefault("log_level", "warn") - v.SetDefault("tilde_dir", where.Dir()) + v.SetDefault("root_dir", where.Dir()) // must be one of always, auto, never v.SetDefault("colors", "auto") // must be one of fancy (use glow), plain (selt formatter), none/raw (raw value) v.SetDefault("style", "fancy") } + +// bind command line flags to viper +func BindFlags(cmd *cobra.Command) { + lookUp := cmd.Flags().Lookup + v.BindPFlag("colors", lookUp("style")) + v.BindPFlag("root_dir", lookUp("dir")) +}