Merge branch 'main' of github.com:LeRoid-hub/Mensa-CLI

This commit is contained in:
Jan 2024-09-20 16:56:42 +02:00
commit e0641d03e0
5 changed files with 118 additions and 17 deletions

64
cmd/delete.go Normal file
View File

@ -0,0 +1,64 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// 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) {
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()
},
}
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 (
"fmt"
"strings"
"github.com/LeRoid-hub/Mensa-CLI/internal"
"github.com/fatih/color"
"github.com/rodaine/table"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@ -18,13 +20,37 @@ var favCmd = &cobra.Command{
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) {
favorites := viper.GetStringSlice("favorites")
if len(favorites) == 0 {
fmt.Println("You have no favorites")
return
}
favorites := viper.Get("favorites").([]interface{})
fmt.Println("Your favorites: " + strings.Join(favorites, ", "))
s := make([]string, len(favorites))
for i, v := range favorites {
s[i] = v.(string)
}
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

@ -4,7 +4,6 @@ Copyright © 2024 NAME HERE <EMAIL ADDRESS>
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
@ -53,8 +52,9 @@ func init() {
}
func setDefaults() {
favorites := []string{}
viper.SetDefault("Server", "https://mensa.barfuss.email")
viper.SetDefault("favorites", []string{})
viper.SetDefault("favorites", favorites)
}
func initConfig() {
@ -73,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())
}
}

View File

@ -91,7 +91,7 @@ var searchCmd = &cobra.Command{
}
if result == "menu" {
data, err := internal.GetMenu(selectedCity, selectedMensa)
data, err := internal.GetSearchMenu(selectedCity, selectedMensa)
if err != nil {
fmt.Println("Error fetching data")
}
@ -113,12 +113,19 @@ var searchCmd = &cobra.Command{
}
if result == "favorites" {
favorites := viper.Get("favorites").([]string)
favorites = append(favorites, selectedCity+"/"+selectedMensa)
favorites := viper.Get("favorites").([]interface{})
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")
viper.Set("favorites", favorites)
fmt.Println(viper.Get("favorites"))
viper.SafeWriteConfig()
viper.WriteConfig()
}
},

View File

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