Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add positional argument validation #3573

Merged
merged 1 commit into from
Jan 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions go/cmd/automation_server/automation_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package main

import (
"flag"
"fmt"
"net"
"os"
Expand All @@ -34,13 +33,7 @@ func init() {
}

func main() {

flag.Parse()

if *servenv.Version {
servenv.AppVersion.Print()
os.Exit(0)
}
servenv.ParseFlags("automation_server")

fmt.Println("Automation Server, listening on:", *servenv.Port)

Expand Down
8 changes: 1 addition & 7 deletions go/cmd/l2vtgate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"flag"
"math/rand"
"os"
"strings"
"time"

Expand Down Expand Up @@ -55,14 +54,9 @@ func init() {
func main() {
defer exit.Recover()

flag.Parse()
servenv.ParseFlags("l2vtgate")
servenv.Init()

if *servenv.Version {
servenv.AppVersion.Print()
os.Exit(0)
}

ts := topo.Open()
defer ts.Close()

Expand Down
7 changes: 1 addition & 6 deletions go/cmd/mysqlctld/mysqlctld.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,7 @@ func main() {
// mysqlctld only starts and stops mysql, only needs dba.
dbconfigFlags := dbconfigs.DbaConfig
dbconfigs.RegisterFlags(dbconfigFlags)
flag.Parse()

if *servenv.Version {
servenv.AppVersion.Print()
os.Exit(0)
}
servenv.ParseFlags("mysqlctld")

// We'll register this OnTerm handler before mysqld starts, so we get notified
// if mysqld dies on its own without us (or our RPC client) telling it to.
Expand Down
14 changes: 1 addition & 13 deletions go/cmd/vtcombo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package main

import (
"flag"
"os"
"strings"
"time"

Expand Down Expand Up @@ -67,18 +66,7 @@ func main() {
dbconfigs.FilteredConfig | dbconfigs.ReplConfig
dbconfigs.RegisterFlags(dbconfigFlags)
mysqlctl.RegisterFlags()
flag.Parse()

if *servenv.Version {
servenv.AppVersion.Print()
os.Exit(0)
}

if len(flag.Args()) > 0 {
flag.Usage()
log.Errorf("vtcombo doesn't take any positional arguments")
exit.Return(1)
}
servenv.ParseFlags("vtcombo")

// parse the input topology
tpb := &vttestpb.VTTestTopology{}
Expand Down
14 changes: 1 addition & 13 deletions go/cmd/vtctl/vtctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,7 @@ func main() {
defer exit.RecoverAll()
defer logutil.Flush()

flag.Parse()
args := flag.Args()

if *servenv.Version {
servenv.AppVersion.Print()
os.Exit(0)
}

if len(args) == 0 {
flag.Usage()
exit.Return(1)
}

args := servenv.ParseFlagsWithArgs("vtctl")
action := args[0]

startMsg := fmt.Sprintf("USER=%v SUDO_USER=%v %v", os.Getenv("USER"), os.Getenv("SUDO_USER"), strings.Join(os.Args, " "))
Expand Down
10 changes: 1 addition & 9 deletions go/cmd/vtctld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ limitations under the License.
package main

import (
"flag"
"os"

"github.com/youtube/vitess/go/vt/servenv"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/vtctld"
Expand All @@ -35,15 +32,10 @@ var (
)

func main() {
flag.Parse()
servenv.ParseFlags("vtctld")
servenv.Init()
defer servenv.Close()

if *servenv.Version {
servenv.AppVersion.Print()
os.Exit(0)
}

ts = topo.Open()
defer ts.Close()

Expand Down
15 changes: 1 addition & 14 deletions go/cmd/vtexplain/vtexplain.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"flag"
"fmt"
"io/ioutil"
"os"

log "github.com/golang/glog"
"github.com/youtube/vitess/go/exit"
Expand Down Expand Up @@ -124,19 +123,7 @@ func main() {
defer exit.RecoverAll()
defer logutil.Flush()

flag.Parse()

if *servenv.Version {
servenv.AppVersion.Print()
os.Exit(0)
}

args := flag.Args()

if len(args) != 0 {
flag.Usage()
exit.Return(1)
}
servenv.ParseFlags("vtexplain")

err := parseAndRun()
if err != nil {
Expand Down
8 changes: 1 addition & 7 deletions go/cmd/vtgate/vtgate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"flag"
"math/rand"
"os"
"strings"
"time"

Expand Down Expand Up @@ -59,14 +58,9 @@ func init() {
func main() {
defer exit.Recover()

flag.Parse()
servenv.ParseFlags("vtgate")
servenv.Init()

if *servenv.Version {
servenv.AppVersion.Print()
os.Exit(0)
}

if initFakeZK != nil {
initFakeZK()
}
Expand Down
10 changes: 1 addition & 9 deletions go/cmd/vtgateclienttest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ limitations under the License.
package main

import (
"flag"
"os"

"github.com/youtube/vitess/go/cmd/vtgateclienttest/services"
"github.com/youtube/vitess/go/exit"
"github.com/youtube/vitess/go/vt/servenv"
Expand All @@ -36,14 +33,9 @@ func init() {
func main() {
defer exit.Recover()

flag.Parse()
servenv.ParseFlags("vtgateclienttest")
servenv.Init()

if *servenv.Version {
servenv.AppVersion.Print()
os.Exit(0)
}

// The implementation chain.
servenv.OnRun(func() {
s := services.CreateServices()
Expand Down
11 changes: 1 addition & 10 deletions go/cmd/vttablet/vttablet.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main

import (
"flag"
"os"

log "github.com/golang/glog"
"github.com/youtube/vitess/go/vt/dbconfigs"
Expand Down Expand Up @@ -52,17 +51,9 @@ func main() {
dbconfigs.FilteredConfig | dbconfigs.ReplConfig
dbconfigs.RegisterFlags(dbconfigFlags)
mysqlctl.RegisterFlags()
flag.Parse()

if *servenv.Version {
servenv.AppVersion.Print()
os.Exit(0)
}
servenv.ParseFlags("vttablet")

if len(flag.Args()) > 0 {
flag.Usage()
log.Exit("vttablet doesn't take any positional arguments")
}
if err := tabletenv.VerifyConfig(); err != nil {
log.Exitf("invalid config: %v", err)
}
Expand Down
35 changes: 35 additions & 0 deletions go/vt/servenv/servenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"net/url"
"os"
"runtime"
"strings"
"sync"
"syscall"
"time"
Expand Down Expand Up @@ -193,3 +194,37 @@ func RegisterDefaultFlags() {
func RunDefault() {
Run(*Port)
}

// ParseFlags initializes flags and handles the common case when no positional
// arguments are expected.
func ParseFlags(cmd string) {
flag.Parse()

if *Version {
AppVersion.Print()
os.Exit(0)
}

args := flag.Args()
if len(args) > 0 {
flag.Usage()
log.Exitf("%s doesn't take any positional arguments, got '%s'", cmd, strings.Join(args, " "))
}
}

// ParseFlagsWithArgs initializes flags and returns the positional arguments
func ParseFlagsWithArgs(cmd string) []string {
flag.Parse()

if *Version {
AppVersion.Print()
os.Exit(0)
}

args := flag.Args()
if len(args) == 0 {
log.Exitf("%s expected at least one positional argument", cmd)
}

return args
}