Created a json config file

This should satisfy issue #111.

The config.json file allows the user to define customizations without
the need to use shell arguments.
This commit is contained in:
Jaron Swab 2019-10-02 22:09:13 -04:00
parent 2cd92e147e
commit af4ef606d9
2 changed files with 70 additions and 0 deletions

11
config.json Normal file
View File

@ -0,0 +1,11 @@
{
"colorscheme": "monokai",
"updateInterval": 1,
"minimalMode": false,
"averageLoad": false,
"percpuLoad": false,
"isFahrenheit": false,
"battery": false,
"statusbar": false,
"netInterface": "NET_INTERFACE_ALL"
}

59
main.go
View File

@ -61,6 +61,20 @@ var (
bar *w.StatusBar
)
// ConfigFile holds data from the config.json file.
// Allowing for customization without the need for termianl arguments.
type ConfigFile struct {
Colorscheme string `json:"colorscheme"`
UpdateInterval int64 `json:"updateInterval"`
MinimalMode bool `json:"minimalMode"`
AverageLoad bool `json:"averageLoad"`
PercpuLoad bool `json:"percpuLoad"`
isFahrenheit bool `json:"isFahrenheit"`
Battery bool `json:"battery"`
Statusbar bool `json:"statusbar"`
NetInterface string `json:"netInterface"`
}
func parseArgs() error {
usage := `
Usage: gotop [options]
@ -123,6 +137,47 @@ Colorschemes:
return nil
}
// parseConfig convert the config.json and sets the appropriate variables
// for customization in place of using the terminal arguments.
func parseConfig() error {
jsonBytes, err := ioutil.ReadFile("config.json")
if err != nil {
log.Fatal(err)
}
var config ConfigFile
err = json.Unmarshal(jsonBytes, &config)
if err != nil {
stderrLogger.Fatalf("failed to unmarshel json: %v", err)
}
if err := handleColorscheme(config.Colorscheme); err != nil {
return err
}
averageLoad = config.AverageLoad
percpuLoad = config.PercpuLoad
battery = config.Battery
minimalMode = config.MinimalMode
statusbar = config.Statusbar
netInterface = config.NetInterface
rateStr := config.UpdateInterval
rate := float64(rateStr)
if rate < 1 {
updateInterval = time.Second * time.Duration(1/rate)
} else {
updateInterval = time.Second / time.Duration(rate)
}
if config.isFahrenheit {
tempScale = w.Fahrenheit
}
return nil
}
func handleColorscheme(cs string) error {
switch cs {
case "default":
@ -426,6 +481,10 @@ func main() {
stderrLogger.Fatalf("failed to parse cli args: %v", err)
}
if err := parseConfig(); err != nil {
stderrLogger.Fatalf("failed to parse the config file: %v", err)
}
logfile, err := setupLogfile()
if err != nil {
stderrLogger.Fatalf("failed to setup log file: %v", err)