Skip to content

Commit 8965233

Browse files
authored
Merge pull request #2 from AO-AO/master
Use credential or config in ~/.aws/ by default
2 parents c227f64 + 9141d96 commit 8965233

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

db/dbsession.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,30 @@ import (
1111
var DynamoDB *dynamodb.DynamoDB
1212

1313
// GetDynamoSession returns a new dynamodb session
14-
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)
14+
func GetDynamoSession(accessKeyID, secretAccessKey, region string) (*dynamodb.DynamoDB, error) {
15+
sessionConfig := session.Options{}
16+
if accessKeyID != "" || secretAccessKey != "" {
17+
token := ""
18+
creds := credentials.NewStaticCredentials(accessKeyID, secretAccessKey, token)
19+
_, err := creds.Get()
20+
if err != nil {
21+
return nil, err
22+
}
23+
sessionConfig.Config.Credentials = creds
2024
}
2125

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

2640
// ListTable returns all table names from dynamoDB

main.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
67
"os/signal"
@@ -214,17 +215,14 @@ func main() {
214215
},
215216
},
216217
Action: func(c *cli.Context) error {
217-
if region == "" {
218-
fmt.Println("Must provide aws config region")
219-
} else if accessKeyID == "" {
220-
fmt.Println("Must provide aws config access key id")
221-
} else if secretAccessKey == "" {
222-
fmt.Println("Must provide aws config secret access key")
223-
} else {
224-
db.GetDynamoSession(accessKeyID, secretAccessKey, region)
218+
if !((accessKeyID == "" && secretAccessKey == "") || (accessKeyID != "" && secretAccessKey != "")) {
219+
return errors.New("Must provide access key id and secret access key at the same time")
220+
} else if _, err := db.GetDynamoSession(accessKeyID, secretAccessKey, region); err == nil {
225221
runPrompt(tablePrefix)
222+
return nil
223+
} else {
224+
return err
226225
}
227-
return nil
228226
},
229227
}
230228

0 commit comments

Comments
 (0)