forked from 99designs/aws-vault
/
config.go
67 lines (55 loc) · 1.25 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
67
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/mitchellh/go-homedir"
"github.com/vaughan0/go-ini"
)
type profiles map[string]map[string]string
type config interface {
Parse() (profiles, error)
}
type fileConfig struct {
file string
}
func newConfigFromEnv() (config, error) {
file := os.Getenv("AWS_CONFIG_FILE")
if file == "" {
home, err := homedir.Dir()
if err != nil {
return nil, err
}
file = filepath.Join(home, "/.aws/config")
if _, err := os.Stat(file); os.IsNotExist(err) {
file = ""
}
}
return &fileConfig{file: file}, nil
}
func (c *fileConfig) Parse() (profiles, error) {
if c.file == "" {
return nil, nil
}
log.Printf("Parsing config file %s", c.file)
f, err := ini.LoadFile(c.file)
if err != nil {
return nil, fmt.Errorf("Error parsing config file %q: %v", c.file, err)
}
profiles := profiles{}
for sectionName, section := range f {
profiles[strings.TrimPrefix(sectionName, "profile ")] = section
}
return profiles, nil
}
// sourceProfile returns either the defined source_profile or p if none exists
func sourceProfile(p string, from profiles) string {
if conf, ok := from[p]; ok {
if source := conf["source_profile"]; source != "" {
return source
}
}
return p
}