Cli: fix filtering devices by name

This commit is contained in:
andig 2023-12-07 08:18:01 +01:00
parent dca99c30bc
commit 10c131647d
6 changed files with 33 additions and 47 deletions

View file

@ -174,12 +174,16 @@ func loadConfigFile(conf *globalConfig) error {
return err
}
func configureMeters(static []config.Named) error {
func configureMeters(static []config.Named, names ...string) error {
for i, cc := range static {
if cc.Name == "" {
return fmt.Errorf("cannot create meter %d: missing name", i+1)
}
if len(names) > 0 && !slices.Contains(names, cc.Name) {
continue
}
instance, err := meter.NewFromConfig(cc.Type, cc.Other)
if err != nil {
return fmt.Errorf("cannot create meter '%s': %w", cc.Name, err)
@ -199,6 +203,10 @@ func configureMeters(static []config.Named) error {
for _, conf := range configurable {
cc := conf.Named()
if len(names) > 0 && !slices.Contains(names, cc.Name) {
return nil
}
instance, err := meter.NewFromConfig(cc.Type, cc.Other)
if err != nil {
return fmt.Errorf("cannot create meter '%s': %w", cc.Name, err)
@ -212,7 +220,7 @@ func configureMeters(static []config.Named) error {
return nil
}
func configureChargers(static []config.Named) error {
func configureChargers(static []config.Named, names ...string) error {
g, _ := errgroup.WithContext(context.Background())
for i, cc := range static {
@ -220,6 +228,10 @@ func configureChargers(static []config.Named) error {
return fmt.Errorf("cannot create charger %d: missing name", i+1)
}
if len(names) > 0 && !slices.Contains(names, cc.Name) {
continue
}
cc := cc
g.Go(func() error {
instance, err := charger.NewFromConfig(cc.Type, cc.Other)
@ -241,6 +253,11 @@ func configureChargers(static []config.Named) error {
conf := conf
g.Go(func() error {
cc := conf.Named()
if len(names) > 0 && !slices.Contains(names, cc.Name) {
return nil
}
instance, err := charger.NewFromConfig(cc.Type, cc.Other)
if err != nil {
return fmt.Errorf("cannot create charger '%s': %w", cc.Name, err)
@ -275,7 +292,7 @@ func vehicleInstance(cc config.Named) (api.Vehicle, error) {
return instance, nil
}
func configureVehicles(static []config.Named) error {
func configureVehicles(static []config.Named, names ...string) error {
var mu sync.Mutex
g, _ := errgroup.WithContext(context.Background())
@ -287,6 +304,10 @@ func configureVehicles(static []config.Named) error {
return fmt.Errorf("cannot create vehicle %d: missing name", i+1)
}
if len(names) > 0 && !slices.Contains(names, cc.Name) {
continue
}
cc := cc
g.Go(func() error {
instance, err := vehicleInstance(cc)
@ -315,6 +336,11 @@ func configureVehicles(static []config.Named) error {
conf := conf
g.Go(func() error {
cc := conf.Named()
if len(names) > 0 && !slices.Contains(names, cc.Name) {
return nil
}
instance, err := vehicleInstance(cc)
if err != nil {
return fmt.Errorf("cannot create vehicle '%s': %w", cc.Name, err)