Mensa-CLI/cmd/search.go

139 lines
3.2 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-18 10:08:11 +00:00
"strings"
2024-09-17 21:02:36 +00:00
2024-09-18 10:08:11 +00:00
"github.com/LeRoid-hub/Mensa-CLI/internal"
2024-09-18 17:08:47 +00:00
"github.com/fatih/color"
2024-09-18 10:08:11 +00:00
"github.com/manifoldco/promptui"
2024-09-18 17:08:47 +00:00
"github.com/rodaine/table"
2024-09-17 21:02:36 +00:00
"github.com/spf13/cobra"
)
// searchCmd represents the search command
var searchCmd = &cobra.Command{
Use: "search",
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.`,
Run: func(cmd *cobra.Command, args []string) {
2024-09-18 10:08:11 +00:00
prompt := promptui.Select{
Label: "Select Day",
Items: []string{"baden-wuerttemberg", "bayern", "berlin", "brandenburg", "bremen",
"hamburg", "hessen", "mecklenburg-vorpommern", "niedersachsen", "nordrhein-westfalen", "rheinland-pfalz", "saarland", "sachsen", "sachsen-anhalt", "schleswig-holstein", "thueringen"},
}
_, result, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
cities, err := internal.GetState(result)
if err != nil {
fmt.Println("Error fetching data")
}
city := strings.Split(cities, ",")
prompt = promptui.Select{
Label: "Select City",
Items: city,
}
_, result, err = prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
2024-09-18 17:08:47 +00:00
selectedCity := result
2024-09-18 10:08:11 +00:00
mensen, err := internal.GetMensen(result)
if err != nil {
fmt.Println("Error fetching data")
}
mensa := strings.Split(mensen, ",")
prompt = promptui.Select{
Label: "Select Mensa",
Items: mensa,
}
_, result, err = prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
2024-09-18 17:08:47 +00:00
selectedMensa := result
2024-09-18 10:08:11 +00:00
prompt = promptui.Select{
Label: "Do you want to see the menu or save the mensa to your favorites?",
Items: []string{"menu", "favorites"},
}
_, result, err = prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
fmt.Printf("You choose %q\n", result)
if result == "menu" {
2024-09-18 17:08:47 +00:00
data, err := internal.GetMenu(selectedCity, selectedMensa)
2024-09-18 10:08:11 +00:00
if err != nil {
fmt.Println("Error fetching data")
}
2024-09-18 17:08:47 +00:00
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)
for _, day := range data.Days {
for _, menu := range day.Menu {
for _, meal := range menu.Meal {
tbl.AddRow(menu.Name, meal.Name, meal.Price)
}
}
}
2024-09-18 10:08:11 +00:00
2024-09-18 17:08:47 +00:00
tbl.Print()
2024-09-18 10:08:11 +00:00
}
if result == "favorites" {
// viper
}
2024-09-17 21:02:36 +00:00
},
}
func init() {
rootCmd.AddCommand(searchCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// searchCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
searchCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}