-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (127 loc) · 3.07 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"encoding/json"
"fmt"
"github.com/TylerBrock/colorjson"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/davecgh/go-spew/spew"
"github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
"github.com/spf13/pflag"
"io"
"os"
"sort"
"strings"
)
var maxCount int
var daxCluster string
var profile string
type Dynamo struct {
api *Api
w io.Writer
emitted int
}
func main() {
/*
len(os.Args)
1 => just app name, list tables
2 => app and table, do a scan
3 => app, table, pkey => do a query
4 => app, table, pkey, skey => implies equality if no operator in skey
*/
pflag.IntVarP(&maxCount, "number", "n", 10, "maximum number of items to output. 0 for no limit")
pflag.StringVar(&daxCluster, "dax", "", "Address of DAX cluster")
pflag.StringVar(&profile, "profile", "", "~/.aws/config profile to use")
pflag.Parse()
args := pflag.Args()
var w io.Writer = os.Stdout
if isatty.IsTerminal(os.Stdout.Fd()) {
w = colorable.NewColorable(os.Stdout)
}
d := &Dynamo{
api: apiClient(daxCluster, profile),
w: w,
}
d.Run(args)
}
func (d *Dynamo) Run(args []string) {
var err error
switch len(args) {
case 0:
err = d.tables()
case 1:
err = d.scan(args[0])
default:
err = d.query(args)
}
if err != nil {
spew.Dump(err)
os.Exit(1)
}
}
func (d *Dynamo) tables() error {
names := []string{}
err := d.api.ListTablesPages(&dynamodb.ListTablesInput{}, func(page *dynamodb.ListTablesOutput, lastPage bool) bool {
for _, name := range page.TableNames {
names = append(names, *name)
}
return !lastPage
})
sort.Slice(names, func(i, j int) bool {
return strings.ToLower(names[i]) < strings.ToLower(names[j])
})
fmt.Println(strings.Join(names, "\n"))
return err
}
func (d *Dynamo) query(args []string) error {
input, err := queryForArgs(d.api, args)
if err != nil {
return err
}
return d.api.QueryPages(input, func(page *dynamodb.QueryOutput, lastPage bool) bool {
return d.write(convert(page.Items)) || lastPage
})
}
func (d *Dynamo) scan(table string) error {
input := &dynamodb.ScanInput{
TableName: aws.String(table),
Limit: aws.Int64(100),
}
return d.api.ScanPages(input, func(page *dynamodb.ScanOutput, lastPage bool) bool {
return d.write(convert(page.Items)) || lastPage
})
}
func convert(items []map[string]*dynamodb.AttributeValue) []interface{} {
ret := []interface{}{}
err := dynamodbattribute.UnmarshalListOfMaps(items, &ret)
if err != nil {
panic(err)
}
return ret
}
func (d *Dynamo) write(jsonItems []interface{}) bool {
var marshaller = json.Marshal
if isatty.IsTerminal(os.Stdout.Fd()) {
f := colorjson.NewFormatter()
f.Indent = 2
marshaller = f.Marshal
}
for _, item := range jsonItems {
bytes, _ := marshaller(item)
_, err := fmt.Fprintln(d.w, string(bytes))
if err != nil {
if err == io.ErrClosedPipe {
return false
}
spew.Fdump(os.Stderr, err)
panic(err)
}
d.emitted++
if maxCount > 0 && d.emitted >= maxCount {
return false
}
}
return true
}