diff --git a/README.md b/README.md index 9b5c207..0816491 100644 --- a/README.md +++ b/README.md @@ -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 +delete-dynamodb-items
[--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. diff --git a/cmd/main.go b/cmd/main.go index 0b9b421..3bbdc7c 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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()) diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..a67d3b4 --- /dev/null +++ b/config/config.go @@ -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 +} diff --git a/dynamo/dynamo.go b/dynamo/dynamo.go index c11c4f1..a9b46e6 100644 --- a/dynamo/dynamo.go +++ b/dynamo/dynamo.go @@ -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 diff --git a/external/cli/cli.go b/external/cli/cli.go new file mode 100644 index 0000000..59c24c0 --- /dev/null +++ b/external/cli/cli.go @@ -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) + } +} diff --git a/go.mod b/go.mod index 9fac5b3..1efcf66 100644 --- a/go.mod +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum index 1f81d1c..e6b8513 100644 --- a/go.sum +++ b/go.sum @@ -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=