summaryrefslogtreecommitdiff
path: root/internal/config/config.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/config/config.go')
-rw-r--r--internal/config/config.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/internal/config/config.go b/internal/config/config.go
new file mode 100644
index 0000000..ad35ee7
--- /dev/null
+++ b/internal/config/config.go
@@ -0,0 +1,56 @@
+package config
+
+import (
+ "errors"
+ "fmt"
+ "os"
+ "path/filepath"
+
+ "gopkg.in/yaml.v3"
+)
+
+type Config struct {
+ Filters []string `yaml:"filters"`
+ Iterations int `yaml:"iterations"`
+}
+
+func getDefaultConfigPath() (defaultPath string, err error) {
+ programName := "uptfs"
+ configFileName := "config.yaml"
+
+ if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" {
+ return filepath.Join(xdg, programName, configFileName), nil
+ }
+
+ if home := os.Getenv("HOME"); home != "" {
+ return filepath.Join(home, programName, configFileName), nil
+ }
+
+ return "", errors.New("both XDG_CONFIG_HOME and HOME are not set, can't proceed")
+}
+
+func (c *Config) LoadConfig(filepath string) *Config {
+ if filepath == "" {
+ var err error
+ filepath, err = getDefaultConfigPath()
+
+ if err != nil {
+ fmt.Printf("%v\n", err)
+ os.Exit(1)
+ }
+ }
+
+ yamlFile, err := os.ReadFile(filepath)
+
+ if err != nil {
+ fmt.Printf("%v\n", err)
+ os.Exit(1)
+ }
+ err = yaml.Unmarshal(yamlFile, c)
+ if err != nil {
+ fmt.Printf("%v\n", err)
+ os.Exit(1)
+ }
+
+ return c
+}