-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (54 loc) · 1.89 KB
/
main.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
package main
import (
"bufio"
"flag"
"io/ioutil"
"log"
"os"
"path"
)
var defaultCredentialsFilePath = path.Join(getHomeDir(), ".aws", "credentials")
func main() {
profile := flag.String("profile", "", "Name of the profile to store in ~/.aws/credentials")
credentialsFilePath := flag.String("credentials-file", "", "Path to the credentials file")
flag.Parse()
if *credentialsFilePath == "" {
credentialsFilePath = &defaultCredentialsFilePath
}
if *profile == "" {
log.Fatal("--profile is required. See --help")
}
log.Printf("Creating profile '%s'", *profile)
// check if there is somethinig to read on STDIN
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
creds := getCredentialsFromStdIn()
// create file if it does not exist
if _, err := os.Stat(*credentialsFilePath); os.IsNotExist(err) {
log.Printf("Creating credentials file '%s'", *credentialsFilePath)
err := os.MkdirAll(path.Dir(*credentialsFilePath), 0700)
handleErr(err)
err = ioutil.WriteFile(*credentialsFilePath, []byte{}, 0600)
handleErr(err)
}
fileContent, err := readLines(*credentialsFilePath)
handleErr(err)
if fileContainsProfile(*profile, fileContent) {
log.Printf("Profile '%s' already exists. Removing it...", *profile)
newCredentialsPayloud := removeProfile(*profile, fileContent)
writeLines(newCredentialsPayloud, *credentialsFilePath)
}
f, err := os.OpenFile(*credentialsFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
handleErr(err)
defer f.Close()
bufio.NewWriter(f)
appendCredentials(creds, *profile, f)
completedFileContents, err := readLines(*credentialsFilePath)
handleErr(err)
formattedLines := formatCredentials(completedFileContents)
writeLines(formattedLines, *credentialsFilePath)
log.Printf("Credentials stored in %s. Verify this via 'cat %s'", *credentialsFilePath, *credentialsFilePath)
} else {
log.Fatal("No input on STDIN")
}
}