Skip to content

Commit

Permalink
started cli
Browse files Browse the repository at this point in the history
  • Loading branch information
gavincabbage committed Sep 20, 2019
1 parent bada39d commit 4447349
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 11 deletions.
160 changes: 160 additions & 0 deletions cmd/chiv/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package main

import (
"database/sql"
"fmt"
"os"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/urfave/cli"

"gavincabbage.com/chiv"

_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
)

var version = "v0.0.0"

func main() {

app := cli.App{
Name: "chiv",
HelpName: "chiv",
Usage: "Archive relational database",
Version: version,
Description: "Ar[chiv]e relational database tables to Amazon S3",
HideHelp: true,
Action: run,
Flags: []cli.Flag{
cli.StringFlag{
Name: "database, d",
Usage: "Database connection string",
EnvVar: "DATABASE_URL",
Required: true,
},
cli.StringFlag{
Name: "table, t",
Usage: "Database table to archive",
Required: true,
},
cli.StringFlag{
Name: "bucket, b",
Usage: "Upload S3 bucket name",
Required: true,
},
cli.StringFlag{
Name: "driver, r",
Usage: "Database driver type: postgres or mysql",
Required: false,
Value: "postgres",
},
cli.StringSliceFlag{
Name: "columns, c",
Usage: "Database columns to archive, comma-separated",
},
cli.StringFlag{
Name: "format, f",
Usage: "Upload format: csv, yaml or json",
Required: false,
},
cli.StringFlag{
Name: "key, k",
Usage: "Upload key",
},
cli.StringFlag{
Name: "extension, e",
Usage: "Upload extension",
},
cli.StringFlag{
Name: "null, n",
Usage: "Upload null value",
},
cli.BoolFlag{
Name: "help, h",
Usage: "Show usage details",
},
},
}

cli.HandleExitCoder(app.Run(os.Args))
}

func run(ctx *cli.Context) (err error) {
defer func() {
if err != nil {
err = cli.NewExitError(err, 1)
}
}()

if ctx.Bool("help") {
return cli.ShowAppHelp(ctx)
}

config := from(ctx)

db, err := sql.Open(config.driver, config.url)
if err != nil {
return fmt.Errorf("opening database connection: %w", err)
}

awsSession, err := session.NewSessionWithOptions(session.Options{
Config: *aws.NewConfig(),
Profile: os.Getenv("AWS_PROFILE"),
SharedConfigState: session.SharedConfigEnable,
})
if err != nil {
return fmt.Errorf("opening AWS session: %w", err)
}
client := s3.New(awsSession)
uploader := s3manager.NewUploaderWithClient(client)

fmt.Printf("Hello, World! (version %s)\n", version)
return chiv.Archive(db, uploader, config.table, config.bucket, config.options...)

}

type config struct {
url string
table string
bucket string
driver string
options []chiv.Option
}

func from(ctx *cli.Context) config {
cfg := config{
url: ctx.String("url"),
table: ctx.String("table"),
bucket: ctx.String("bucket"),
driver: ctx.String("driver"),
}

if columns := ctx.StringSlice("columns"); columns != nil {
cfg.options = append(cfg.options, chiv.WithColumns(columns...))
}

var m = map[string]chiv.FormatterFunc{
"csv": chiv.CSV,
"yaml": chiv.YAML,
"json": chiv.JSON,
}
if format := ctx.String("format"); format != "" {
cfg.options = append(cfg.options, chiv.WithFormat(m[format]))
}

if key := ctx.String("key"); key != "" {
cfg.options = append(cfg.options, chiv.WithKey(key))
} else if extension := ctx.String("extension"); extension != "" {
cfg.options = append(cfg.options, chiv.WithExtension(extension))
}

if null := ctx.String("null"); null != "" {
cfg.options = append(cfg.options, chiv.WithNull(null))
}

return cfg
}
11 changes: 0 additions & 11 deletions cmd/main.go

This file was deleted.

8 changes: 8 additions & 0 deletions formatters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,13 @@ import (
)

func TestCsvFormatter(t *testing.T) {
t.Skip()
}

func TestYamlFormatter(t *testing.T) {
t.Skip()
}

func TestJsonFormatter(t *testing.T) {
t.Skip()
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/spf13/viper v1.4.0 // indirect
github.com/stretchr/testify v1.4.0
github.com/ultraware/funlen v0.0.2 // indirect
github.com/urfave/cli v1.22.1
golang.org/x/sync v0.0.0-20190423024810-112230192c58
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3 // indirect
golang.org/x/text v0.3.2 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8Nz
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man v1.0.10 h1:BSKMNlYxDvnunlTymqtgONjNnaRV1sTpcovwwjF22jk=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -235,14 +238,19 @@ github.com/quasilyte/go-consistent v0.0.0-20190521200055-c6f3937de18c/go.mod h1:
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.3.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/go-glob v0.0.0-20170128012129-256dc444b735/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
github.com/shirou/gopsutil v0.0.0-20180427012116-c95755e4bcd7/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4/go.mod h1:qsXQc7+bwAM3Q1u/4XEfrquwF8Lw7D7y5cD8CuHnfIc=
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e h1:MZM7FHLqUHYI0Y/mQAt3d2aYa0SiNms/hFqC9qJYolM=
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041 h1:llrF3Fs4018ePo4+G/HV/uQUqEI1HMDjCeOf2V6puPc=
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.0.5 h1:8c8b5uO0zS4X6RPl/sd1ENwSkIc0/H2PaHxE3udaE8I=
github.com/sirupsen/logrus v1.0.5/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
Expand Down Expand Up @@ -294,6 +302,8 @@ github.com/ultraware/funlen v0.0.1 h1:UeC9tpM4wNWzUJfan8z9sFE4QCzjjzlCZmuJN+aOkH
github.com/ultraware/funlen v0.0.1/go.mod h1:Dp4UiAus7Wdb9KUZsYWZEWiRzGuM2kXM1lPbfaF6xhA=
github.com/ultraware/funlen v0.0.2 h1:Av96YVBwwNSe4MLR7iI/BIa3VyI7/djnto/pK3Uxbdo=
github.com/ultraware/funlen v0.0.2/go.mod h1:Dp4UiAus7Wdb9KUZsYWZEWiRzGuM2kXM1lPbfaF6xhA=
github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.2.0/go.mod h1:4vX61m6KN+xDduDNwXrhIAVZaZaZiQ1luJk8LWSxF3s=
github.com/valyala/quicktemplate v1.1.1/go.mod h1:EH+4AkTd43SvgIbQHYu59/cJyxDoOVRUAfrukLPuGJ4=
Expand Down

0 comments on commit 4447349

Please sign in to comment.