| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package agent |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | |
| 8 | "gopkg.in/yaml.v2" |
| 9 | ) |
| 10 | |
| 11 | func defaultConfig() config { |
| 12 | return config{ |
| 13 | Enabled: true, |
| 14 | DefaultRun: true, |
| 15 | MaxProcs: 0, |
| 16 | Modules: nil, |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | type config struct { |
| 21 | Enabled bool `yaml:"enabled"` |
| 22 | DefaultRun bool `yaml:"default_run"` |
| 23 | MaxProcs int `yaml:"max_procs"` |
| 24 | Modules map[string]bool `yaml:"modules"` |
| 25 | } |
| 26 | |
| 27 | func (c *config) String() string { |
| 28 | return fmt.Sprintf("enabled '%v', default_run '%v', max_procs '%d'", |
| 29 | c.Enabled, c.DefaultRun, c.MaxProcs) |
| 30 | } |
| 31 | |
| 32 | func (c *config) isExplicitlyEnabled(moduleName string) bool { |
| 33 | return c.isEnabled(moduleName, true) |
| 34 | } |
| 35 | |
| 36 | func (c *config) isImplicitlyEnabled(moduleName string) bool { |
| 37 | return c.isEnabled(moduleName, false) |
| 38 | } |
| 39 | |
| 40 | func (c *config) isEnabled(moduleName string, explicit bool) bool { |
| 41 | if enabled, ok := c.Modules[moduleName]; ok { |
| 42 | return enabled |
| 43 | } |
| 44 | if explicit { |
| 45 | return false |
| 46 | } |
| 47 | return c.DefaultRun |
| 48 | } |
| 49 | |
| 50 | func (c *config) UnmarshalYAML(unmarshal func(any) error) error { |
| 51 | type plain config |
| 52 | if err := unmarshal((*plain)(c)); err != nil { |
| 53 | return err |
| 54 | } |
| 55 | |
| 56 | var m map[string]any |
| 57 | if err := unmarshal(&m); err != nil { |
| 58 | return err |
| 59 | } |
| 60 | |
| 61 | for key, value := range m { |
| 62 | switch key { |
| 63 | case "enabled", "default_run", "max_procs", "modules": |
| 64 | continue |
| 65 | } |
| 66 | var b bool |
| 67 | if in, err := yaml.Marshal(value); err != nil || yaml.Unmarshal(in, &b) != nil { |
| 68 | continue |
| 69 | } |
| 70 | if c.Modules == nil { |
| 71 | c.Modules = make(map[string]bool) |
| 72 | } |
| 73 | c.Modules[key] = b |
| 74 | } |
| 75 | return nil |
| 76 | } |