Tinkering with Viper to read configuration files

This commit is contained in:
raul 2024-05-16 09:30:48 +02:00
parent 4b17a74965
commit 21dc78fe4a
3 changed files with 12 additions and 10 deletions

View File

@ -5,10 +5,9 @@ package cmd
import ( import (
"fmt" "fmt"
"os"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"os"
) )
var cfgFile string var cfgFile string
@ -41,7 +40,7 @@ func init() {
// Cobra supports persistent flags, which, if defined here, // Cobra supports persistent flags, which, if defined here,
// will be global for your application. // will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.tune.yaml)") rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/config/tune.yaml)")
// Cobra also supports local flags, which will only run // Cobra also supports local flags, which will only run
// when this action is called directly. // when this action is called directly.
@ -59,9 +58,9 @@ func initConfig() {
cobra.CheckErr(err) cobra.CheckErr(err)
// Search config in home directory with name ".tune" (without extension). // Search config in home directory with name ".tune" (without extension).
viper.AddConfigPath(home) viper.AddConfigPath(home + "/.config")
viper.SetConfigType("yaml") viper.SetConfigType("toml")
viper.SetConfigName(".tune") viper.SetConfigName("tune")
} }
viper.AutomaticEnv() // read in environment variables that match viper.AutomaticEnv() // read in environment variables that match

View File

@ -3,10 +3,13 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/ebitengine/oto/v3" "github.com/spf13/viper"
//"github.com/ebitengine/oto/v3"
) )
func tune() { func tune() {
fmt.Println() fmt.Println("tune called")
title := viper.Get("title")
fmt.Println(title)
} }

View File

@ -1,9 +1,9 @@
package cmd package cmd
import ( import (
"github.com/jroimartin/gocui" // "github.com/jroimartin/gocui"
) )
func UI() { func UI() {
g, err := gocui.NewGui() //g, err := gocui.NewGui()
} }