mostly os search

This commit is contained in:
Jan 2024-09-18 12:08:11 +02:00
parent b1781673b0
commit d4e79ed688
6 changed files with 162 additions and 2 deletions

View File

@ -1,6 +1,5 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

View File

@ -5,7 +5,10 @@ package cmd
import (
"fmt"
"strings"
"github.com/LeRoid-hub/Mensa-CLI/internal"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
)
@ -20,7 +23,84 @@ 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) {
fmt.Println("search called")
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
}
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
}
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" {
menu, err := internal.GetMenu(result)
if err != nil {
fmt.Println("Error fetching data")
}
fmt.Println(menu)
}
if result == "favorites" {
// viper
}
},
}

View File

@ -1 +1,39 @@
package internal
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
func GetState(state string) (string, error) {
fmt.Println("Get all called")
http, err := http.Get("https://mensa.barfuss.email/state/" + state)
if err != nil {
return "", err
}
defer http.Body.Close()
body, err := io.ReadAll(http.Body)
if err != nil {
return "", err
}
data := string(body)
var cities []string
err = json.Unmarshal([]byte(data), &cities)
if err != nil {
return "", err
}
var result string
for _, city := range cities {
result += city + ","
}
city := result[:len(result)-1]
return city, nil
}

View File

@ -1 +1,39 @@
package internal
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
func GetMensen(city string) (string, error) {
fmt.Println("Get all called")
http, err := http.Get("https://mensa.barfuss.email/city/" + city)
if err != nil {
return "", err
}
defer http.Body.Close()
body, err := io.ReadAll(http.Body)
if err != nil {
return "", err
}
data := string(body)
var mensen []string
err = json.Unmarshal([]byte(data), &mensen)
if err != nil {
return "", err
}
var result string
for _, mensa := range mensen {
result += mensa + ","
}
mensa := result[:len(result)-1]
return mensa, nil
}

5
internal/getMenu.go Normal file
View File

@ -0,0 +1,5 @@
package internal
func GetMenu(mensa string) (string, error) {
return "", nil
}