aboutsummaryrefslogtreecommitdiff
path: root/config.go
blob: 86b9e181572fb53b358073ec1c093f12e7ca4099 (plain)
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
package main

import (
	"bytes"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"

	"github.com/BurntSushi/toml"
)

type config struct {
	Threshold             int
	DisplayMonitorSources bool
	EnableUpdates         bool
	GuiltTripped          bool
	FilterInput           bool
	FilterOutput          bool
	LastUsedInput         string
	LastUsedOutput        string
}

const configFile = "config.toml"

func initializeConfigIfNot() {
	log.Println("Checking if config needs to be initialized")

	// if you're a package maintainer and you mess with this, we have a problem.
	// Unless you set -tags release on the build the updater is *not* compiled in anymore. DO NOT MESS WITH THIS!
	// This isn't and never was the proper location to disable the updater.
	conf := config{
		Threshold:             95,
		DisplayMonitorSources: false,
		EnableUpdates:         true,
		GuiltTripped:          false,
		FilterInput:           true,
		FilterOutput:          false,
		LastUsedInput:         "",
		LastUsedOutput:        ""}

	configdir := 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(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(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 configDir() string {
	return filepath.Join(xdgOrFallback("XDG_CONFIG_HOME", filepath.Join(os.Getenv("HOME"), ".config")), "noisetorch")
}

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
}

func xdgOrFallback(xdg string, fallback string) string {
	dir := os.Getenv(xdg)
	if dir != "" {
		if ok, err := exists(dir); ok && err == nil {
			log.Printf("Resolved $%s to '%s'\n", xdg, dir)
			return dir
		}

	}

	log.Printf("Couldn't resolve $%s falling back to '%s'\n", xdg, fallback)
	return fallback
}