-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaws_profile.go
121 lines (112 loc) · 2.67 KB
/
aws_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
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
package main
import (
"encoding/json"
"fmt"
"github.com/go-ini/ini"
"io/ioutil"
"log"
"os"
"path"
)
var (
awsProfileNamesPath = path.Join(resourcePath, "aws_profile_names.json")
awsCredentialFilePath = path.Join(os.Getenv("HOME"), ".aws", "credentials")
)
func existingProfiles() bool {
if _, err := os.Stat(awsProfileNamesPath); os.IsNotExist(err) {
return false
}
return true
}
func awsProfiles() []string {
if existingProfiles() {
profiles, err := readAWSProfileFile()
if err != nil {
fmt.Printf("Error: %s", err)
}
return profiles
}
userQuestion := fmt.Sprint("Use AWS profiles? [y/n]:")
if confirmUserSelection(userQuestion) {
setupProfiles()
profiles, err := readAWSProfileFile()
if err != nil {
fmt.Printf("Error: %s", err)
}
return profiles
}
return []string{"default"}
}
func writeAWSProfileFile(profileNames []string) {
profileNamesJSON, err := json.Marshal(profileNames)
if err != nil {
fmt.Println(err)
return
}
writeError := ioutil.WriteFile(awsProfileNamesPath, profileNamesJSON, 0755)
if writeError != nil {
fmt.Print("Could not write AWS profile names to config file\n")
log.Fatal(writeError)
}
}
func readAWSProfileFile() ([]string, error) {
file, e := ioutil.ReadFile(awsProfileNamesPath)
if e != nil {
if noSuchFileErrRegexp.MatchString(e.Error()) {
return []string{}, e
}
fmt.Printf("Could not: %v\n", e.Error())
os.Exit(1)
}
var awsProfiles []string
err := json.Unmarshal(file, &awsProfiles)
if err != nil {
log.Fatal("could not read AWS profile file")
}
return awsProfiles, nil
}
func detail4Capture(attr string) string {
var response string
fmt.Printf("%s ", attr)
_, err := fmt.Scanln(&response)
if err != nil {
if err.Error() == "unexpected newline" {
return ""
}
log.Fatal(err)
}
return response
}
func confirmUserSelection(userPrompt string) bool {
var returnVar bool
confirmation := detail4Capture(userPrompt)
switch confirmation {
case "y":
returnVar = true
case "n":
returnVar = false
default:
confirmUserSelection(userPrompt)
}
return returnVar
}
func setupProfiles() {
fmt.Println("Discovering AWS profile names from credentials file")
cfg, err := ini.Load(awsCredentialFilePath)
if err != nil {
fmt.Printf("error reading AWS credential file: %s", err)
}
sections := cfg.SectionStrings()
fmt.Println("Please select profiles AWS profiles to include")
var addedProfiles []string
for _, section := range sections {
if section == ini.DEFAULT_SECTION {
continue
}
userQuestion := fmt.Sprintf("Include profile [%s]? [y/n]:", section)
if confirmUserSelection(userQuestion) {
addedProfiles = append(addedProfiles, section)
}
}
writeAWSProfileFile(addedProfiles)
}