-
Notifications
You must be signed in to change notification settings - Fork 0
/
credentials.go
88 lines (77 loc) · 1.8 KB
/
credentials.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"log"
"path/filepath"
"regexp"
"strings"
)
type Profiles map[string]Profile
type Profile map[string]string
func (p *Profiles) Marshal() []byte {
var lines []string
for name, profile := range *p {
line := fmt.Sprintf("[%s]", name)
lines = append(lines, line)
for key, value := range profile {
line = fmt.Sprintf("%s = %s", key, value)
lines = append(lines, line)
}
lines = append(lines, "") //separate profiles with blank line
}
contents := []byte(strings.Join(lines, "\n"))
return contents
}
func Unmarshal(data []byte) Profiles {
reader := bufio.NewReader(bytes.NewReader(data))
profiles := Profiles{}
var profileName string
for {
line, err := reader.ReadString('\n')
if err == io.EOF {
break
}
if IsBlank(line) {
continue
}
if IsProfileName(line) {
profileName = ParseProfileName(line)
profiles[profileName] = Profile{}
continue
}
key, value := ParseKeyValue(line)
profiles[profileName][key] = value
}
return profiles
}
func IsBlank(line string) bool {
match, _ := regexp.MatchString(`^\s*$`, line)
return match
}
func IsProfileName(line string) bool {
match, _ := regexp.MatchString(`^\[[ A-Za-z0-9\-_]+]`, line)
return match
}
func ParseProfileName(line string) string {
r := regexp.MustCompile(`\[([^]]*)]`)
parts := r.FindStringSubmatch(line)
if len(parts) < 2 {
log.Panic("could not parse credentials file")
}
return parts[1]
}
func ParseKeyValue(line string) (key string, value string) {
r := regexp.MustCompile(`([^= ]*)\s*=\s*(\S*)`)
parts := r.FindStringSubmatch(line)
if len(parts) < 3 {
log.Println(line)
log.Panic("could not parse credentials file")
}
return parts[1], parts[2]
}
func CredentialsFilepath() string {
return filepath.Join(HomeDirectory(), ".aws", "credentials")
}