summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2020-04-25 17:02:47 +0530
committerAndinus <andinus@nand.sh>2020-04-25 17:02:47 +0530
commite9e268d945f539bd229e46648e8e8fb83955ab83 (patch)
tree453b5553bd4d8cced58075e7dec560b4a78a5b0f
parent46a9d786dcf736894a13ef2ca19714fe4c378a1a (diff)
downloadpavo-e9e268d945f539bd229e46648e8e8fb83955ab83.tar.gz
Add configDir & configFile function
-rw-r--r--config.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/config.go b/config.go
new file mode 100644
index 0000000..1045f3d
--- /dev/null
+++ b/config.go
@@ -0,0 +1,43 @@
+package main
+
+import (
+	"fmt"
+	"os"
+)
+
+// configFile returns the config file for pavo. It is
+// configDir/pavo.json by default but can be changed by setting
+// PAVO_CONFIG_FILE environment variable. If you set this then no need
+// to set PAVO_CONFIG_DIR.
+func configFile() (configFile string) {
+	configFile = os.Getenv("PAVO_CONFIG_FILE")
+
+	if len(configFile) == 0 {
+		configFile = fmt.Sprintf("%s/%s",
+			configDir(),
+			"pavo.json")
+	}
+
+	return
+}
+
+// configDir returns the config directory for pavo. First
+// PAVO_CONFIG_DIR environment variable is checked, if it doesn't
+// exist then XDG_CONFIG_HOME & if that is not set either then we
+// assume it to be the default value which is $HOME/.config according
+// to XDG Base Directory Specification.
+func configDir() (configDir string) {
+	configDir = os.Getenv("PAVO_CONFIG_DIR")
+
+	if len(configDir) == 0 {
+		configDir = os.Getenv("XDG_CONFIG_HOME")
+	}
+
+	if len(configDir) == 0 {
+		configDir = fmt.Sprintf("%s/%s",
+			os.Getenv("HOME"),
+			".config")
+	}
+
+	return
+}