Skip to content

Commit

Permalink
chore: use eg for errgroup
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Oct 3, 2024
1 parent 3b6de6f commit 7d2256f
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"cmp"
"context"
"errors"
"fmt"
"net/http"
Expand Down Expand Up @@ -204,7 +203,7 @@ NEXT:
}

func configureMeters(static []config.Named, names ...string) error {
g, _ := errgroup.WithContext(context.Background())
var eg errgroup.Group

for i, cc := range static {
if cc.Name == "" {
Expand All @@ -219,7 +218,7 @@ func configureMeters(static []config.Named, names ...string) error {
log.WARN.Printf("create meter %d: %v", i+1, err)
}

g.Go(func() error {
eg.Go(func() error {
instance, err := meter.NewFromConfig(cc.Type, cc.Other)
if err != nil {
return &DeviceError{cc.Name, fmt.Errorf("cannot create meter '%s': %w", cc.Name, err)}
Expand All @@ -240,7 +239,7 @@ func configureMeters(static []config.Named, names ...string) error {
}

for _, conf := range configurable {
g.Go(func() error {
eg.Go(func() error {
cc := conf.Named()

if len(names) > 0 && !slices.Contains(names, cc.Name) {
Expand All @@ -262,11 +261,11 @@ func configureMeters(static []config.Named, names ...string) error {
})
}

return g.Wait()
return eg.Wait()
}

func configureChargers(static []config.Named, names ...string) error {
g, _ := errgroup.WithContext(context.Background())
var eg errgroup.Group

for i, cc := range static {
if cc.Name == "" {
Expand All @@ -281,7 +280,7 @@ func configureChargers(static []config.Named, names ...string) error {
log.WARN.Printf("create charger %d: %v", i+1, err)
}

g.Go(func() error {
eg.Go(func() error {
instance, err := charger.NewFromConfig(cc.Type, cc.Other)
if err != nil {
return &DeviceError{cc.Name, fmt.Errorf("cannot create charger '%s': %w", cc.Name, err)}
Expand All @@ -302,7 +301,7 @@ func configureChargers(static []config.Named, names ...string) error {
}

for _, conf := range configurable {
g.Go(func() error {
eg.Go(func() error {
cc := conf.Named()

if len(names) > 0 && !slices.Contains(names, cc.Name) {
Expand All @@ -324,7 +323,7 @@ func configureChargers(static []config.Named, names ...string) error {
})
}

return g.Wait()
return eg.Wait()
}

func vehicleInstance(cc config.Named) (api.Vehicle, error) {
Expand All @@ -351,7 +350,7 @@ func vehicleInstance(cc config.Named) (api.Vehicle, error) {

func configureVehicles(static []config.Named, names ...string) error {
var mu sync.Mutex
g, _ := errgroup.WithContext(context.Background())
var eg errgroup.Group

// stable-sort vehicles by name
devs1 := make([]config.Device[api.Vehicle], 0, len(static))
Expand All @@ -369,7 +368,7 @@ func configureVehicles(static []config.Named, names ...string) error {
return fmt.Errorf("cannot create vehicle %d: %w", i+1, err)
}

g.Go(func() error {
eg.Go(func() error {
instance, err := vehicleInstance(cc)
if err != nil {
return fmt.Errorf("cannot create vehicle '%s': %w", cc.Name, err)
Expand All @@ -393,7 +392,7 @@ func configureVehicles(static []config.Named, names ...string) error {
devs2 := make([]config.ConfigurableDevice[api.Vehicle], 0, len(configurable))

for _, conf := range configurable {
g.Go(func() error {
eg.Go(func() error {
cc := conf.Named()

if len(names) > 0 && !slices.Contains(names, cc.Name) {
Expand All @@ -413,7 +412,7 @@ func configureVehicles(static []config.Named, names ...string) error {
})
}

if err := g.Wait(); err != nil {
if err := eg.Wait(); err != nil {
return err
}

Expand Down Expand Up @@ -813,13 +812,13 @@ func configureTariffs(conf globalconfig.Tariffs) (*tariff.Tariffs, error) {
tariffs.Currency = currency.MustParseISO(conf.Currency)
}

g, _ := errgroup.WithContext(context.Background())
g.Go(func() error { return configureTariff("grid", conf.Grid, &tariffs.Grid) })
g.Go(func() error { return configureTariff("feedin", conf.FeedIn, &tariffs.FeedIn) })
g.Go(func() error { return configureTariff("co2", conf.Co2, &tariffs.Co2) })
g.Go(func() error { return configureTariff("planner", conf.Planner, &tariffs.Planner) })
var eg errgroup.Group
eg.Go(func() error { return configureTariff("grid", conf.Grid, &tariffs.Grid) })
eg.Go(func() error { return configureTariff("feedin", conf.FeedIn, &tariffs.FeedIn) })
eg.Go(func() error { return configureTariff("co2", conf.Co2, &tariffs.Co2) })
eg.Go(func() error { return configureTariff("planner", conf.Planner, &tariffs.Planner) })

if err := g.Wait(); err != nil {
if err := eg.Wait(); err != nil {
return nil, &ClassError{ClassTariff, err}
}

Expand Down

0 comments on commit 7d2256f

Please sign in to comment.