-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathawsprofile.go
125 lines (98 loc) · 2.16 KB
/
awsprofile.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package awsprofile
// Null values
const (
EmptyString string = ""
ZeroInt int = 0
)
// Error messages
const (
ErrorNotFound string = " is not found"
)
// AwsProfile provide Credentials and Configs
type AwsProfile struct {
Credentials *Credentials
Configs *Configs
}
// New create a AwsProfile instance
func New() *AwsProfile {
awsProfile := &AwsProfile{
Credentials: NewCredentials(),
Configs: NewConfigs(),
}
return awsProfile
}
// Parse credential file and config file
func (a *AwsProfile) Parse() error {
credentialsFile, err := GetCredentialsPath()
if err != nil {
return err
}
if err = a.Credentials.Parse(credentialsFile); err != nil {
return err
}
configsFile, err := GetConfigsPath()
if err != nil {
return err
}
if err = a.Configs.Parse(configsFile); err != nil {
return err
}
return nil
}
// ProfileNames get name of profiles
func (a *AwsProfile) ProfileNames() ([]string, error) {
var profileNames []string
for _, credential := range *a.Credentials {
profileNames = append(profileNames, credential.ProfileName)
}
for _, config := range *a.Configs {
profileNames = append(profileNames, config.ProfileName)
}
profileNames = removeDuplicate(profileNames)
return profileNames, nil
}
// GetCredentials get Credentials
func (a *AwsProfile) GetCredentials() *Credentials {
return a.Credentials
}
// GetConfigs get Configs
func (a *AwsProfile) GetConfigs() *Configs {
return a.Configs
}
// IsCredential
func (a *AwsProfile) IsCredential(profile string) (bool, *Credential) {
var ok bool = false
var cred *Credential
for _, credential := range *a.Credentials {
if credential.ProfileName == profile {
ok = true
cred = &credential
break
}
}
return ok, cred
}
// IsConfig
func (a *AwsProfile) IsConfig(profile string) (bool, *Config) {
var ok bool = false
var conf *Config
for _, config := range *a.Configs {
if config.ProfileName == profile {
ok = true
conf = &config
break
}
}
return ok, conf
}
func removeDuplicate(s []string) []string {
var list []string
m := make(map[string]bool)
for _, v := range s {
if !m[v] {
m[v] = true
list = append(list, v)
}
}
return list
}