Skip to content

Commit

Permalink
switch: cleanup validation
Browse files Browse the repository at this point in the history
  • Loading branch information
endocrimes committed Apr 30, 2020
1 parent ebbd60c commit 316d02e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
47 changes: 40 additions & 7 deletions command/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package command

import (
"context"
"fmt"
"strings"

"github.com/endocrimes/keylight-go"
Expand All @@ -23,10 +24,14 @@ type lightDiscoverer struct {
AllLights bool
Discovery keylight.Discovery

discoveredLights []*keylight.KeyLight
discoveredLights map[string]*keylight.KeyLight
}

func (l *lightDiscoverer) runCollector(ctx context.Context) error {
if l.discoveredLights == nil {
l.discoveredLights = make(map[string]*keylight.KeyLight)
}

resultsCh := l.Discovery.ResultsCh()
for {
select {
Expand All @@ -38,28 +43,51 @@ func (l *lightDiscoverer) runCollector(ctx context.Context) error {
}

if l.AllLights {
l.discoveredLights = append(l.discoveredLights, light)
l.discoveredLights[light.Name] = light
continue
}

for _, req := range l.RequiredLights {
// TODO: Should check if the requirement is a full name or short name
// and compare differently based on the two (full match vs suffix)
if strings.HasSuffix(light.Name, req) {
l.discoveredLights = append(l.discoveredLights, light)
l.discoveredLights[light.Name] = light
}
}

// TODO: Potential bug here if a light is discovered multiple times during
// this phase (e.g flaky network or power cycling). Should probably
// store discovered lights as map[name]light to avoid this.
if len(l.discoveredLights) == len(l.RequiredLights) {
return nil
}
}
}
}

func validateAllRequiredLights(lights []*keylight.KeyLight, requirements []string) error {
if len(requirements) == 0 {
return nil
}

REQUIREMENTS:
for _, req := range requirements {
for _, light := range lights {
if strings.HasSuffix(light.Name, req) {
continue REQUIREMENTS
}
}
return fmt.Errorf("no light found for requirement '%s'", req)
}

return nil
}

func (l *lightDiscoverer) DiscoveredLights() []*keylight.KeyLight {
var result []*keylight.KeyLight
for _, light := range l.discoveredLights {
result = append(result, light)
}
return result
}

func (l *lightDiscoverer) Run(ctx context.Context) ([]*keylight.KeyLight, error) {
childCtx, cancelFn := context.WithCancel(ctx)
defer cancelFn()
Expand All @@ -84,5 +112,10 @@ func (l *lightDiscoverer) Run(ctx context.Context) ([]*keylight.KeyLight, error)
return nil, discoveryErr
}

return l.discoveredLights, nil
lights := l.DiscoveredLights()
err = validateAllRequiredLights(lights, l.RequiredLights)
if err != nil {
return nil, err
}
return lights, nil
}
21 changes: 1 addition & 20 deletions command/switch_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,7 @@ func (c *SwitchCommand) Run(args []string) int {
return 1
}

valid := validateAllRequiredLights(found, lights)
if !valid {
c.UI.Error("Missing required lights, found:")
for _, light := range found {
c.UI.Error(fmt.Sprintf("- %s", light.Name))
}
return 1
}

updateCtx, updateCancelFn := context.WithTimeout(context.Background(), 5*time.Second)
updateCtx, updateCancelFn := context.WithTimeout(context.Background(), 15*time.Second)
defer updateCancelFn()

for _, light := range found {
Expand All @@ -151,13 +142,3 @@ func (c *SwitchCommand) Run(args []string) int {

return 0
}

func validateAllRequiredLights(lights []*keylight.KeyLight, requirements []string) bool {
if len(requirements) == 0 {
return true
}

// TODO: Implement me.

return true
}

0 comments on commit 316d02e

Please sign in to comment.