From 0fc5b368432d141f77a0afde8daab545ecfcb022 Mon Sep 17 00:00:00 2001 From: Jan Date: Thu, 19 Sep 2024 21:41:15 +0200 Subject: [PATCH] some config --- cmd/root.go | 10 ++++++++++ cmd/search.go | 5 ++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index 1abdeb7..56b9391 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -34,6 +34,10 @@ func Execute() { func init() { cobra.OnInitialize(initConfig) + + setDefaults() + + viper.SafeWriteConfig() // Here you will define your flags and configuration settings. // Cobra supports persistent flags, which, if defined here, // will be global for your application. @@ -48,6 +52,11 @@ func init() { rootCmd.CompletionOptions.DisableDefaultCmd = true } +func setDefaults() { + viper.SetDefault("Server", "https://mensa.barfuss.email") + viper.SetDefault("favorites", []string{}) +} + func initConfig() { if cfgFile != "" { viper.SetConfigFile(cfgFile) @@ -60,6 +69,7 @@ func initConfig() { viper.SetConfigName(".Mensa-CLI") } + viper.SetEnvPrefix("MENSA") viper.AutomaticEnv() if err := viper.ReadInConfig(); err == nil { diff --git a/cmd/search.go b/cmd/search.go index 2c08e68..602ab15 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -12,6 +12,7 @@ import ( "github.com/manifoldco/promptui" "github.com/rodaine/table" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // searchCmd represents the search command @@ -112,7 +113,9 @@ var searchCmd = &cobra.Command{ } if result == "favorites" { - // viper + favorites := viper.Get("favorites").([]string) + favorites = append(favorites, selectedCity+"/"+selectedMensa) + viper.Set("favorites", favorites) } },