Skip to content

Commit 74007d2

Browse files
authored
Merge pull request #10 from halprin/set-endpoint
Use Custom Endpoint
2 parents dfc2c64 + 2022c36 commit 74007d2

7 files changed

Lines changed: 79 additions & 17 deletions

File tree

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@ _**Warning**: running this command will result in all the items in the specified
1212
is no "are you sure?" prompt._
1313

1414
```shell
15-
delete-dynamodb-items <table name>
15+
delete-dynamodb-items <table name> [--endpoint=URL]
1616
```
1717

1818
The program uses the default AWS credential algorithm to determine what IAM entity and region is used. E.g. the
1919
`~/.aws/credentials` file, the `AWS_*` environment variables, etc.
2020

21+
### Custom Endpoint
22+
23+
You can customize the DynamoDB endpoint with the `--endpoint=` (or `-e`) option. Set it to the URL of the endpoint.
24+
E.g. `--endpoint=http://localhost:8002`. If unspecified, the default AWS endpoints are used.
25+
2126
## Build
2227

2328
Run the following to compile your own copy from source.

cmd/main.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,23 @@
11
package main
22

33
import (
4-
"errors"
54
"github.com/halprin/delete-dynamodb-items/dynamo"
5+
"github.com/halprin/delete-dynamodb-items/external/cli"
66
"log"
7-
"os"
87
)
98

109
func main() {
1110
log.Println("Start")
1211

13-
tableName, err := getTableName()
14-
if err != nil {
15-
killExecution(err)
16-
}
12+
cli.FillConfig()
1713

18-
err = dynamo.DeleteAllItemsInTable(tableName)
14+
err := dynamo.DeleteAllItemsInTable()
1915
if err != nil {
2016
killExecution(err)
2117
}
2218
log.Println("Complete")
2319
}
2420

25-
func getTableName() (string, error) {
26-
if len(os.Args) < 2 {
27-
return "", errors.New("Provide a table name for the first argument")
28-
}
29-
return os.Args[1], nil
30-
}
31-
3221
func killExecution(err error) {
3322
log.Println("Failure")
3423
log.Fatal(err.Error())

config/config.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package config
2+
3+
var tableName *string
4+
var dynamoDbEndpoint *string
5+
6+
func SetTableName(name string) {
7+
tableName = &name
8+
}
9+
10+
func GetTableName() *string {
11+
return tableName
12+
}
13+
14+
func SetDynamoDbEndpoint(endpoint string) {
15+
dynamoDbEndpoint = &endpoint
16+
}
17+
18+
func GetDynamoDbEndpoint() *string {
19+
return dynamoDbEndpoint
20+
}

dynamo/dynamo.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,27 @@ import (
44
"github.com/aws/aws-sdk-go/aws"
55
"github.com/aws/aws-sdk-go/aws/session"
66
"github.com/aws/aws-sdk-go/service/dynamodb"
7+
"github.com/halprin/delete-dynamodb-items/config"
78
"log"
89
)
910

1011
var awsSession, sessionErr = session.NewSession()
1112
var dynamoService = dynamodb.New(awsSession)
1213

13-
func DeleteAllItemsInTable(tableName string) error {
14+
func DeleteAllItemsInTable() error {
1415
if sessionErr != nil {
1516
log.Println("Initial AWS session failed")
1617
return sessionErr
1718
}
1819

20+
endpoint := config.GetDynamoDbEndpoint()
21+
if endpoint != nil {
22+
log.Printf("Using the custom endpoint %s", *endpoint)
23+
dynamoService = dynamodb.New(awsSession, aws.NewConfig().WithEndpoint(*endpoint))
24+
}
25+
26+
tableName := *config.GetTableName()
27+
1928
items, err := getItems(tableName)
2029
if err != nil {
2130
return err

external/cli/cli.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cli
2+
3+
import (
4+
"github.com/halprin/delete-dynamodb-items/config"
5+
"github.com/teris-io/cli"
6+
"os"
7+
)
8+
9+
func FillConfig() {
10+
endpointKey := "endpoint"
11+
tableNameCliArg := cli.NewArg("table name", "The name of the table for which all the items will be deleted").WithType(cli.TypeString)
12+
endpointCliOption := cli.NewOption(endpointKey, "A URL of the DynamoDB endpoint to use").WithChar('e').WithType(cli.TypeString)
13+
14+
parser := cli.New("Deletes all the items in a DynamoDB table").WithArg(tableNameCliArg).WithOption(endpointCliOption)
15+
16+
invocation, arguments, options, err := parser.Parse(os.Args)
17+
help, helpExistsInOptions := options["help"]
18+
19+
if err != nil {
20+
_ = parser.Usage(invocation, os.Stdout)
21+
os.Exit(1)
22+
} else if helpExistsInOptions && help == "true" {
23+
_ = parser.Usage(invocation, os.Stdout)
24+
os.Exit(0)
25+
}
26+
27+
tableName := arguments[0]
28+
config.SetTableName(tableName)
29+
30+
endpoint, endpointExistsInOptions := options[endpointKey]
31+
if endpointExistsInOptions {
32+
config.SetDynamoDbEndpoint(endpoint)
33+
}
34+
}

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/halprin/delete-dynamodb-items
22

33
go 1.16
44

5-
require github.com/aws/aws-sdk-go v1.37.17
5+
require (
6+
github.com/aws/aws-sdk-go v1.37.17
7+
github.com/teris-io/cli v1.0.1
8+
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
1010
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1111
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1212
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
13+
github.com/teris-io/cli v1.0.1 h1:J6jnVHC552uqx7zT+Ux0++tIvLmJQULqxVhCid2u/Gk=
14+
github.com/teris-io/cli v1.0.1/go.mod h1:V9nVD5aZ873RU/tQXLSXO8FieVPQhQvuNohsdsKXsGw=
1315
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
1416
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
1517
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=

0 commit comments

Comments
 (0)