diff options
author | lawl <github@dumbinter.net> | 2020-07-05 00:38:30 +0200 |
---|---|---|
committer | lawl <github@dumbinter.net> | 2020-07-05 00:38:30 +0200 |
commit | e758ce0ac70f6e7ecdfee64f3c3c7090713e83f6 (patch) | |
tree | 996bc0d52afdfb290e0fa69ebc5adbf6f57fb0ec /config.go | |
parent | d29abc7a0d4cca46d3d6e217f763d4e521b03244 (diff) | |
download | noisetorch-e758ce0ac70f6e7ecdfee64f3c3c7090713e83f6.tar.gz noisetorch-e758ce0ac70f6e7ecdfee64f3c3c7090713e83f6.zip |
big bang
Diffstat (limited to 'config.go')
-rw-r--r-- | config.go | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/config.go b/config.go new file mode 100644 index 0000000..278e62b --- /dev/null +++ b/config.go @@ -0,0 +1,74 @@ +package main + +import ( + "bytes" + "io/ioutil" + "log" + "os" + "path/filepath" + + "github.com/BurntSushi/toml" +) + +type config struct { + Threshold int + DisplayMonitorSources bool +} + +const configDir = ".config/noiseui/" +const configFile = "config.toml" + +func initializeConfigIfNot() { + log.Println("Checking if config needs to be initialized") + conf := config{Threshold: 95, DisplayMonitorSources: false} + configdir := filepath.Join(os.Getenv("HOME"), configDir) + ok, err := exists(configdir) + if err != nil { + log.Fatalf("Couldn't check if config directory exists: %v\n", err) + } + if !ok { + err = os.MkdirAll(configdir, 0700) + if err != nil { + log.Fatalf("Couldn't create config directory: %v\n", err) + } + } + tomlfile := filepath.Join(configdir, configFile) + ok, err = exists(tomlfile) + if err != nil { + log.Fatalf("Couldn't check if config file exists: %v\n", err) + } + if !ok { + log.Println("Initializing config") + writeConfig(&conf) + } +} + +func readConfig() *config { + f := filepath.Join(os.Getenv("HOME"), configDir, configFile) + config := config{} + if _, err := toml.DecodeFile(f, &config); err != nil { + log.Fatalf("Couldn't read config file: %v\n", err) + } + + return &config +} + +func writeConfig(conf *config) { + f := filepath.Join(os.Getenv("HOME"), configDir, configFile) + var buffer bytes.Buffer + if err := toml.NewEncoder(&buffer).Encode(&conf); err != nil { + log.Fatalf("Couldn't write config file: %v\n", err) + } + ioutil.WriteFile(f, []byte(buffer.String()), 0644) +} + +func exists(path string) (bool, error) { + _, err := os.Stat(path) + if err == nil { + return true, nil + } + if os.IsNotExist(err) { + return false, nil + } + return false, err +} |