-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
46 lines (38 loc) · 968 Bytes
/
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
package main
import (
"fmt"
"log"
"os/user"
"path"
"github.com/spf13/viper"
)
type Config struct {
Profile string `viper:"profile"`
AwsConfigPath string `viper:awsConfigPath`
}
const (
pathConfig = ".aws-env"
)
func getHomeDir() string {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
return usr.HomeDir
}
func getConfig() *Config {
viper.AddConfigPath("$HOME/" + pathConfig)
viper.SetConfigName("config")
viper.SetDefault("profile", "default")
viper.SetDefault("AwsConfigPath", path.Join(getHomeDir(), ".aws/credentials"))
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file must exist in ~/"+pathConfig+"./config.yaml: %s \n", err))
}
config := &Config{}
err = viper.Unmarshal(config)
if err != nil {
panic(fmt.Errorf("Fatal error unmarhsal config struct : %s \n", err))
}
return config
}