generated from Serpentiel/go-cli
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viper.go
62 lines (47 loc) · 1.33 KB
/
viper.go
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
// Package provide is the package that contains all of the providers for the dependency injection container.
package provide
import (
"os"
"strings"
"github.com/Serpentiel/arikawa-boilerplate/internal/pkg/asset"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/fx"
)
// Viper is a function which provides a viper.Viper instance.
func Viper(lc fx.Lifecycle, cmd *cobra.Command) (*viper.Viper, error) {
const (
// configFileName is the name of the config file.
configFileName string = ".arikawa-boilerplate"
// configFileMode is the mode that the config file will be created with.
configFileMode os.FileMode = 0660
)
v := viper.New()
configFile, err := cmd.PersistentFlags().GetString("config")
if err != nil {
return nil, err
}
if configFile == "" {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, err
}
v.AddConfigPath(homeDir)
v.SetConfigName(configFileName)
v.SetConfigType("yaml")
configFile = strings.Join([]string{homeDir, configFileName + ".yaml"}, string(os.PathSeparator))
} else {
v.SetConfigFile(configFile)
}
v.AutomaticEnv()
if err := v.ReadInConfig(); err != nil {
err = os.WriteFile(configFile, asset.ExampleConfig, configFileMode)
if err != nil {
return nil, err
}
if err = v.ReadInConfig(); err != nil {
return nil, err
}
}
return v, nil
}