-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (83 loc) · 1.65 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
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
package main
import (
"fmt"
"io"
"log"
"os"
"os/user"
)
const VERSION = "4.0.0"
func main() {
halt := DoOptions()
if halt {
return
}
log.SetFlags(log.Llongfile)
var alias string
if len(os.Args) < 2 {
profile := os.Getenv("AWS_PROFILE")
if profile != "" {
alias = profile
} else {
fmt.Println("missing argument: profile-alias")
return
}
} else {
alias = os.Args[1]
}
//open and parse credentials
file, err := ReadCredentialsFile()
if err != nil {
log.Fatalln(err)
}
credentials := Unmarshal(file)
profile, err := GetCredentials(alias)
if err == io.EOF {
fmt.Println("no active session. Please login using aws sso login --profile {{profile}}")
return
}
if err != nil {
log.Println(err)
return
}
//modify profile
credentials[alias] = profile
//re-write credentials file
contents := credentials.Marshal()
err = os.WriteFile(CredentialsFilepath(), contents, 0644)
if err != nil {
log.Fatal(err)
}
fmt.Println("updated:", alias)
}
func nonBlankLines(lines []string) []string {
var newLines []string
for _, line := range lines {
if !IsBlank(line) {
newLines = append(newLines, line)
}
}
return newLines
}
func ReadCredentialsFile() ([]byte, error) {
credentialsFile := CredentialsFilepath()
if !fileExists(credentialsFile) {
return nil, fmt.Errorf("file not found: %s", credentialsFile)
}
data, err := os.ReadFile(credentialsFile)
if err != nil {
return nil, err
}
return data, nil
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) || info.IsDir() {
return false
}
return true
}
func HomeDirectory() string {
u, _ := user.Current()
return u.HomeDir
}