mirror of
https://github.com/LeRoid-hub/Mensa-CLI.git
synced 2025-01-31 03:34:57 +00:00
mostly os search
This commit is contained in:
parent
b1781673b0
commit
d4e79ed688
@ -1,6 +1,5 @@
|
||||
/*
|
||||
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
|
||||
|
||||
*/
|
||||
package cmd
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
5
internal/getMenu.go
Normal file
@ -0,0 +1,5 @@
|
||||
package internal
|
||||
|
||||
func GetMenu(mensa string) (string, error) {
|
||||
return "", nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user