-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupdateIni.go
42 lines (37 loc) · 1.14 KB
/
updateIni.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
package main
import (
"fmt"
"gopkg.in/ini.v1"
"os/user"
)
func updateConfig(credFile string, profile string, accessKey string, secretKey string, sessionToken string) {
if credFile == "" {
usr, err := user.Current()
if err != nil {
fmt.Println("Error getting current user: ", err)
return
}
credFile = usr.HomeDir + "/.aws/credentials"
}
cfg, err := ini.Load(credFile)
if err != nil {
fmt.Println("Error loading credentials file: ", err)
return
}
if cfg.HasSection(profile) {
cfg.Section(profile).Key("aws_access_key_id").SetValue(accessKey)
cfg.Section(profile).Key("aws_secret_access_key").SetValue(secretKey)
cfg.Section(profile).Key("aws_session_token").SetValue(sessionToken)
} else {
cfg.NewSection(profile)
cfg.Section(profile).Key("aws_access_key_id").SetValue(accessKey)
cfg.Section(profile).Key("aws_secret_access_key").SetValue(secretKey)
cfg.Section(profile).Key("aws_session_token").SetValue(sessionToken)
}
err = cfg.SaveTo(credFile)
if err != nil {
fmt.Println("Error saving to credentials file: ", err)
return
}
fmt.Println("Updated credentials file with new session token. Profile: ", profile)
}