some config

This commit is contained in:
Jan 2024-09-19 21:41:15 +02:00
parent 17bf99c828
commit 0fc5b36843
2 changed files with 14 additions and 1 deletions

View File

@ -34,6 +34,10 @@ func Execute() {
func init() { func init() {
cobra.OnInitialize(initConfig) cobra.OnInitialize(initConfig)
setDefaults()
viper.SafeWriteConfig()
// Here you will define your flags and configuration settings. // Here you will define your flags and configuration settings.
// 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.
@ -48,6 +52,11 @@ func init() {
rootCmd.CompletionOptions.DisableDefaultCmd = true rootCmd.CompletionOptions.DisableDefaultCmd = true
} }
func setDefaults() {
viper.SetDefault("Server", "https://mensa.barfuss.email")
viper.SetDefault("favorites", []string{})
}
func initConfig() { func initConfig() {
if cfgFile != "" { if cfgFile != "" {
viper.SetConfigFile(cfgFile) viper.SetConfigFile(cfgFile)
@ -60,6 +69,7 @@ func initConfig() {
viper.SetConfigName(".Mensa-CLI") viper.SetConfigName(".Mensa-CLI")
} }
viper.SetEnvPrefix("MENSA")
viper.AutomaticEnv() viper.AutomaticEnv()
if err := viper.ReadInConfig(); err == nil { if err := viper.ReadInConfig(); err == nil {

View File

@ -12,6 +12,7 @@ import (
"github.com/manifoldco/promptui" "github.com/manifoldco/promptui"
"github.com/rodaine/table" "github.com/rodaine/table"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
) )
// searchCmd represents the search command // searchCmd represents the search command
@ -112,7 +113,9 @@ var searchCmd = &cobra.Command{
} }
if result == "favorites" { if result == "favorites" {
// viper favorites := viper.Get("favorites").([]string)
favorites = append(favorites, selectedCity+"/"+selectedMensa)
viper.Set("favorites", favorites)
} }
}, },