-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
83 lines (70 loc) · 1.72 KB
/
config.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
. "github.com/gdubicki/dynamotd/dynamotd"
. "github.com/gdubicki/dynamotd/plugins"
"github.com/spf13/viper"
)
var MaxRows = 40
var config = viper.New()
func init() {
config.SetDefault("rows", []string{
"timestamp",
"",
"fqdn",
"ip",
"uptime",
"",
"load",
"memory",
"diskspace",
})
config.SetConfigName("dynamotd.yaml")
config.AddConfigPath("/etc/")
config.AddConfigPath("$HOME/.dynamotd")
config.AddConfigPath(".")
err := config.ReadInConfig()
if err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// that's ok - we don't need a config file for this app. the defaults are enough to run it.
} else {
panic(fmt.Errorf("Fatal error in config file: %s \n", err))
}
}
}
func GetRows() []Row {
var rows []Row
rowsStrings := config.GetStringSlice("rows")
if len(rowsStrings) == 0 {
panic(fmt.Errorf("no rows defined"))
}
if len(rowsStrings) > MaxRows {
panic(fmt.Errorf("more rows than %d not supported", MaxRows))
}
for _, rowString := range rowsStrings {
// TODO: consider using reflection to be able to add row types without editing this file
switch rowString {
case "", "emptyLine":
rows = append(rows, EmptyLine())
case "timestamp":
rows = append(rows, Timestamp())
case "fqdn":
rows = append(rows, Fqdn())
case "load":
rows = append(rows, Load())
case "ip":
rows = append(rows, Ip())
case "uptime":
rows = append(rows, Uptime())
case "cores":
rows = append(rows, Cores())
case "memory":
rows = append(rows, Memory())
case "diskspace":
rows = append(rows, DiskSpace("/"))
default:
panic(fmt.Errorf("error while generating row from string '%s' - check for typos", rowString))
}
}
return rows
}