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() {
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 {

View File

@ -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)
}
},