Skip to content

Commit

Permalink
feat: init config
Browse files Browse the repository at this point in the history
  • Loading branch information
Yakiyo committed Aug 23, 2023
1 parent 01dae66 commit 00078f7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
7 changes: 3 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -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")
}
13 changes: 9 additions & 4 deletions config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@ package config

import (
"github.com/Yakiyo/tilde/where"
"github.com/spf13/cobra"
v "github.com/spf13/viper"
)

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"))
}

0 comments on commit 00078f7

Please sign in to comment.