-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
159 lines (138 loc) · 3.8 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
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"errors"
"fmt"
"os"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
func defaultFlags() []cli.Flag {
return []cli.Flag{
cli.StringFlag{
Name: "source-table, st",
Value: "",
Usage: "Required: Dynamodb Table Name to source data from",
EnvVar: "SOURCE_TABLE",
},
cli.StringFlag{
Name: "source-region, sr",
Value: "us-east-1",
Usage: "aws region of source table",
EnvVar: "SOURCE_REGION",
},
cli.StringFlag{
Name: "source-account, sa",
Value: "",
Usage: "AWS Account number of the source table",
EnvVar: "SOURCE_ACCOUNT",
},
cli.StringFlag{
Name: "destination-table, dt",
Value: "",
Usage: "Required: Dynamodb Table Name to write to",
EnvVar: "DESTINATION_TABLE",
},
cli.StringFlag{
Name: "destination-region, dr",
Value: "us-east-1",
Usage: "aws region of destination table",
EnvVar: "DESTINATION_REGION",
},
cli.StringFlag{
Name: "destination-account, da",
Value: "",
Usage: "AWS Account number of the destination table",
EnvVar: "DESTINATION_ACCOUNT",
},
cli.StringFlag{
Name: "role, r",
Value: "",
Usage: "AWS Role Name to assume for source/dest accounts. Must be present in both accounts",
EnvVar: "ROLE",
},
cli.StringFlag{
Name: "log-level",
Value: "error",
Usage: "Log level (panic, fatal, error, warn, info, or debug)",
EnvVar: "LOG_LEVEL",
},
}
}
func parseParams(c *cli.Context) error {
logLevelString := c.String("log-level")
logLevel, err := log.ParseLevel(logLevelString)
if err != nil {
return err
}
log.SetLevel(logLevel)
// required flags
if !c.IsSet("source-table") || !c.IsSet("destination-table") {
return errors.New("source-table and destination-table flags are required")
}
// conditionally required flags
anyCrossAccountFlagsSet := c.IsSet("source-account") || c.IsSet("destination-account") || c.IsSet("role")
allCrossAccountFlagsSet := c.IsSet("source-account") && c.IsSet("destination-account") && c.IsSet("role")
if anyCrossAccountFlagsSet && !allCrossAccountFlagsSet {
return errors.New("If any of [source-account, destination-account, role] flags are provided all must be provided")
}
return nil
}
func initApp() *cli.App {
app := cli.NewApp()
app.Name = "dynamodb-migrator"
app.Usage = "Migrate data from one dynamodb table to another"
app.Version = fmt.Sprintf("0.1.0")
app.Flags = defaultFlags()
app.Action = cli.ActionFunc(defaultAction)
app.Commands = []cli.Command{
{
Name: "migrate",
Action: defaultAction,
Usage: "Migrates data from Source Table to Destination Table",
Flags: defaultFlags(),
},
{
Name: "compare",
Action: compare,
Usage: "Compares item counts from Source Table and Destination Table",
Flags: defaultFlags(),
},
}
return app
}
func defaultAction(c *cli.Context) error {
err := parseParams(c)
if err != nil {
return err
}
migrator := NewMigrator(&MigratorConfig{
SourceTable: c.String("source-table"),
SourceRegion: c.String("source-region"),
SourceAccount: c.String("source-account"),
DestTable: c.String("destination-table"),
DestRegion: c.String("destination-region"),
DestAccount: c.String("destination-account"),
Role: c.String("role"),
})
return migrator.Migrate()
}
func compare(c *cli.Context) error {
err := parseParams(c)
if err != nil {
return err
}
migrator := NewMigrator(&MigratorConfig{
SourceTable: c.String("source-table"),
SourceRegion: c.String("source-region"),
SourceAccount: c.String("source-account"),
DestTable: c.String("destination-table"),
DestRegion: c.String("destination-region"),
DestAccount: c.String("destination-account"),
Role: c.String("role"),
})
return migrator.Compare()
}
func main() {
app := initApp()
app.Run(os.Args)
}