viper not working

This commit is contained in:
Jan 2024-09-19 23:11:28 +02:00
parent 0fc5b36843
commit 86048da2cb
2 changed files with 16 additions and 9 deletions

View File

@ -1,27 +1,31 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// favCmd represents the fav command
var favCmd = &cobra.Command{
Use: "fav",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Short: "Retrieves the favorite mensa menus",
Long: `Retrieves the favorite mensa menus for the current day.
You can add mensas to your favorites using the search command.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("fav called")
favorites := viper.GetStringSlice("favorites")
if len(favorites) == 0 {
fmt.Println("You have no favorites")
return
}
fmt.Println("Your favorites: " + strings.Join(favorites, ", "))
},
}

View File

@ -115,7 +115,10 @@ var searchCmd = &cobra.Command{
if result == "favorites" {
favorites := viper.Get("favorites").([]string)
favorites = append(favorites, selectedCity+"/"+selectedMensa)
fmt.Println("Added " + selectedCity + "/" + selectedMensa + " to your favorites")
viper.Set("favorites", favorites)
fmt.Println(viper.Get("favorites"))
viper.SafeWriteConfig()
}
},