forked from k1LoW/awsdo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ini.go
64 lines (54 loc) · 1.59 KB
/
ini.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
63
64
package ini
import (
"fmt"
"os"
"path/filepath"
"gopkg.in/ini.v1"
)
type Ini struct {
configIni *ini.File
credsIni *ini.File
}
func (i *Ini) GetKey(profile, key string) string {
switch {
case i.credsIni.Section(profile).Key(key).String() != "":
return i.credsIni.Section(profile).Key(key).String()
case i.credsIni.Section(fmt.Sprintf("profile %s", profile)).Key(key).String() != "":
return i.credsIni.Section(fmt.Sprintf("profile %s", profile)).Key(key).String()
case i.configIni.Section(profile).Key(key).String() != "":
return i.configIni.Section(profile).Key(key).String()
case i.configIni.Section(fmt.Sprintf("profile %s", profile)).Key(key).String() != "":
return i.configIni.Section(fmt.Sprintf("profile %s", profile)).Key(key).String()
case i.credsIni.Section("default").Key(key).String() != "":
return i.credsIni.Section("default").Key(key).String()
case i.configIni.Section("default").Key(key).String() != "":
return i.configIni.Section("default").Key(key).String()
}
return ""
}
func New() (*Ini, error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}
configIniPath := os.Getenv("AWS_CONFIG_FILE")
if configIniPath == "" {
configIniPath = filepath.Join(home, ".aws", "config")
}
credsIniPath := os.Getenv("AWS_SHARED_CREDENTIALS_FILE")
if credsIniPath == "" {
credsIniPath = filepath.Join(home, ".aws", "credentials")
}
configIni, err := ini.Load(configIniPath)
if err != nil {
return nil, err
}
credsIni, err := ini.Load(credsIniPath)
if err != nil {
return nil, err
}
return &Ini{
configIni: configIni,
credsIni: credsIni,
}, nil
}