/
config.go
59 lines (50 loc) · 1.49 KB
/
config.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
package v0
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
config "github.com/threeport/threeport/pkg/config/v0"
)
// InitConfig sets up the threeport config for a CLI.
func InitConfig(cfgFile string) {
// determine user home dir
home, err := homedir.Dir()
if err != nil {
Error("failed to determine user home directory", err)
os.Exit(1)
}
// set default threeport config path if not set by user
if cfgFile == "" {
viper.AddConfigPath(config.DefaultThreeportConfigPath(home))
viper.SetConfigName(config.ThreeportConfigName)
viper.SetConfigType(config.ThreeportConfigType)
cfgFile = filepath.Join(
config.DefaultThreeportConfigPath(home),
fmt.Sprintf("%s.%s", config.ThreeportConfigName, config.ThreeportConfigType),
)
}
// create file if it doesn't exit
if _, err := os.Stat(cfgFile); errors.Is(err, os.ErrNotExist) {
if err := os.MkdirAll(config.DefaultThreeportConfigPath(home), os.ModePerm); err != nil {
Error("failed to create config directory", err)
os.Exit(1)
}
if err := viper.WriteConfigAs(cfgFile); err != nil {
Error("failed to write config to disk", err)
os.Exit(1)
}
}
viper.SetConfigFile(cfgFile)
// ensure config permissions are read/write for user only
if err := os.Chmod(cfgFile, 0600); err != nil {
Error("failed to set permissions to read/write only", err)
os.Exit(1)
}
if err := viper.ReadInConfig(); err != nil {
Error("failed to read config", err)
os.Exit(1)
}
}