-
Notifications
You must be signed in to change notification settings - Fork 4
/
host_query.go
67 lines (62 loc) · 1.63 KB
/
host_query.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package psutilsql
import (
"github.com/noborus/trdsql"
"github.com/shirou/gopsutil/host"
)
// HostInfoReader returns host.Info result as trdsql.SliceReader.
func HostInfoReader() (*trdsql.SliceReader, error) {
v, err := host.Info()
if err != nil {
return nil, err
}
return trdsql.NewSliceReader(psHostInfo, v), nil
}
// HostUsersReader returns host.Users result as trdsql.SliceReader.
func HostUsersReader() (*trdsql.SliceReader, error) {
v, err := host.Users()
if err != nil {
return nil, err
}
return trdsql.NewSliceReader(psHostUser, v), nil
}
// HostTemperatureReader returns host.SensorsTemperatures result as trdsql.SliceReader.
func HostTemperatureReader() (*trdsql.SliceReader, error) {
v, err := host.SensorsTemperatures()
if err != nil {
return nil, err
}
return trdsql.NewSliceReader(psHostTemperature, v), nil
}
// HostQuery executes SQL on host.Info or host.Users or host.SensorsTemperatures.
func HostQuery(tempera bool, users bool, query string, w trdsql.Writer) error {
if tempera {
reader, err := HostTemperatureReader()
if err != nil {
return err
}
defaultQuery := "SELECT * FROM " + psHostTemperature
if query == "" {
query = defaultQuery
}
return readerExec(reader, query, w)
} else if users {
reader, err := HostUsersReader()
if err != nil {
return err
}
defaultQuery := "SELECT * FROM " + psHostUser
if query == "" {
query = defaultQuery
}
return readerExec(reader, query, w)
}
reader, err := HostInfoReader()
if err != nil {
return err
}
defaultQuery := "SELECT * FROM " + psHostInfo
if query == "" {
query = defaultQuery
}
return readerExec(reader, query, w)
}