xmtop/config.go

123 lines
2.7 KiB
Go
Raw Normal View History

2020-02-14 00:15:52 +08:00
package gotop
import (
"bufio"
"fmt"
"io"
"strconv"
"strings"
"time"
2020-02-29 04:48:35 +08:00
"github.com/xxxserxxx/gotop/v3/colorschemes"
"github.com/xxxserxxx/gotop/v3/widgets"
)
2020-02-21 08:56:52 +08:00
// TODO: test, build, release [#119] [#120] [#121]
type Config struct {
ConfigDir string
LogDir string
2020-02-16 04:27:31 +08:00
LogFile string
GraphHorizontalScale int
HelpVisible bool
Colorscheme colorschemes.Colorscheme
UpdateInterval time.Duration
AverageLoad bool
PercpuLoad bool
Statusbar bool
TempScale widgets.TempScale
NetInterface string
Layout string
2020-02-16 04:27:31 +08:00
MaxLogSize int64
ExportPort string
2020-02-29 00:03:41 +08:00
Extensions []string
}
func Parse(in io.Reader, conf *Config) error {
r := bufio.NewScanner(in)
var lineNo int
for r.Scan() {
l := strings.TrimSpace(r.Text())
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:
return fmt.Errorf("invalid config option %s", key)
case "configdir":
conf.ConfigDir = kv[1]
case "logdir":
conf.LogDir = kv[1]
case "logfile":
conf.LogFile = 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":
iv, err := strconv.Atoi(kv[1])
if err != nil {
return err
}
conf.TempScale = widgets.TempScale(iv)
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]
2020-02-29 00:03:41 +08:00
case "extensions":
conf.Extensions = strings.Split(kv[1], ",")
}
}
return nil
}