Tinkering with Viper to read configuration files
This commit is contained in:
parent
4b17a74965
commit
21dc78fe4a
11
cmd/root.go
11
cmd/root.go
|
@ -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
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue