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
|
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
}
|