summaryrefslogtreecommitdiffstats
path: root/devices/devices.go
blob: efdc4042e819296f6911e13bb902fa5609a3be49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package devices

import "log"

const (
	Temperatures = "Temperatures"
)

var Domains []string = []string{Temperatures}
var shutdownFuncs []func() error
var _devs map[string][]string

// RegisterShutdown stores a function to be called by gotop on exit, allowing
// extensions to properly release resources.  Extensions should register a
// shutdown function IFF the extension is using resources that need to be
// released.  The returned error will be logged, but no other action will be
// taken.
func RegisterShutdown(f func() error) {
	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 {
		err := f()
		if err != nil {
			log.Print(err)
		}
	}
}

func RegisterDeviceList(typ string, f func() []string) {
	if _devs == nil {
		_devs = make(map[string][]string)
	}
	if ls, ok := _devs[typ]; ok {
		_devs[typ] = append(ls, f()...)
		return
	}
	_devs[typ] = f()
}

func Devices(domain string) []string {
	return _devs[domain]
}