Skip to content

Commit

Permalink
Merge pull request #10 from halprin/set-endpoint
Browse files Browse the repository at this point in the history
Use Custom Endpoint
  • Loading branch information
halprin authored Feb 28, 2021
2 parents dfc2c64 + 2022c36 commit 74007d2
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 17 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ _**Warning**: running this command will result in all the items in the specified
is no "are you sure?" prompt._

```shell
delete-dynamodb-items <table name>
delete-dynamodb-items <table name> [--endpoint=URL]
```

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

### Custom Endpoint

You can customize the DynamoDB endpoint with the `--endpoint=` (or `-e`) option. Set it to the URL of the endpoint.
E.g. `--endpoint=http://localhost:8002`. If unspecified, the default AWS endpoints are used.

## Build

Run the following to compile your own copy from source.
Expand Down
17 changes: 3 additions & 14 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
package main

import (
"errors"
"github.com/halprin/delete-dynamodb-items/dynamo"
"github.com/halprin/delete-dynamodb-items/external/cli"
"log"
"os"
)

func main() {
log.Println("Start")

tableName, err := getTableName()
if err != nil {
killExecution(err)
}
cli.FillConfig()

err = dynamo.DeleteAllItemsInTable(tableName)
err := dynamo.DeleteAllItemsInTable()
if err != nil {
killExecution(err)
}
log.Println("Complete")
}

func getTableName() (string, error) {
if len(os.Args) < 2 {
return "", errors.New("Provide a table name for the first argument")
}
return os.Args[1], nil
}

func killExecution(err error) {
log.Println("Failure")
log.Fatal(err.Error())
Expand Down
20 changes: 20 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package config

var tableName *string
var dynamoDbEndpoint *string

func SetTableName(name string) {
tableName = &name
}

func GetTableName() *string {
return tableName
}

func SetDynamoDbEndpoint(endpoint string) {
dynamoDbEndpoint = &endpoint
}

func GetDynamoDbEndpoint() *string {
return dynamoDbEndpoint
}
11 changes: 10 additions & 1 deletion dynamo/dynamo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/halprin/delete-dynamodb-items/config"
"log"
)

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

func DeleteAllItemsInTable(tableName string) error {
func DeleteAllItemsInTable() error {
if sessionErr != nil {
log.Println("Initial AWS session failed")
return sessionErr
}

endpoint := config.GetDynamoDbEndpoint()
if endpoint != nil {
log.Printf("Using the custom endpoint %s", *endpoint)
dynamoService = dynamodb.New(awsSession, aws.NewConfig().WithEndpoint(*endpoint))
}

tableName := *config.GetTableName()

items, err := getItems(tableName)
if err != nil {
return err
Expand Down
34 changes: 34 additions & 0 deletions external/cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cli

import (
"github.com/halprin/delete-dynamodb-items/config"
"github.com/teris-io/cli"
"os"
)

func FillConfig() {
endpointKey := "endpoint"
tableNameCliArg := cli.NewArg("table name", "The name of the table for which all the items will be deleted").WithType(cli.TypeString)
endpointCliOption := cli.NewOption(endpointKey, "A URL of the DynamoDB endpoint to use").WithChar('e').WithType(cli.TypeString)

parser := cli.New("Deletes all the items in a DynamoDB table").WithArg(tableNameCliArg).WithOption(endpointCliOption)

invocation, arguments, options, err := parser.Parse(os.Args)
help, helpExistsInOptions := options["help"]

if err != nil {
_ = parser.Usage(invocation, os.Stdout)
os.Exit(1)
} else if helpExistsInOptions && help == "true" {
_ = parser.Usage(invocation, os.Stdout)
os.Exit(0)
}

tableName := arguments[0]
config.SetTableName(tableName)

endpoint, endpointExistsInOptions := options[endpointKey]
if endpointExistsInOptions {
config.SetDynamoDbEndpoint(endpoint)
}
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/halprin/delete-dynamodb-items

go 1.16

require github.com/aws/aws-sdk-go v1.37.17
require (
github.com/aws/aws-sdk-go v1.37.17
github.com/teris-io/cli v1.0.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/teris-io/cli v1.0.1 h1:J6jnVHC552uqx7zT+Ux0++tIvLmJQULqxVhCid2u/Gk=
github.com/teris-io/cli v1.0.1/go.mod h1:V9nVD5aZ873RU/tQXLSXO8FieVPQhQvuNohsdsKXsGw=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand Down

0 comments on commit 74007d2

Please sign in to comment.