Skip to content

Commit

Permalink
feat(cli): fix ignore file
Browse files Browse the repository at this point in the history
  • Loading branch information
shuntaka9576 committed May 29, 2022
1 parent 6865599 commit 666e372
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 3 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: goreleaser

on:
push:
tags:
- "v[0-9]+.[0-9]+.[0-9]+"
jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
-
name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GORELEASER_TOKEN }}
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
*.jsonl
!testdata/*.jsonl
.docker
ddbrew
.docker/
/ddbrew

dist/
2 changes: 1 addition & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
var Version string
var Revision = "HEAD"

var embedVersion = "0.0.1"
var embedVersion = "0.0.2"

type VersionFlag string

Expand Down
99 changes: 99 additions & 0 deletions cmd/ddbrew/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package main

import (
"context"
"errors"
"fmt"
"os"

"github.com/alecthomas/kong"
"github.com/shuntaka9576/ddbrew"
"github.com/shuntaka9576/ddbrew/cli"
)

type Globals struct {
Local string `short:"L" name:"local" help:"Specify DynamoDB local endpoint. ex: (http://)localhost:8000"`
Version cli.VersionFlag `short:"v" name:"version" help:"print the version."`
}

var CLI struct {
Globals
Backup struct {
TableName string `arg:"" name:"tableName" help:"Specify table name to backup."`
File string `short:"f" name:"file" help:"Specify the file path to output (default backup_tableName_yyyymmdd-HHMMSS.jsonl)."`
Limit int `short:"l" help:"Limit the number of reads per second to the specified number (units are automatically determined as RCUs for provisioned tables and RRUs for on-demand tables)."`
} `cmd:"" help:"Backup DynamoDB table."`
Restore struct {
TableName string `arg:"" name:"tableName" help:"Specifies table name to restore."`
File string `short:"f" name:"file" required:"" help:"Specify the jsonline file containing the table data to be restored."`
DryRun bool `short:"d" name:"dry-run" help:"Simulate WRUs/WCUs to consume."`
Limit int `short:"l" name:"limit" help:"Limit the number of writes per second by the specified number (the unit is automatically determined as RCU for provisioned tables and RRU for on-demand tables)."`
} `cmd:"" help:"Restore DynamoDB table."`
Delete struct {
TableName string `arg:"" name:"tableName" help:"Specifies table name to delete."`
File string `short:"f" name:"file" required:"" help:"Specify the jsonline file containing the table data to be delete."`
DryRun bool `short:"d" name:"dry-run" help:"Simulate WRUs/WCUs to consume."`
Limit int `short:"l" name:"limit" help:"Limit the number of request units if the target table is on-demand, or capacity units if the target table is provisioned."`
} `cmd:"" help:"Delete DynamoDB table."`
}

func main() {
kontext := kong.Parse(&CLI,
kong.Name("ddbrew"),
kong.Description("Simple DynamoDB utility"),
)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cmdErrCh := make(chan error)

ddbrew.InitClient(&ddbrew.DDBClientOption{
Local: CLI.Local,
})

go func() {
switch kontext.Command() {
case "backup <tableName>":
cmdErrCh <- cli.Backup(ctx, &cli.BackupOption{
TableName: CLI.Backup.TableName,
FilePath: CLI.Backup.File,
Limit: CLI.Backup.Limit,
})
case "restore <tableName>":
cmdErrCh <- cli.Restore(ctx, &cli.RestoreOption{
TableName: CLI.Restore.TableName,
FilePath: CLI.Restore.File,
DryRun: CLI.Restore.DryRun,
Limit: CLI.Restore.Limit,
})
case "delete <tableName>":
cmdErrCh <- cli.Delete(ctx, &cli.DeleteOption{
TableName: CLI.Delete.TableName,
FilePath: CLI.Delete.File,
DryRun: CLI.Delete.DryRun,
Limit: CLI.Delete.Limit,
})
}
}()

LOOP:
for {
select {
case err := <-cmdErrCh:
if err != nil {
switch {
case errors.Is(err, cli.ErrorDescribeTable):
fmt.Fprintf(os.Stderr, "descirebe table error: %s", err)
default:
fmt.Fprintf(os.Stderr, "%s", err)
}

cancel()
}

break LOOP
case <-ctx.Done():
break LOOP
}
}
}

0 comments on commit 666e372

Please sign in to comment.