Skip to content

Commit 88a1818

Browse files
author
Xu Ao
committed
Use credential or config in ~/.aws/ by default
1 parent e97b4a4 commit 88a1818

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

db/dbsession.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,28 @@ var DynamoDB *dynamodb.DynamoDB
1212

1313
// GetDynamoSession returns a new dynamodb session
1414
func GetDynamoSession(accessKeyID, secretAccessKey, region string) *dynamodb.DynamoDB {
15-
token := ""
16-
creds := credentials.NewStaticCredentials(accessKeyID, secretAccessKey, token)
17-
_, err := creds.Get()
18-
if err != nil {
19-
panic(err)
15+
session_config := session.Options{}
16+
if accessKeyID != "" || secretAccessKey != "" {
17+
token := ""
18+
creds := credentials.NewStaticCredentials(accessKeyID, secretAccessKey, token)
19+
_, err := creds.Get()
20+
if err != nil {
21+
panic(err)
22+
}
23+
session_config.Config.Credentials = creds
2024
}
2125

22-
DynamoDB = dynamodb.New(session.New(&aws.Config{Region: &region}))
26+
if region != "" {
27+
session_config.Config.Region = &region
28+
} else {
29+
session_config.SharedConfigState = session.SharedConfigEnable
30+
}
31+
32+
session, err := session.NewSessionWithOptions(session_config)
33+
if err != nil{
34+
panic(err)
35+
}
36+
DynamoDB = dynamodb.New(session)
2337
return DynamoDB
2438
}
2539

main.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,8 @@ func main() {
213213
},
214214
},
215215
Action: func(c *cli.Context) error {
216-
if region == "" {
217-
fmt.Println("Must provide aws config region")
218-
} else if accessKeyID == "" {
219-
fmt.Println("Must provide aws config access key id")
220-
} else if secretAccessKey == "" {
221-
fmt.Println("Must provide aws config secret access key")
216+
if !((accessKeyID == "" && secretAccessKey == "") || (accessKeyID != "" && secretAccessKey != "")){
217+
fmt.Println("Must provide access key id and secret access key at the same time")
222218
} else {
223219
db.GetDynamoSession(accessKeyID, secretAccessKey, region)
224220
runPrompt(tablePrefix)

0 commit comments

Comments
 (0)