-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.go
53 lines (46 loc) · 1.01 KB
/
profile.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
package main
import (
"bufio"
"os"
"path/filepath"
"strings"
)
const default_cred = "~/.aws/credentials"
func correctHomeDir(path string) (res string) {
if strings.HasPrefix(path, "~") {
home := os.Getenv("HOME")
dir := filepath.Dir(path)
dir = strings.Replace(dir, "~", home, 1)
bname := filepath.Base(path)
res = filepath.Join(dir, bname)
} else {
res = path
}
return
}
func load_file(file_name string) (ret []string, err error) {
file_name = correctHomeDir(file_name)
fp, err := os.Open(file_name)
if err != nil {
if file_name == correctHomeDir(default_cred) {
return []string{""}, nil
}
return nil, err
}
defer fp.Close()
sc := bufio.NewScanner(fp)
d := make(map[string]bool)
for sc.Scan() {
line := sc.Text()
if strings.HasPrefix(line, "[") {
line = strings.Replace(line, "[profile ", "", 1)
line = strings.Replace(line, "[", "", 1)
line = strings.Replace(line, "]", "", 1)
if !d[line] {
d[line] = true
ret = append(ret, line)
}
}
}
return ret, nil
}