Skip to content

Commit

Permalink
Merge pull request #2 from lukasmoellerch/dont-panic
Browse files Browse the repository at this point in the history
Don't panic
  • Loading branch information
lukasmoellerch authored Feb 17, 2022
2 parents 2f24fef + a4690f2 commit 71fa35d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/group-add.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ This command will open an editor to allow you to add the group's caanteens.`,

store, err := base.NewStore(storageDirectory)
if err != nil {
panic(err)
return err
}
if err := syncStore(ctx, store); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/group-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var groupUpdateCmd = &cobra.Command{

store, err := base.NewStore(storageDirectory)
if err != nil {
panic(err)
return err
}
if err := syncStore(ctx, store); err != nil {
return err
Expand Down
17 changes: 15 additions & 2 deletions cmd/meals.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var mealsFilterFlag string
var mealsGroupFlag string
var mealsDinnerFlag bool
var mealsLunchFlag bool
var mealsAllFlag bool
var mealsTomorrowFlag bool

// mealsCmd represents the get command
Expand All @@ -40,6 +41,13 @@ If the filter is empty, all facilities are fetched. The filter is matched agains
if mealsGroupFlag != "" && mealsFilterFlag != "" {
return fmt.Errorf("cannot use both --group and --filter")
}
if mealsGroupFlag != "" && mealsAllFlag {
return fmt.Errorf("cannot use both --group and --all")
}
if mealsFilterFlag != "" && mealsAllFlag {
return fmt.Errorf("cannot use both --filter and --all")
}

if mealsDinnerFlag && mealsDaytimeFlag != "" {
return fmt.Errorf("cannot use both --dinner and --daytime")
}
Expand Down Expand Up @@ -88,7 +96,7 @@ If the filter is empty, all facilities are fetched. The filter is matched agains
}

filtered := []*storage.CanteenData{}
if mealsFilterFlag != "" {
if mealsFilterFlag != "" || mealsAllFlag {
filtered, err = store.Filter(ctx, langFlag, mealsFilterFlag)
if err != nil {
return err
Expand Down Expand Up @@ -205,12 +213,16 @@ func renderResult(results map[string]base.CanteenMenu, canteenLabels map[string]
if !ok {
label = result.Canteen
}

s += fmt.Sprintf("\n%s\n\n", canteenNameStyle.Render(label))

for i, meal := range result.Meals {
if i != 0 {
s += "\n"
}
s += fmt.Sprintf("%s\n", headingStyle.Render(meal.Label))
if meal.Label != "" {
s += fmt.Sprintf("%s\n", headingStyle.Render(meal.Label))
}
for _, line := range meal.Description {
s += fmt.Sprintf("%s\n", descriptionStyle.Render(strings.TrimSpace(line)))
}
Expand All @@ -237,5 +249,6 @@ func init() {
mealsCmd.Flags().StringVarP(&mealsGroupFlag, "group", "g", "", "Group to fetch canteens from")
mealsCmd.Flags().BoolVarP(&mealsDinnerFlag, "dinner", "i", false, "Fetch dinner meals")
mealsCmd.Flags().BoolVarP(&mealsLunchFlag, "lunch", "l", false, "Fetch lunch meals")
mealsCmd.Flags().BoolVarP(&mealsAllFlag, "all", "a", false, "Fetch meals for all canteens")
mealsCmd.Flags().BoolVarP(&mealsTomorrowFlag, "tomorrow", "m", false, "Fetch tomorrow's meals")
}
21 changes: 11 additions & 10 deletions internal/uzh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ func (p *Provider) FetchMenus(ctx context.Context, caanteens []base.CanteenRef,
return nil
}
} else {
return fmt.Errorf("invalid daytime: %s", daytime)
return nil
}
menu, err := fetchMenuUzh(ctx, ref.ID, slug, lang, strWeekday)
if err != nil {
return err
return nil
}
menus[i] = menu
return nil
Expand All @@ -96,11 +96,11 @@ func (p *Provider) FetchCanteens(ctx context.Context, lang string) ([]base.Cante

req, err := http.NewRequest("GET", url, nil)
if err != nil {
panic(err)
return nil, err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
Expand All @@ -109,7 +109,7 @@ func (p *Provider) FetchCanteens(ctx context.Context, lang string) ([]base.Cante

doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
panic(err)
return nil, err
}

list := doc.Find("ul[role=navigation] > li > ul > li > a")
Expand Down Expand Up @@ -197,11 +197,11 @@ func fetchMenuUzh(ctx context.Context, id string, slug string, lang string, week
url := fmt.Sprintf("http://www.mensa.uzh.ch/%s/menueplaene/%s/%s.html", lang, slug, weekday)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
panic(err)
return base.CanteenMenu{}, err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
return base.CanteenMenu{}, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
Expand All @@ -210,12 +210,12 @@ func fetchMenuUzh(ctx context.Context, id string, slug string, lang string, week

doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
panic(err)
return base.CanteenMenu{}, err
}

divs := doc.Find(".newslist-description > div")
if divs.Length() != 1 {
panic("no div")
return base.CanteenMenu{}, fmt.Errorf("invalid number of divs: %d", divs.Length())
}

meals := []base.Meal{}
Expand Down Expand Up @@ -266,7 +266,8 @@ func fetchMenuUzh(ctx context.Context, id string, slug string, lang string, week
// Split by | - separating the name and the price
parts := strings.Split(s.Text(), " | ")
if len(parts) != 2 {
panic("invalid h3 tag")
loopErr = fmt.Errorf("invalid label: %s", s.Text())
return false
}
label = parts[0]
price := parts[1]
Expand Down

0 comments on commit 71fa35d

Please sign in to comment.