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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
package gotop
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/shibukawa/configdir"
"github.com/xxxserxxx/gotop/v4/colorschemes"
"github.com/xxxserxxx/gotop/v4/widgets"
)
// CONFFILE is the name of the default config file
const CONFFILE = "gotop.conf"
type Config struct {
ConfigDir configdir.ConfigDir
GraphHorizontalScale int
HelpVisible bool
Colorscheme colorschemes.Colorscheme
UpdateInterval time.Duration
AverageLoad bool
PercpuLoad bool
Statusbar bool
TempScale widgets.TempScale
NetInterface string
Layout string
MaxLogSize int64
ExportPort string
Mbps bool
Temps []string
Test bool
ExtensionVars map[string]string
ConfigFile string
}
func NewConfig() Config {
cd := configdir.New("", "gotop")
cd.LocalPath, _ = filepath.Abs(".")
conf := Config{
ConfigDir: cd,
GraphHorizontalScale: 7,
HelpVisible: false,
UpdateInterval: time.Second,
AverageLoad: false,
PercpuLoad: true,
TempScale: widgets.Celsius,
Statusbar: false,
NetInterface: widgets.NetInterfaceAll,
MaxLogSize: 5000000,
Layout: "default",
ExtensionVars: make(map[string]string),
}
conf.Colorscheme, _ = colorschemes.FromName(conf.ConfigDir, "default")
folder := conf.ConfigDir.QueryFolderContainsFile(CONFFILE)
if folder != nil {
conf.ConfigFile = filepath.Join(folder.Path, CONFFILE)
}
return conf
}
func (conf *Config) Load() error {
var in []byte
if conf.ConfigFile == "" {
return nil
}
var err error
if _, err = os.Stat(conf.ConfigFile); os.IsNotExist(err) {
// Check for the file in the usual suspects
folder := conf.ConfigDir.QueryFolderContainsFile(conf.ConfigFile)
if folder == nil {
return nil
}
conf.ConfigFile = filepath.Join(folder.Path, conf.ConfigFile)
}
if in, err = ioutil.ReadFile(conf.ConfigFile); err != nil {
return err
}
return load(bytes.NewReader(in), conf)
}
func load(in io.Reader, conf *Config) error {
r := bufio.NewScanner(in)
var lineNo int
for r.Scan() {
l := strings.TrimSpace(r.Text())
if l[0] == '#' {
continue
}
kv := strings.Split(l, "=")
if len(kv) != 2 {
return fmt.Errorf("bad config file syntax; should be KEY=VALUE, was %s", l)
}
key := strings.ToLower(kv[0])
switch key {
default:
conf.ExtensionVars[key] = kv[1]
case "configdir":
log.Printf("configdir is deprecated. Ignored configdir=%s", kv[1])
case "logdir":
log.Printf("logdir is deprecated. Ignored logdir=%s", kv[1])
case "logfile":
log.Printf("logfile is deprecated. Ignored logfile=%s", kv[1])
case graphhorizontalscale:
iv, err := strconv.Atoi(kv[1])
if err != nil {
return err
}
conf.GraphHorizontalScale = iv
case helpvisible:
bv, err := strconv.ParseBool(kv[1])
if err != nil {
return fmt.Errorf("line %d: %v", lineNo, err)
}
conf.HelpVisible = bv
case colorscheme:
cs, err := colorschemes.FromName(conf.ConfigDir, kv[1])
if err != nil {
return fmt.Errorf("line %d: %v", lineNo, err)
}
conf.Colorscheme = cs
case updateinterval:
iv, err := strconv.Atoi(kv[1])
if err != nil {
return err
}
conf.UpdateInterval = time.Duration(iv)
case averagecpu:
bv, err := strconv.ParseBool(kv[1])
if err != nil {
return fmt.Errorf("line %d: %v", lineNo, err)
}
conf.AverageLoad = bv
case percpuload:
bv, err := strconv.ParseBool(kv[1])
if err != nil {
return fmt.Errorf("line %d: %v", lineNo, err)
}
conf.PercpuLoad = bv
case tempscale:
switch kv[1] {
case "C":
conf.TempScale = 'C'
case "F":
conf.TempScale = 'F'
default:
conf.TempScale = 'C'
return fmt.Errorf("invalid TempScale value %s", kv[1])
}
case statusbar:
bv, err := strconv.ParseBool(kv[1])
if err != nil {
return fmt.Errorf("line %d: %v", lineNo, err)
}
conf.Statusbar = bv
case netinterface:
conf.NetInterface = kv[1]
case layout:
conf.Layout = kv[1]
case maxlogsize:
iv, err := strconv.Atoi(kv[1])
if err != nil {
return err
}
conf.MaxLogSize = int64(iv)
case export:
conf.ExportPort = kv[1]
case mbps:
conf.Mbps = true
case temperatures:
conf.Temps = strings.Split(kv[1], ",")
}
}
return nil
}
// Write serializes the configuration to a file.
// The configuration written is based on the loaded configuration, plus any
// command-line changes, so it can be used to update an existing configuration
// file. The file will be written to the specificed `--config` argument file,
// if one is set; otherwise, it'll create one in the user's config directory.
func (conf *Config) Write() (string, error) {
var dir *configdir.Config
var file string = CONFFILE
if conf.ConfigFile == "" {
ds := conf.ConfigDir.QueryFolders(configdir.Global)
if len(ds) == 0 {
ds = conf.ConfigDir.QueryFolders(configdir.Local)
if len(ds) == 0 {
return "", fmt.Errorf("error locating config folders")
}
}
ds[0].CreateParentDir(CONFFILE)
dir = ds[0]
} else {
dir = &configdir.Config{}
dir.Path = filepath.Dir(conf.ConfigFile)
file = filepath.Base(conf.ConfigFile)
}
marshalled := marshal(conf)
err := dir.WriteFile(file, marshalled)
if err != nil {
return "", err
}
return filepath.Join(dir.Path, file), nil
}
func marshal(c *Config) []byte {
buff := bytes.NewBuffer(nil)
fmt.Fprintln(buff, "# Scale graphs to this level; 7 is the default, 2 is zoomed out.")
fmt.Fprintf(buff, "%s=%d\n", graphhorizontalscale, c.GraphHorizontalScale)
fmt.Fprintln(buff, "# If true, start the UI with the help visible")
fmt.Fprintf(buff, "%s=%t\n", helpvisible, c.HelpVisible)
fmt.Fprintln(buff, "# The color scheme to use. See `--list colorschemes`")
fmt.Fprintf(buff, "%s=%s\n", colorscheme, c.Colorscheme.Name)
fmt.Fprintln(buff, "# How frequently to update the UI, in nanoseconds")
fmt.Fprintf(buff, "%s=%d\n", updateinterval, c.UpdateInterval)
fmt.Fprintln(buff, "# If true, show the average CPU load")
fmt.Fprintf(buff, "%s=%t\n", averagecpu, c.AverageLoad)
fmt.Fprintln(buff, "# If true, show load per CPU")
fmt.Fprintf(buf
|