Repository navigation
Expand file tree
/
Copy pathinit.go
More file actions
85 lines (66 loc) · 1.58 KB
/
Copy pathinit.go
File metadata and controls
85 lines (66 loc) · 1.58 KB
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
package cli
import (
"errors"
"log/slog"
"os"
"strings"
"github.com/adrg/xdg"
"github.com/spf13/viper"
)
var (
cfgFile string
cfgName string
)
func initCobra() {
viper.SetEnvPrefix("minibridge")
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
dataFolder, err := xdg.DataFile("minibridge")
if err != nil {
slog.Error("failed to retrieve xdg data folder: %w", err)
os.Exit(1)
}
configFolder, err := xdg.ConfigFile("minibridge")
if err != nil {
slog.Error("failed to retrieve xdg data folder: %w", err)
os.Exit(1)
}
slog.Debug("Folders configured", "config", configFolder, "data", dataFolder)
if cfgFile == "" {
cfgFile = os.Getenv("MINIBRIDGE_CONFIG")
}
if cfgFile != "" {
if _, err := os.Stat(cfgFile); os.IsNotExist(err) {
slog.Error("Config file does not exist", err)
os.Exit(1)
}
viper.SetConfigType("yaml")
viper.SetConfigFile(cfgFile)
if err = viper.ReadInConfig(); err != nil {
slog.Error("Unable to read config",
"path", cfgFile,
err,
)
os.Exit(1)
}
slog.Debug("Using config file", "path", cfgFile)
return
}
viper.AddConfigPath(configFolder)
viper.AddConfigPath("/usr/local/etc/minibridge")
viper.AddConfigPath("/etc/minibridge")
if cfgName == "" {
cfgName = os.Getenv("MINIBRIDGE_CONFIG_NAME")
}
if cfgName == "" {
cfgName = "default"
}
viper.SetConfigName(cfgName)
if err = viper.ReadInConfig(); err != nil {
if !errors.As(err, &viper.ConfigFileNotFoundError{}) {
slog.Error("Unable to read config", err)
os.Exit(1)
}
}
slog.Debug("Using config name", "name", cfgName)
}