-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
ListTables.go
71 lines (59 loc) · 1.79 KB
/
ListTables.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[dynamodb.go.list_all_tables]
package main
// snippet-start:[dynamodb.go.list_all_tables.imports]
import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
// snippet-end:[dynamodb.go.list_all_tables.imports]
// GetTables retrieves a list of your Amazon DynamoDB tables
// Inputs:
//
// sess is the current session, which provides configuration for the SDK's service clients
// limit is the maximum number of tables to return
//
// Output:
//
// If success, a list of the tables and nil
// Otherwise, nil and an error from the call to ListTables
func GetTables(sess *session.Session, limit *int64) ([]*string, error) {
// snippet-start:[dynamodb.go.list_all_tables.call]
svc := dynamodb.New(sess)
result, err := svc.ListTables(&dynamodb.ListTablesInput{
Limit: limit,
})
// snippet-end:[dynamodb.go.list_all_tables.call]
if err != nil {
return nil, err
}
return result.TableNames, nil
}
func main() {
// snippet-start:[dynamodb.go.list_all_tables.args]
limit := flag.Int64("l", 100, "How many tables to return")
flag.Parse()
if *limit < int64(0) {
*limit = int64(10)
}
// snippet-end:[dynamodb.go.list_all_tables.args]
// snippet-start:[dynamodb.go.list_all_tables.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// snippet-end:[dynamodb.go.list_all_tables.session]
tables, err := GetTables(sess, limit)
if err != nil {
fmt.Println("Got an error retrieving table names:")
fmt.Println(err)
return
}
// Get up to limit tables
for _, n := range tables {
fmt.Println(*n)
}
}
// snippet-end:[dynamodb.go.list_all_tables]