diff options
author | Jaron Swab <jrswab@gmail.com> | 2019-10-02 22:09:13 -0400 |
---|---|---|
committer | Jaron Swab <jrswab@gmail.com> | 2019-10-02 22:09:13 -0400 |
commit | af4ef606d9aa7d4bb1240d6457ebf8c6eb453c5c (patch) | |
tree | f72a4b15ce6b511bb74bf0f3a8e216f40ac5387b | |
parent | 2cd92e147e6b7f60d76693f93f3a52e66a2fa9d1 (diff) |
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.
-rw-r--r-- | config.json | 11 | ||||
-rw-r--r-- | main.go | 59 |
2 files changed, 70 insertions, 0 deletions
diff --git a/config.json b/config.json new file mode 100644 index 0000000..6a4a618 --- /dev/null +++ b/config.json @@ -0,0 +1,11 @@ +{ + "colorscheme": "monokai", + "updateInterval": 1, + "minimalMode": false, + "averageLoad": false, + "percpuLoad": false, + "isFahrenheit": false, + "battery": false, + "statusbar": false, + "netInterface": "NET_INTERFACE_ALL" +}
\ No newline at end of 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) |