Skip to content
This repository was archived by the owner on Aug 21, 2023. It is now read-only.

Commit 7104b68

Browse files
authored
use cobra to simplify command flags (#28)
1 parent cf05a05 commit 7104b68

File tree

6 files changed

+142
-37
lines changed

6 files changed

+142
-37
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
bin/
33
coverage.txt
44
.idea
5+
var

cmd/dumpling/main.go

+29-35
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package main
1515

1616
import (
17-
"flag"
1817
"fmt"
1918
_ "net/http/pprof"
2019
"os"
@@ -23,6 +22,7 @@ import (
2322
"github.com/pingcap/dumpling/v4/cli"
2423
"github.com/pingcap/dumpling/v4/export"
2524
"github.com/pingcap/dumpling/v4/log"
25+
"github.com/spf13/cobra"
2626
"go.uber.org/zap"
2727
)
2828

@@ -38,48 +38,38 @@ var (
3838
logLevel string
3939
consistency string
4040
snapshot string
41-
)
42-
43-
func init() {
44-
flag.StringVar(&database, "database", "", "Database to dump")
45-
flag.StringVar(&database, "B", "", "Database to dump")
46-
47-
flag.StringVar(&host, "h", "127.0.0.1", "The host to connect to")
48-
flag.StringVar(&host, "host", "127.0.0.1", "The host to connect to")
49-
50-
flag.StringVar(&user, "user", "root", "Username with privileges to run the dump")
51-
flag.StringVar(&user, "u", "root", "Username with privileges to run the dump")
52-
53-
flag.IntVar(&port, "port", 4000, "TCP/IP port to connect to")
54-
flag.IntVar(&port, "P", 4000, "TCP/IP port to connect to")
55-
56-
flag.StringVar(&password, "password", "", "User password")
57-
flag.StringVar(&password, "p", "", "User password")
58-
59-
flag.IntVar(&threads, "threads", 4, "Number of goroutines to use, default 4")
60-
flag.IntVar(&threads, "t", 4, "Number of goroutines to use, default 4")
61-
62-
flag.Uint64Var(&fileSize, "F", export.UnspecifiedSize, "The approximate size of output file")
63-
flag.Uint64Var(&fileSize, "filesize", export.UnspecifiedSize, "The approximate size of output file")
64-
65-
flag.StringVar(&outputDir, "output", defaultOutputDir, "Output directory")
66-
flag.StringVar(&outputDir, "o", defaultOutputDir, "Output directory")
67-
68-
flag.StringVar(&logLevel, "loglevel", "info", "Log level: {debug|info|warn|error|dpanic|panic|fatal}")
6941

70-
flag.StringVar(&consistency, "consistency", "auto", "Consistency level during dumping: {auto|none|flush|lock|snapshot}")
71-
72-
flag.StringVar(&snapshot, "snapshot", "", "Snapshot position. Valid only when consistency=snapshot")
73-
}
42+
rootCmd = &cobra.Command{
43+
Use: "dumpling",
44+
Short: "A tool to dump MySQL/TiDB data",
45+
Long: `Dumpling is a CLI tool that helps you dump MySQL/TiDB data`,
46+
Run: func(cmd *cobra.Command, args []string) {
47+
run()
48+
},
49+
}
50+
)
7451

7552
var defaultOutputDir = timestampDirName()
7653

7754
func timestampDirName() string {
7855
return fmt.Sprintf("./export-%s", time.Now().Format(time.RFC3339))
7956
}
8057

81-
func main() {
82-
flag.Parse()
58+
func init() {
59+
rootCmd.PersistentFlags().StringVarP(&database, "database", "B", "", "Database to dump")
60+
rootCmd.PersistentFlags().StringVarP(&host, "host", "H", "127.0.0.1", "The host to connect to")
61+
rootCmd.PersistentFlags().StringVarP(&user, "user", "u", "root", "Username with privileges to run the dump")
62+
rootCmd.PersistentFlags().IntVarP(&port, "port", "P", 4000, "TCP/IP port to connect to")
63+
rootCmd.PersistentFlags().StringVarP(&password, "password", "p", "", "User password")
64+
rootCmd.PersistentFlags().IntVarP(&threads, "threads", "t", 4, "Number of goroutines to use, default 4")
65+
rootCmd.PersistentFlags().Uint64VarP(&fileSize, "filesize", "F", export.UnspecifiedSize, "The approximate size of output file")
66+
rootCmd.PersistentFlags().StringVarP(&outputDir, "output", "o", defaultOutputDir, "Output directory")
67+
rootCmd.PersistentFlags().StringVar(&logLevel, "loglevel", "info", "Log level: {debug|info|warn|error|dpanic|panic|fatal}")
68+
rootCmd.PersistentFlags().StringVar(&consistency, "consistency", "auto", "Consistency level during dumping: {auto|none|flush|lock|snapshot}")
69+
rootCmd.PersistentFlags().StringVar(&snapshot, "snapshot", "", "Snapshot position. Valid only when consistency=snapshot")
70+
}
71+
72+
func run() {
8373
println(cli.LongVersion())
8474

8575
err := log.InitAppLogger(&log.Config{Level: logLevel})
@@ -105,3 +95,7 @@ func main() {
10595
}
10696
return
10797
}
98+
99+
func main() {
100+
rootCmd.Execute()
101+
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/pingcap/check v0.0.0-20191107115940-caf2b9e6ccf4
88
github.com/pingcap/errors v0.11.0
99
github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9
10+
github.com/spf13/cobra v0.0.6
1011
go.uber.org/zap v1.12.0
1112
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
1213
google.golang.org/appengine v1.6.5 // indirect

0 commit comments

Comments
 (0)