From 86048da2cbda7aa98f2ee30eaf904ba1a22e307b Mon Sep 17 00:00:00 2001 From: Jan Date: Thu, 19 Sep 2024 23:11:28 +0200 Subject: [PATCH] viper not working --- cmd/fav.go | 22 +++++++++++++--------- cmd/search.go | 3 +++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/cmd/fav.go b/cmd/fav.go index 0f8c89a..729204a 100644 --- a/cmd/fav.go +++ b/cmd/fav.go @@ -1,27 +1,31 @@ /* Copyright © 2024 NAME HERE - */ 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, ", ")) + }, } diff --git a/cmd/search.go b/cmd/search.go index 602ab15..40c7341 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -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() } },