...

Source file src/edge-infra.dev/pkg/lib/kernel/devices/rules.go

Documentation: edge-infra.dev/pkg/lib/kernel/devices

     1  package devices
     2  
     3  import (
     4  	"github.com/gobwas/glob"
     5  )
     6  
     7  // ruleType is the type of rule (attribute or property)
     8  type ruleType int8
     9  
    10  const (
    11  	// match by attribute
    12  	attributeRule ruleType = iota
    13  	// match by property
    14  	propertyRule
    15  )
    16  
    17  // Rule is a property/attribute rule to match by
    18  type Rule struct {
    19  	name string
    20  	ruleType
    21  	glob      glob.Glob
    22  	subsystem string
    23  }
    24  
    25  // NewPropRule will return a new property rule
    26  func NewPropRule(property string, value string) *Rule {
    27  	rule := Rule{
    28  		name:     property,
    29  		ruleType: propertyRule,
    30  	}
    31  	if property == "SUBSYSTEM" {
    32  		rule.subsystem = value
    33  	}
    34  	g, err := glob.Compile(value)
    35  	if err == nil {
    36  		rule.glob = g
    37  	}
    38  	return &rule
    39  }
    40  
    41  // NewAttributeRule will return a new attribute rule
    42  func NewAttrRule(attribute string, value string) *Rule {
    43  	rule := Rule{
    44  		name:     attribute,
    45  		ruleType: attributeRule,
    46  	}
    47  	g, err := glob.Compile(value)
    48  	if err == nil {
    49  		rule.glob = g
    50  	}
    51  	return &rule
    52  }
    53  
    54  // isAttributeEqual attempts to find and match the device attribute
    55  func (r *Rule) isAttributeEqual(device Device) bool {
    56  	attrValue, found, _ := device.Attribute(r.name)
    57  	if !found {
    58  		return false
    59  	}
    60  	return r.glob.Match(attrValue)
    61  }
    62  
    63  // isPropertyEqual attempts to find and match the device property
    64  func (r *Rule) isPropertyEqual(device Device) bool {
    65  	propValue, found, _ := device.Property(r.name)
    66  	if !found {
    67  		return false
    68  	}
    69  	return r.glob.Match(propValue)
    70  }
    71  

View as plain text