From 1a21f47555785886952267372ada07cd9587a59a Mon Sep 17 00:00:00 2001 From: Jan Date: Fri, 20 Sep 2024 16:32:07 +0200 Subject: [PATCH] fav delete --- cmd/delete.go | 32 +++++++++++++++++++++++++++++++- cmd/fav.go | 3 --- cmd/root.go | 3 +-- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/cmd/delete.go b/cmd/delete.go index 62987c2..1dd2a3c 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -6,7 +6,9 @@ package cmd import ( "fmt" + "github.com/manifoldco/promptui" "github.com/spf13/cobra" + "github.com/spf13/viper" ) // deleteCmd represents the delete command @@ -15,7 +17,35 @@ var deleteCmd = &cobra.Command{ Short: "Deletes a favorite mensa", Long: `Returns a list of your favorite mensas and prompts you to select one to delete.`, Run: func(cmd *cobra.Command, args []string) { - fmt.Println("delete called") + favorites := viper.Get("favorites").([]interface{}) + + s := make([]string, len(favorites)) + for i, v := range favorites { + s[i] = v.(string) + } + + prompt := promptui.Select{ + Label: "Select Mensa to delete", + Items: s, + } + + _, result, err := prompt.Run() + + if err != nil { + fmt.Printf("Prompt failed %v\n", err) + return + } + + for i := 0; i < len(s); i++ { + if s[i] == result { + s = append(s[:i], s[i+1:]...) + break + } + } + + viper.Set("favorites", s) + viper.WriteConfig() + }, } diff --git a/cmd/fav.go b/cmd/fav.go index 4069595..a58692b 100644 --- a/cmd/fav.go +++ b/cmd/fav.go @@ -21,14 +21,11 @@ var favCmd = &cobra.Command{ You can add mensas to your favorites using the search command.`, Run: func(cmd *cobra.Command, args []string) { favorites := viper.Get("favorites").([]interface{}) - fmt.Println(favorites) s := make([]string, len(favorites)) for i, v := range favorites { s[i] = v.(string) } - fmt.Println(s, len(s), s[0]) - for i := 0; i < len(s); i++ { data, err := internal.GetMenu(s[i]) if err != nil { diff --git a/cmd/root.go b/cmd/root.go index 733714a..9c89e0c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,7 +4,6 @@ Copyright © 2024 NAME HERE package cmd import ( - "fmt" "os" "github.com/spf13/cobra" @@ -74,7 +73,7 @@ func initConfig() { viper.AutomaticEnv() if err := viper.ReadInConfig(); err == nil { - fmt.Println("Using config file:", viper.ConfigFileUsed()) + //fmt.Println("Using config file:", viper.ConfigFileUsed()) } }