Skip to content

Commit

Permalink
Added --version, --logfile and --logfmt flags (pingcap#72)
Browse files Browse the repository at this point in the history
* cmd: replace cobra by pflag

we don't need subcommand parsing, let's choose a less heavyweight library

* cmd: added the --version/-V flag

* log: support configurating log file and format
  • Loading branch information
kennytm authored May 20, 2020
1 parent 62b8414 commit 0eb24b8
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 143 deletions.
87 changes: 46 additions & 41 deletions cmd/dumpling/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
package main

import (
"errors"
"fmt"
_ "net/http/pprof"
"os"
"time"

"github.com/pingcap/dumpling/v4/cli"
"github.com/pingcap/dumpling/v4/export"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

var (
Expand All @@ -35,6 +36,8 @@ var (
fileSize uint64
statementSize uint64
logLevel string
logFile string
logFormat string
consistency string
snapshot string
noViews bool
Expand All @@ -49,15 +52,6 @@ var (
sql string

escapeBackslash bool

rootCmd = &cobra.Command{
Use: "dumpling",
Short: "A tool to dump MySQL/TiDB data",
Long: `Dumpling is a CLI tool that helps you dump MySQL/TiDB data`,
Run: func(cmd *cobra.Command, args []string) {
run()
},
}
)

var defaultOutputDir = timestampDirName()
Expand All @@ -66,35 +60,49 @@ func timestampDirName() string {
return fmt.Sprintf("./export-%s", time.Now().Format(time.RFC3339))
}

func init() {
rootCmd.PersistentFlags().StringVarP(&database, "database", "B", "", "Database to dump")
rootCmd.PersistentFlags().StringVarP(&host, "host", "H", "127.0.0.1", "The host to connect to")
rootCmd.PersistentFlags().StringVarP(&user, "user", "u", "root", "Username with privileges to run the dump")
rootCmd.PersistentFlags().IntVarP(&port, "port", "P", 4000, "TCP/IP port to connect to")
rootCmd.PersistentFlags().StringVarP(&password, "password", "p", "", "User password")
rootCmd.PersistentFlags().IntVarP(&threads, "threads", "t", 4, "Number of goroutines to use, default 4")
rootCmd.PersistentFlags().Uint64VarP(&fileSize, "filesize", "F", export.UnspecifiedSize, "The approximate size of output file")
rootCmd.PersistentFlags().Uint64VarP(&statementSize, "statement-size", "S", export.UnspecifiedSize, "Attempted size of INSERT statement in bytes")
rootCmd.PersistentFlags().StringVarP(&outputDir, "output", "o", defaultOutputDir, "Output directory")
rootCmd.PersistentFlags().StringVar(&logLevel, "loglevel", "info", "Log level: {debug|info|warn|error|dpanic|panic|fatal}")
rootCmd.PersistentFlags().StringVar(&consistency, "consistency", "auto", "Consistency level during dumping: {auto|none|flush|lock|snapshot}")
rootCmd.PersistentFlags().StringVar(&snapshot, "snapshot", "", "Snapshot position. Valid only when consistency=snapshot")
rootCmd.PersistentFlags().BoolVarP(&noViews, "no-views", "W", true, "Do not dump views")
rootCmd.PersistentFlags().StringVar(&statusAddr, "status-addr", ":8281", "dumpling API server and pprof addr")
rootCmd.PersistentFlags().Uint64VarP(&rows, "rows", "r", export.UnspecifiedSize, "Split table into chunks of this many rows, default unlimited")
rootCmd.PersistentFlags().StringVar(&where, "where", "", "Dump only selected records")
rootCmd.PersistentFlags().BoolVar(&escapeBackslash, "escape-backslash", true, "use backslash to escape quotation marks")
rootCmd.PersistentFlags().StringVar(&fileType, "filetype", "sql", "The type of export file (sql/csv)")
rootCmd.PersistentFlags().BoolVar(&noHeader, "no-header", false, "whether not to dump CSV table header")
rootCmd.PersistentFlags().BoolVarP(&noSchemas, "no-schemas", "m", false, "Do not dump table schemas with the data")
rootCmd.PersistentFlags().BoolVarP(&noData, "no-data", "d", false, "Do not dump table data")
rootCmd.PersistentFlags().StringVar(&csvNullValue, "csv-null-value", "\\N", "The null value used when export to csv")
rootCmd.PersistentFlags().StringVarP(&sql, "sql", "s", "", "Dump data with given sql")
}
func main() {
pflag.Usage = func() {
fmt.Fprint(os.Stderr, "Dumpling is a CLI tool that helps you dump MySQL/TiDB data\n\nUsage:\n dumpling [flags]\n\nFlags:\n")
pflag.PrintDefaults()
}
pflag.ErrHelp = errors.New("")

pflag.StringVarP(&database, "database", "B", "", "Database to dump")
pflag.StringVarP(&host, "host", "H", "127.0.0.1", "The host to connect to")
pflag.StringVarP(&user, "user", "u", "root", "Username with privileges to run the dump")
pflag.IntVarP(&port, "port", "P", 4000, "TCP/IP port to connect to")
pflag.StringVarP(&password, "password", "p", "", "User password")
pflag.IntVarP(&threads, "threads", "t", 4, "Number of goroutines to use, default 4")
pflag.Uint64VarP(&fileSize, "filesize", "F", export.UnspecifiedSize, "The approximate size of output file")
pflag.Uint64VarP(&statementSize, "statement-size", "S", export.UnspecifiedSize, "Attempted size of INSERT statement in bytes")
pflag.StringVarP(&outputDir, "output", "o", defaultOutputDir, "Output directory")
pflag.StringVar(&logLevel, "loglevel", "info", "Log level: {debug|info|warn|error|dpanic|panic|fatal}")
pflag.StringVarP(&logFile, "logfile", "L", "", "Log file `path`, leave empty to write to console")
pflag.StringVar(&logFormat, "logfmt", "text", "Log `format`: {text|json}")
pflag.StringVar(&consistency, "consistency", "auto", "Consistency level during dumping: {auto|none|flush|lock|snapshot}")
pflag.StringVar(&snapshot, "snapshot", "", "Snapshot position. Valid only when consistency=snapshot")
pflag.BoolVarP(&noViews, "no-views", "W", true, "Do not dump views")
pflag.StringVar(&statusAddr, "status-addr", ":8281", "dumpling API server and pprof addr")
pflag.Uint64VarP(&rows, "rows", "r", export.UnspecifiedSize, "Split table into chunks of this many rows, default unlimited")
pflag.StringVar(&where, "where", "", "Dump only selected records")
pflag.BoolVar(&escapeBackslash, "escape-backslash", true, "use backslash to escape quotation marks")
pflag.StringVar(&fileType, "filetype", "sql", "The type of export file (sql/csv)")
pflag.BoolVar(&noHeader, "no-header", false, "whether not to dump CSV table header")
pflag.BoolVarP(&noSchemas, "no-schemas", "m", false, "Do not dump table schemas with the data")
pflag.BoolVarP(&noData, "no-data", "d", false, "Do not dump table data")
pflag.StringVar(&csvNullValue, "csv-null-value", "\\N", "The null value used when export to csv")
pflag.StringVarP(&sql, "sql", "s", "", "Dump data with given sql")

printVersion := pflag.BoolP("version", "V", false, "Print Dumpling version")

pflag.Parse()

func run() {
println(cli.LongVersion())

if *printVersion {
return
}

conf := export.DefaultConfig()
conf.Database = database
conf.Host = host
Expand All @@ -112,6 +120,8 @@ func run() {
conf.Where = where
conf.EscapeBackslash = escapeBackslash
conf.LogLevel = logLevel
conf.LogFile = logFile
conf.LogFormat = logFormat
conf.FileType = fileType
conf.NoHeader = noHeader
conf.NoSchemas = noSchemas
Expand All @@ -124,9 +134,4 @@ func run() {
fmt.Printf("dump failed: %s\n", err.Error())
os.Exit(1)
}
return
}

func main() {
rootCmd.Execute()
}
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ require (
github.com/DATA-DOG/go-sqlmock v1.4.1
github.com/coreos/go-semver v0.3.0
github.com/go-sql-driver/mysql v1.5.0
github.com/golang/protobuf v1.3.1 // indirect
github.com/pingcap/check v0.0.0-20190102082844-67f458068fc8
github.com/pingcap/errors v0.11.4
github.com/pingcap/log v0.0.0-20200117041106-d28c14d3b1cd
github.com/pingcap/log v0.0.0-20200511115504-543df19646ad
github.com/pingcap/tidb-tools v3.0.13+incompatible
github.com/pkg/errors v0.9.1
github.com/soheilhy/cmux v0.1.4
github.com/spf13/cobra v0.0.6
github.com/spf13/pflag v1.0.3
go.uber.org/zap v1.14.0
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
google.golang.org/grpc v1.21.0 // indirect
)
Loading

0 comments on commit 0eb24b8

Please sign in to comment.