2026-01-24 15:04:42 -08:00
|
|
|
package tendrils
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Config struct {
|
2026-01-24 16:00:26 -08:00
|
|
|
Locations []*Location `yaml:"locations" json:"locations"`
|
2026-01-24 15:04:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Location struct {
|
2026-01-31 09:52:51 -08:00
|
|
|
Name string `yaml:"name" json:"name"`
|
|
|
|
|
Direction string `yaml:"direction,omitempty" json:"direction,omitempty"`
|
|
|
|
|
Nodes []*NodeConfig `yaml:"nodes,omitempty" json:"nodes,omitempty"`
|
|
|
|
|
Children []*Location `yaml:"children,omitempty" json:"children,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type NodeConfig struct {
|
|
|
|
|
Names []string `yaml:"names,omitempty" json:"names,omitempty"`
|
|
|
|
|
MACs []string `yaml:"macs,omitempty" json:"macs,omitempty"`
|
|
|
|
|
IPs []string `yaml:"ips,omitempty" json:"ips,omitempty"`
|
|
|
|
|
Avoid bool `yaml:"avoid,omitempty" json:"avoid,omitempty"`
|
2026-01-24 15:04:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func LoadConfig(path string) (*Config, error) {
|
|
|
|
|
if path == "" {
|
|
|
|
|
return &Config{}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := os.ReadFile(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var cfg Config
|
|
|
|
|
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &cfg, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Config) ToJSON() ([]byte, error) {
|
|
|
|
|
return json.Marshal(c)
|
|
|
|
|
}
|
2026-01-31 09:52:51 -08:00
|
|
|
|
|
|
|
|
func (c *Config) AllNodeConfigs() []*NodeConfig {
|
|
|
|
|
var result []*NodeConfig
|
|
|
|
|
var visit func([]*Location)
|
|
|
|
|
visit = func(locs []*Location) {
|
|
|
|
|
for _, loc := range locs {
|
|
|
|
|
result = append(result, loc.Nodes...)
|
|
|
|
|
visit(loc.Children)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
visit(c.Locations)
|
|
|
|
|
return result
|
|
|
|
|
}
|