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> Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/ */
package cmd package cmd
import ( import (
"fmt" "fmt"
"strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
) )
// favCmd represents the fav command // favCmd represents the fav command
var favCmd = &cobra.Command{ var favCmd = &cobra.Command{
Use: "fav", Use: "fav",
Short: "A brief description of your command", Short: "Retrieves the favorite mensa menus",
Long: `A longer description that spans multiple lines and likely contains examples Long: `Retrieves the favorite mensa menus for the current day.
and usage of using your command. For example: You can add mensas to your favorites using the search command.`,
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.`,
Run: func(cmd *cobra.Command, args []string) { 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" { if result == "favorites" {
favorites := viper.Get("favorites").([]string) favorites := viper.Get("favorites").([]string)
favorites = append(favorites, selectedCity+"/"+selectedMensa) favorites = append(favorites, selectedCity+"/"+selectedMensa)
fmt.Println("Added " + selectedCity + "/" + selectedMensa + " to your favorites")
viper.Set("favorites", favorites) viper.Set("favorites", favorites)
fmt.Println(viper.Get("favorites"))
viper.SafeWriteConfig()
} }
}, },