Mensa-CLI/cmd/fav.go

71 lines
1.7 KiB
Go
Raw Normal View History

2024-09-17 21:02:36 +00:00
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
2024-09-20 14:25:46 +00:00
"github.com/LeRoid-hub/Mensa-CLI/internal"
"github.com/fatih/color"
"github.com/rodaine/table"
2024-09-17 21:02:36 +00:00
"github.com/spf13/cobra"
2024-09-19 21:11:28 +00:00
"github.com/spf13/viper"
2024-09-17 21:02:36 +00:00
)
// favCmd represents the fav command
var favCmd = &cobra.Command{
Use: "fav",
2024-09-19 21:11:28 +00:00
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.`,
2024-09-17 21:02:36 +00:00
Run: func(cmd *cobra.Command, args []string) {
2024-09-20 14:25:46 +00:00
favorites := viper.Get("favorites").([]interface{})
s := make([]string, len(favorites))
for i, v := range favorites {
s[i] = v.(string)
2024-09-19 21:11:28 +00:00
}
2024-09-20 14:25:46 +00:00
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)
2024-09-19 21:11:28 +00:00
2024-09-20 14:25:46 +00:00
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()
}
2024-09-19 21:11:28 +00:00
2024-09-17 21:02:36 +00:00
},
}
func init() {
rootCmd.AddCommand(favCmd)
2024-09-17 21:02:36 +00:00
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// favCmd.PersistentFlags().String("foo", "", "A help for foo")
2024-09-17 21:02:36 +00:00
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// favCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
2024-09-17 21:02:36 +00:00
}