Skip to content

Commit

Permalink
feat(cli): fix option truncate -> delete
Browse files Browse the repository at this point in the history
  • Loading branch information
shuntaka9576 committed May 29, 2022
1 parent 6899127 commit b871cd1
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 153 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
*.jsonl
!testdata/*.jsonl
.docker
ddbrew

dist/
17 changes: 5 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
lint:
staticcheck ./...

clean:
-rm ddbrew
BIN := ddbrew
VERSION := $(shell git describe --tags)
REVISION := $(shell git rev-parse --short HEAD)

build:
go build ./cmd/ddbrew/ddbrew.go

test:
go test

all: lint test
go build -ldflags="-s -w -X github.com/shuntaka9576/$(BIN)/cli.Version=$(VERSION) -X github.com/shuntaka9576/$(BIN)/cli.Revision=$(REVISION)" -o ddbrew ./cmd/$(BIN)

.PHONY: lint build clean test all
.PHONY: build
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ ddbrew restore fooTable \
--file ./testdata/1.jsonl
```

### Truncate
### Delete
Read jsonl file and delete table data. Note that the deletion also consumes RUC/RRU.

```bash
ddbrew truncate fooTable \
ddbrew delete fooTable \
--file ./testdata/1.jsonl
```

Expand Down
26 changes: 26 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cli

import (
"fmt"

"github.com/alecthomas/kong"
)

var Version string
var Revision = "HEAD"

var embedVersion = "0.0.1"

type VersionFlag string

func (v VersionFlag) Decode(ctx *kong.DecodeContext) error { return nil }
func (v VersionFlag) IsBool() bool { return true }
func (v VersionFlag) BeforeApply(app *kong.Kong, vars kong.Vars) error {
if Version == "" {
Version = embedVersion
}
fmt.Printf("ddbrew version %s (rev:%s)\n", Version, Revision)
app.Exit(0)

return nil
}
23 changes: 4 additions & 19 deletions cli/truncate.go → cli/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,18 @@ import (
"github.com/shuntaka9576/ddbrew"
)

type TruncateOption struct {
type DeleteOption struct {
TableName string
FilePath string
DryRun bool
Limit int
}

func (c *TruncateOption) validate() error {
if c.FilePath == "" {
return ErrorOptInputError
}

return nil
}

func Truncate(ctx context.Context, opt *TruncateOption) error {
err := opt.validate()
if err != nil {
return err
}

func Delete(ctx context.Context, opt *DeleteOption) error {
var f *os.File
f, err = os.Open(opt.FilePath)

f, err := os.Open(opt.FilePath)
if err != nil {
return err

}

tinfo, err := ddbrew.DdbClient.DescribeTable(ctx, &dynamodb.DescribeTableInput{
Expand Down Expand Up @@ -70,7 +55,7 @@ func Truncate(ctx context.Context, opt *TruncateOption) error {
limitUnit = &opt.Limit
}

err = ddbrew.Truncate(ctx, &ddbrew.TruncateOption{
err = ddbrew.Delete(ctx, &ddbrew.DeleteOption{
TableName: opt.TableName,
File: f,
LimitUnit: limitUnit,
Expand Down
1 change: 0 additions & 1 deletion cli/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ import (
)

var (
ErrorOptInputError = errors.New("ErrorOptInputError")
ErrorDescribeTable = errors.New("ErrorDescribeTable")
)
16 changes: 1 addition & 15 deletions cli/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,9 @@ type RestoreOption struct {
Limit int
}

func (c *RestoreOption) validate() error {
if c.FilePath == "" {
return ErrorOptInputError
}

return nil
}

func Restore(ctx context.Context, opt *RestoreOption) error {
err := opt.validate()
if err != nil {
return err
}

var f *os.File
f, err = os.Open(opt.FilePath)

f, err := os.Open(opt.FilePath)
if err != nil {
return err
}
Expand Down
92 changes: 0 additions & 92 deletions cmd/ddbrew/ddbrew.go

This file was deleted.

6 changes: 3 additions & 3 deletions truncate.go → delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import (
"github.com/pkg/errors"
)

type TruncateOption struct {
type DeleteOption struct {
TableName string
File *os.File
LimitUnit *int
}

func Truncate(ctx context.Context, opt *TruncateOption) error {
func Delete(ctx context.Context, opt *DeleteOption) error {
tasks := make(chan Task)
results := make(chan Result)
writeItems := make(chan *WriteItem)
Expand All @@ -49,7 +49,7 @@ func Truncate(ctx context.Context, opt *TruncateOption) error {

wo := (&WriteOrchestrator{
Ctx: ctx,
TaskType: TASK_TYPE_TRUNCATE,
TaskType: TASK_TYPE_DELETE,
TableName: tableName,
WriteItems: writeItems,
Tasks: tasks,
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142 h1:8Uy0oSf5co/NZXj
github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
github.com/aws/aws-sdk-go-v2 v1.16.4 h1:swQTEQUyJF/UkEA94/Ga55miiKFoXmm/Zd67XHgmjSg=
github.com/aws/aws-sdk-go-v2 v1.16.4/go.mod h1:ytwTPBG6fXTZLxxeeCCWj2/EMYp/xDUgX+OET6TLNNU=
github.com/aws/aws-sdk-go-v2/config v1.15.7 h1:PrzhYjDpWnGSpjedmEapldQKPW4x8cCNzUI8XOho1CM=
github.com/aws/aws-sdk-go-v2/config v1.15.7/go.mod h1:exERlvqx1OoUHrxQpMgrmfSW0H6B1+r3xziZD3bBXRg=
github.com/aws/aws-sdk-go-v2/config v1.15.9 h1:TK5yNEnFDQ9iaO04gJS/3Y+eW8BioQiCUafW75/Wc3Q=
github.com/aws/aws-sdk-go-v2/config v1.15.9/go.mod h1:rv/l/TbZo67kp99v/3Kb0qV6Fm1KEtKyruEV2GvVfgs=
github.com/aws/aws-sdk-go-v2/credentials v1.12.2 h1:tX4EHQFU4+O9at5QjnwIKb/Qgv7MbgbUNtqTRF0Vu2M=
github.com/aws/aws-sdk-go-v2/credentials v1.12.2/go.mod h1:/XWqDVuzclEKvzileqtD7/t+wIhOogv//6JFlKEe0Wc=
github.com/aws/aws-sdk-go-v2/credentials v1.12.4 h1:xggwS+qxCukXRVXJBJWQJGyUsvuxGC8+J1kKzv2cxuw=
github.com/aws/aws-sdk-go-v2/credentials v1.12.4/go.mod h1:7g+GGSp7xtR823o1jedxKmqRZGqLdoHQfI4eFasKKxs=
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.9.2 h1:DvvtcTzxaQ2Pj0KHKRzsPV4oI8HG4MquzOYhPlQX5Ak=
Expand All @@ -32,8 +28,6 @@ github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.7.5 h1:5luSE
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.7.5/go.mod h1:yu4bJTJjxrsTWxt/Hn90WT5lhGV6auJNyey1+dVW2yA=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.5 h1:gRW1ZisKc93EWEORNJRvy/ZydF3o6xLSveJHdi1Oa0U=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.5/go.mod h1:ZbkttHXaVn3bBo/wpJbQGiiIWR90eTBUVBrEHUEQlho=
github.com/aws/aws-sdk-go-v2/service/sso v1.11.5 h1:TfJ/zuOYvHnxkvohSwAF3Ppn9KT/SrGZuOZHTPy8Guw=
github.com/aws/aws-sdk-go-v2/service/sso v1.11.5/go.mod h1:TFVe6Rr2joVLsYQ1ABACXgOC6lXip/qpX2x5jWg/A9w=
github.com/aws/aws-sdk-go-v2/service/sso v1.11.7 h1:suAGD+RyiHWPPihZzY+jw4mCZlOFWgmdjb2AeTenz7c=
github.com/aws/aws-sdk-go-v2/service/sso v1.11.7/go.mod h1:TFVe6Rr2joVLsYQ1ABACXgOC6lXip/qpX2x5jWg/A9w=
github.com/aws/aws-sdk-go-v2/service/sts v1.16.6 h1:aYToU0/iazkMY67/BYLt3r6/LT/mUtarLAF5mGof1Kg=
Expand Down
6 changes: 3 additions & 3 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

const (
TASK_TYPE_RESTORE = "TASK_NAME_RESTORE"
TASK_TYPE_TRUNCATE = "TASK_TYPE_TRUNCATE"
TASK_TYPE_RESTORE = "TASK_NAME_RESTORE"
TASK_TYPE_DELETE = "TASK_TYPE_DELETE"
)

type Result struct {
Expand Down Expand Up @@ -53,7 +53,7 @@ func (t *Task) Run() Result {

if t.taskType == TASK_TYPE_RESTORE {
err = attributevalue.UnmarshalMap(item.PutRequest.Item, &parsedJl)
} else if t.taskType == TASK_TYPE_TRUNCATE {
} else if t.taskType == TASK_TYPE_DELETE {
err = attributevalue.UnmarshalMap(item.DeleteRequest.Key, &parsedJl)
}
if err != nil {
Expand Down

0 comments on commit b871cd1

Please sign in to comment.