viper and fav works

This commit is contained in:
Jan 2024-09-20 16:25:46 +02:00
parent 86048da2cb
commit 0f3200a8fe
5 changed files with 90 additions and 15 deletions

34
cmd/delete.go Normal file
View File

@ -0,0 +1,34 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// deleteCmd represents the delete command
var deleteCmd = &cobra.Command{
Use: "delete",
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")
},
}
func init() {
favCmd.AddCommand(deleteCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// deleteCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// deleteCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

View File

@ -5,8 +5,10 @@ package cmd
import ( import (
"fmt" "fmt"
"strings"
"github.com/LeRoid-hub/Mensa-CLI/internal"
"github.com/fatih/color"
"github.com/rodaine/table"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -18,13 +20,40 @@ var favCmd = &cobra.Command{
Long: `Retrieves the favorite mensa menus for the current day. Long: `Retrieves the favorite mensa menus for the current day.
You can add mensas to your favorites using the search command.`, You can add mensas to your favorites using the search command.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
favorites := viper.GetStringSlice("favorites") favorites := viper.Get("favorites").([]interface{})
if len(favorites) == 0 { fmt.Println(favorites)
fmt.Println("You have no favorites")
return
}
fmt.Println("Your favorites: " + strings.Join(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 {
fmt.Println("Error fetching data")
}
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()
tbl := table.New("Offering", "Dish", "Price")
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)
color.Green("Mensa: %s", data.Name)
for _, day := range data.Days {
for _, menu := range day.Menu {
for _, meal := range menu.Meal {
tbl.AddRow(menu.Name, meal.Name, meal.Price)
}
}
}
tbl.Print()
fmt.Println()
}
}, },
} }

View File

@ -53,8 +53,9 @@ func init() {
} }
func setDefaults() { func setDefaults() {
favorites := []string{}
viper.SetDefault("Server", "https://mensa.barfuss.email") viper.SetDefault("Server", "https://mensa.barfuss.email")
viper.SetDefault("favorites", []string{}) viper.SetDefault("favorites", favorites)
} }
func initConfig() { func initConfig() {

View File

@ -91,7 +91,7 @@ var searchCmd = &cobra.Command{
} }
if result == "menu" { if result == "menu" {
data, err := internal.GetMenu(selectedCity, selectedMensa) data, err := internal.GetSearchMenu(selectedCity, selectedMensa)
if err != nil { if err != nil {
fmt.Println("Error fetching data") fmt.Println("Error fetching data")
} }
@ -113,12 +113,19 @@ var searchCmd = &cobra.Command{
} }
if result == "favorites" { if result == "favorites" {
favorites := viper.Get("favorites").([]string) favorites := viper.Get("favorites").([]interface{})
favorites = append(favorites, selectedCity+"/"+selectedMensa)
s := make([]string, len(favorites))
for i, v := range favorites {
s[i] = v.(string)
}
s = append(s, selectedCity+"/"+selectedMensa)
viper.Set("favorites", s)
fmt.Println("Added " + selectedCity + "/" + selectedMensa + " to your favorites") fmt.Println("Added " + selectedCity + "/" + selectedMensa + " to your favorites")
viper.Set("favorites", favorites)
fmt.Println(viper.Get("favorites")) fmt.Println(viper.Get("favorites"))
viper.SafeWriteConfig() viper.WriteConfig()
} }
}, },

View File

@ -9,8 +9,8 @@ import (
"github.com/LeRoid-hub/Mensa-CLI/models" "github.com/LeRoid-hub/Mensa-CLI/models"
) )
func GetMenu(city string, mensa string) (models.Mensa, error) { func GetMenu(mensa string) (models.Mensa, error) {
http, err := http.Get("https://mensa.barfuss.email/mensa/" + city + "/" + mensa) http, err := http.Get("https://mensa.barfuss.email/mensa/" + mensa)
if err != nil { if err != nil {
return models.Mensa{}, err return models.Mensa{}, err
} }
@ -31,3 +31,7 @@ func GetMenu(city string, mensa string) (models.Mensa, error) {
return data, nil return data, nil
} }
func GetSearchMenu(city string, mensa string) (models.Mensa, error) {
return GetMenu(city + "/" + mensa)
}