Skip to content

Commit

Permalink
[cmd/*] Switch to pflag for all CLI flag parsing (vitessio#10619)
Browse files Browse the repository at this point in the history
* Switch to `pflag` for all parsing

This transparently swaps the cli parsing library used by `internal/flag`
from the standard library `flag` package to `spf13/pflag`.

It also introduces hook points for packages throughout the vitess codebase
to register their flags for either all commands using `servenv` or a
particular subset of commands. This allows these packages to continue to
define their flag variables in a package-private way, but without
polluting the global flagset.

Signed-off-by: Andrew Mason <[email protected]>

* Workaround exit code difference between stdlib `flag` and `pflag`

tl;dr stdlib `flag` has [this][1] and `pflag` does not

[1]: golang/go@dcf0929

Signed-off-by: Andrew Mason <[email protected]>

* adjust test data for difference in spacing between pflag/stdflag

Signed-off-by: Andrew Mason <[email protected]>

* update lingering legacy flag tests

Signed-off-by: Andrew Mason <[email protected]>

* Update vtgate/tabletgateway.go to use new interface to isolate flags, update test data

Signed-off-by: Andrew Mason <[email protected]>

* update flags in java test code

Signed-off-by: Andrew Mason <[email protected]>
  • Loading branch information
Andrew Mason authored Jul 25, 2022
1 parent cc4f018 commit c3f4a99
Show file tree
Hide file tree
Showing 27 changed files with 872 additions and 1,486 deletions.
4 changes: 3 additions & 1 deletion go/cmd/mysqlctl/mysqlctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"os"
"time"

"github.com/spf13/pflag"

"vitess.io/vitess/go/cmd"
"vitess.io/vitess/go/exit"
"vitess.io/vitess/go/flagutil"
Expand Down Expand Up @@ -248,7 +250,7 @@ func main() {
exit.Return(1)
}
dbconfigs.RegisterFlags(dbconfigs.Dba)
_flag.Parse()
_flag.Parse(pflag.NewFlagSet("mysqlctl", pflag.ExitOnError))

tabletAddr = netutil.JoinHostPort("localhost", int32(*port))

Expand Down
4 changes: 3 additions & 1 deletion go/cmd/query_analyzer/query_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"os"
"sort"

"github.com/spf13/pflag"

"vitess.io/vitess/go/exit"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/sqlparser"
Expand Down Expand Up @@ -59,7 +61,7 @@ func (a stats) Less(i, j int) bool { return a[i].Count > a[j].Count }

func main() {
defer exit.Recover()
_flag.Parse()
_flag.Parse(pflag.NewFlagSet("query_analyzer", pflag.ExitOnError))
for _, filename := range _flag.Args() {
fmt.Printf("processing: %s\n", filename)
if err := processFile(filename); err != nil {
Expand Down
4 changes: 3 additions & 1 deletion go/cmd/topo2topo/topo2topo.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"fmt"
"os"

"github.com/spf13/pflag"

"vitess.io/vitess/go/exit"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
Expand Down Expand Up @@ -53,7 +55,7 @@ func main() {
defer exit.RecoverAll()
defer logutil.Flush()

_flag.Parse()
_flag.Parse(pflag.NewFlagSet("topo2topo", pflag.ExitOnError))
args := _flag.Args()
if len(args) != 0 {
flag.Usage()
Expand Down
4 changes: 3 additions & 1 deletion go/cmd/vtbench/vtbench.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"strings"
"time"

"github.com/spf13/pflag"

"vitess.io/vitess/go/exit"
"vitess.io/vitess/go/vt/dbconfigs"
"vitess.io/vitess/go/vt/log"
Expand Down Expand Up @@ -104,7 +106,7 @@ func main() {
defer exit.Recover()

flag.Lookup("logtostderr").Value.Set("true")
_flag.Parse()
_flag.Parse(pflag.NewFlagSet("vtbench", pflag.ExitOnError))

clientProto := vtbench.MySQL
switch *protocol {
Expand Down
3 changes: 2 additions & 1 deletion go/cmd/vtclient/vtclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"time"

"github.com/olekukonko/tablewriter"
"github.com/spf13/pflag"

"vitess.io/vitess/go/vt/concurrency"
"vitess.io/vitess/go/vt/log"
Expand Down Expand Up @@ -147,7 +148,7 @@ func main() {
}

func run() (*results, error) {
_flag.Parse()
_flag.Parse(pflag.NewFlagSet("vtclient", pflag.ExitOnError))
args := _flag.Args()

if len(args) == 0 {
Expand Down
4 changes: 3 additions & 1 deletion go/cmd/vtctlclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"strings"
"time"

"github.com/spf13/pflag"

"vitess.io/vitess/go/exit"
"vitess.io/vitess/go/trace"
"vitess.io/vitess/go/vt/log"
Expand Down Expand Up @@ -70,7 +72,7 @@ func checkDeprecations(args []string) {
func main() {
defer exit.Recover()

_flag.Parse()
_flag.Parse(pflag.NewFlagSet("vtctlclient", pflag.ExitOnError))

closer := trace.StartTracing("vtctlclient")
defer trace.LogErrorsWhenClosing(closer)
Expand Down
2 changes: 1 addition & 1 deletion go/cmd/vtgate/vtgate.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func main() {
log.Exitf("tablet_types_to_wait should contain at least one serving tablet type")
}

err := CheckCellFlags(context.Background(), resilientServer, *cell, *vtgate.CellsToWatch)
err := CheckCellFlags(context.Background(), resilientServer, *cell, vtgate.CellsToWatch)
if err != nil {
log.Exitf("cells_to_watch validation failed: %v", err)
}
Expand Down
4 changes: 3 additions & 1 deletion go/cmd/vtgr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"flag"
"strings"

"github.com/spf13/pflag"

"vitess.io/vitess/go/vt/vtgr"

// Include deprecation warnings for soon-to-be-unsupported flag invocations.
Expand All @@ -26,7 +28,7 @@ import (

func main() {
clustersToWatch := flag.String("clusters_to_watch", "", "Comma-separated list of keyspaces or keyspace/shards that this instance will monitor and repair. Defaults to all clusters in the topology. Example: \"ks1,ks2/-80\"")
_flag.Parse()
_flag.Parse(pflag.NewFlagSet("vtgr", pflag.ExitOnError))

// openTabletDiscovery will open up a connection to topo server
// and populate the tablets in memory
Expand Down
4 changes: 3 additions & 1 deletion go/cmd/vttlstest/vttlstest.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"fmt"
"io"

"github.com/spf13/pflag"

"vitess.io/vitess/go/exit"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
Expand Down Expand Up @@ -137,7 +139,7 @@ func main() {
_flag.SetUsage(flag.CommandLine, _flag.UsageOptions{
Preface: func(w io.Writer) { fmt.Fprint(w, doc) },
})
_flag.Parse()
_flag.Parse(pflag.NewFlagSet("vttlstest", pflag.ExitOnError))
args := _flag.Args()
if len(args) == 0 {
flag.Usage()
Expand Down
6 changes: 4 additions & 2 deletions go/cmd/zk/zkcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"syscall"
"time"

"github.com/spf13/pflag"
"github.com/z-division/go-zookeeper/zk"
"golang.org/x/term"

Expand Down Expand Up @@ -140,10 +141,11 @@ func main() {
defer exit.Recover()
defer logutil.Flush()

_flag.SetUsage(flag.CommandLine, _flag.UsageOptions{
fs := pflag.NewFlagSet("zkcmd", pflag.ExitOnError)
_flag.SetUsage(flag.CommandLine, _flag.UsageOptions{ // TODO: hmmm
Epilogue: func(w io.Writer) { fmt.Fprint(w, doc) },
})
_flag.Parse()
_flag.Parse(fs)
args := _flag.Args()
if len(args) == 0 {
flag.Usage()
Expand Down
4 changes: 3 additions & 1 deletion go/cmd/zkctl/zkctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"io"
"os"

"github.com/spf13/pflag"

"vitess.io/vitess/go/exit"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
Expand Down Expand Up @@ -60,7 +62,7 @@ func main() {
defer exit.Recover()
defer logutil.Flush()

_flag.Parse()
_flag.Parse(pflag.NewFlagSet("zkctl", pflag.ExitOnError))
args := _flag.Args()

if len(args) == 0 {
Expand Down
4 changes: 3 additions & 1 deletion go/cmd/zkctld/zkctld.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"os/signal"
"syscall"

"github.com/spf13/pflag"

"vitess.io/vitess/go/exit"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
Expand All @@ -45,7 +47,7 @@ func main() {
defer exit.Recover()
defer logutil.Flush()

_flag.Parse()
_flag.Parse(pflag.NewFlagSet("zkctld", pflag.ExitOnError))

zkConfig := zkctl.MakeZkConfigFromString(*zkCfg, uint32(*myID))
zkd := zkctl.NewZkd(zkConfig)
Expand Down
Loading

0 comments on commit c3f4a99

Please sign in to comment.