diff options
author | Sean E. Russell <ser@ser1.net> | 2020-04-23 13:01:13 -0500 |
---|---|---|
committer | Sean E. Russell <ser@ser1.net> | 2020-04-23 13:01:13 -0500 |
commit | 6cee395f0636e17174881fa88e4a34f1f1e6a3dc (patch) | |
tree | e6b80632530cc635dd258346704bc81f67d0d0ce /devices/devices.go | |
parent | 5ada5315d940038fcd83777b1e73797bbbada108 (diff) |
Add sensible defaults for temps. Improve config writing.
Diffstat (limited to 'devices/devices.go')
-rw-r--r-- | devices/devices.go | 37 |
1 files changed, 26 insertions, 11 deletions
diff --git a/devices/devices.go b/devices/devices.go index efdc404..4a029cd 100644 --- a/devices/devices.go +++ b/devices/devices.go @@ -3,12 +3,13 @@ package devices import "log" const ( - Temperatures = "Temperatures" + Temperatures = "Temperatures" // Device domain for temperature sensors ) var Domains []string = []string{Temperatures} -var shutdownFuncs []func() error +var _shutdownFuncs []func() error var _devs map[string][]string +var _defaults map[string][]string // RegisterShutdown stores a function to be called by gotop on exit, allowing // extensions to properly release resources. Extensions should register a @@ -16,14 +17,14 @@ var _devs map[string][]string // released. The returned error will be logged, but no other action will be // taken. func RegisterShutdown(f func() error) { - shutdownFuncs = append(shutdownFuncs, f) + _shutdownFuncs = append(_shutdownFuncs, f) } // Shutdown will be called by the `main()` function if gotop is exited // cleanly. It will call all of the registered shutdown functions of devices, // logging all errors but otherwise not responding to them. func Shutdown() { - for _, f := range shutdownFuncs { + for _, f := range _shutdownFuncs { err := f() if err != nil { log.Print(err) @@ -31,17 +32,31 @@ func Shutdown() { } } -func RegisterDeviceList(typ string, f func() []string) { +func RegisterDeviceList(typ string, all func() []string, def func() []string) { if _devs == nil { _devs = make(map[string][]string) } - if ls, ok := _devs[typ]; ok { - _devs[typ] = append(ls, f()...) - return + if _defaults == nil { + _defaults = make(map[string][]string) } - _devs[typ] = f() + if _, ok := _devs[typ]; !ok { + _devs[typ] = []string{} + } + _devs[typ] = append(_devs[typ], all()...) + if _, ok := _defaults[typ]; !ok { + _defaults[typ] = []string{} + } + _defaults[typ] = append(_defaults[typ], def()...) } -func Devices(domain string) []string { - return _devs[domain] +// Return a list of devices registered under domain, where `domain` is one of the +// defined constants in `devices`, e.g., devices.Temperatures. The +// `enabledOnly` flag determines whether all devices are returned (false), or +// only the ones that have been enabled for the domain. +func Devices(domain string, all bool) []string { + if all { + return _devs[domain] + } else { + return _defaults[domain] + } } |