forked from 99designs/aws-vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
66 lines (55 loc) · 1.18 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
60
61
62
63
64
65
66
package main
import (
"fmt"
"log"
"os"
"os/user"
"strings"
"github.com/vaughan0/go-ini"
)
type profiles map[string]map[string]string
func configFile() (file string, err error) {
file = os.Getenv("AWS_CONFIG_FILE")
if file == "" {
usr, err := user.Current()
if err != nil {
return file, err
}
file = usr.HomeDir + "/.aws/config"
}
return file, err
}
func parseProfiles() (profiles, error) {
file, err := configFile()
if err != nil {
return nil, err
}
log.Printf("Parsing config file %s", file)
f, err := ini.LoadFile(file)
if err != nil {
return nil, err
}
profiles := profiles{}
for sectionName, section := range f {
profiles[strings.TrimPrefix(sectionName, "profile ")] = section
}
return profiles, nil
}
func (p profiles) sourceProfile(profile string) string {
if conf, ok := p[profile]; ok {
if source := conf["source_profile"]; source != "" {
return source
}
}
return profile
}
func writeProfiles(dest *os.File, profiles profiles) error {
for profile, vals := range profiles {
fmt.Fprintf(dest, "[profile %s]\n", profile)
for k, v := range vals {
fmt.Fprintf(dest, "%s = %s\n", k, v)
}
fmt.Fprintln(dest, "")
}
return dest.Sync()
}