[cli] [servenv] Migrate miscellaneous flags to `pflag` (#11186)

* [cli] [servenv] Migrate `--socket_file` flag to pflag and un-export

Relates to #11144.

Signed-off-by: Andrew Mason <andrew@planetscale.com>

* [cli] [servenv] Migrate `--pid_file` flag to pflag

Relates to #11144.

Signed-off-by: Andrew Mason <andrew@planetscale.com>

* [cli] [servenv] Correct old flag.Usage call

Relates to #11144.

Signed-off-by: Andrew Mason <andrew@planetscale.com>
This commit is contained in:
Andrew Mason 2022-09-08 06:37:15 -04:00 коммит произвёл GitHub
Родитель af200130cf
Коммит bf0e0c3a6f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 28 добавлений и 20 удалений

Просмотреть файл

@ -117,7 +117,6 @@ Usage of vtexplain:
--normalize Whether to enable vtgate normalization
--normalize_queries Rewrite queries with bind vars. Turn this off if the app itself sends normalized queries with bind vars. (default true)
--output-mode string Output in human-friendly text or json (default "text")
--pid_file string If set, the process will write its pid to the named file, and delete it on graceful shutdown.
--planner-version string Sets the query planner version to use when generating the explain output. Valid values are V3 and Gen4
--planner_version string Deprecated flag. Use planner-version instead
--pool_hostname_resolve_interval duration if set force an update to all hostnames and reconnect if changed, defaults to 0 (disabled) (default 0s)

Просмотреть файл

@ -29,7 +29,6 @@ Usage of vtgr:
--logtostderr log to standard error instead of files
--mysql_server_flush_delay duration Delay after which buffered response will be flushed to the client. (default 100ms)
--mysql_server_version string MySQL server version to advertise.
--pid_file string If set, the process will write its pid to the named file, and delete it on graceful shutdown.
--ping_tablet_timeout duration time to wait when we ping a tablet (default 2s)
--pprof strings enable profiling
--purge_logs_interval duration how often try to remove old logs (default 1h0m0s)

Просмотреть файл

@ -57,7 +57,7 @@ func Parse(fs *flag.FlagSet) {
defer func() {
if help {
flag.Usage()
Usage()
os.Exit(0)
}
}()
@ -67,6 +67,12 @@ func Parse(fs *flag.FlagSet) {
flag.Parse()
}
// Usage invokes the current CommandLine's Usage func, or if not overridden,
// "prints a simple header and calls PrintDefaults".
func Usage() {
flag.Usage()
}
// filterTestFlags returns two slices: the second one has just the flags for `go test` and the first one contains
// the rest of the flags.
const goTestFlagSuffix = "-test"

Просмотреть файл

@ -166,7 +166,7 @@ func isGRPCEnabled() bool {
return true
}
if SocketFile != nil && *SocketFile != "" {
if socketFile != "" {
return true
}

Просмотреть файл

@ -17,27 +17,26 @@ limitations under the License.
package servenv
import (
"flag"
"fmt"
"os"
"vitess.io/vitess/go/vt/log"
)
var pidFile = flag.String("pid_file", "", "If set, the process will write its pid to the named file, and delete it on graceful shutdown.")
var pidFile string // registered in RegisterFlags as --pid_file
func init() {
pidFileCreated := false
// Create pid file after flags are parsed.
OnInit(func() {
if *pidFile == "" {
if pidFile == "" {
return
}
file, err := os.OpenFile(*pidFile, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
file, err := os.OpenFile(pidFile, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
if err != nil {
log.Errorf("Unable to create pid file '%s': %v", *pidFile, err)
log.Errorf("Unable to create pid file '%s': %v", pidFile, err)
return
}
pidFileCreated = true
@ -47,15 +46,15 @@ func init() {
// Remove pid file on graceful shutdown.
OnClose(func() {
if *pidFile == "" {
if pidFile == "" {
return
}
if !pidFileCreated {
return
}
if err := os.Remove(*pidFile); err != nil {
log.Errorf("Unable to remove pid file '%s': %v", *pidFile, err)
if err := os.Remove(pidFile); err != nil {
log.Errorf("Unable to remove pid file '%s': %v", pidFile, err)
}
})
}

Просмотреть файл

@ -29,7 +29,6 @@ limitations under the License.
package servenv
import (
"flag"
"net/url"
"os"
"os/signal"
@ -94,6 +93,9 @@ func RegisterFlags() {
fs.DurationVar(&onTermTimeout, "onterm_timeout", onTermTimeout, "wait no more than this for OnTermSync handlers before stopping")
fs.DurationVar(&onCloseTimeout, "onclose_timeout", onCloseTimeout, "wait no more than this for OnClose handlers before stopping")
fs.BoolVar(&catchSigpipe, "catch-sigpipe", catchSigpipe, "catch and ignore SIGPIPE on stdout and stderr if specified")
// pid_file.go
fs.StringVar(&pidFile, "pid_file", pidFile, "If set, the process will write its pid to the named file, and delete it on graceful shutdown.")
})
}
@ -318,7 +320,7 @@ func ParseFlags(cmd string) {
args := fs.Args()
if len(args) > 0 {
flag.Usage()
_flag.Usage()
log.Exitf("%s doesn't take any positional arguments, got '%s'", cmd, strings.Join(args, " "))
}
}

Просмотреть файл

@ -17,26 +17,27 @@ limitations under the License.
package servenv
import (
"flag"
"net"
"os"
"github.com/spf13/pflag"
"vitess.io/vitess/go/vt/log"
)
var (
// SocketFile has the flag used when calling
// socketFile has the flag used when calling
// RegisterDefaultSocketFileFlags.
SocketFile *string
socketFile string
)
// serveSocketFile listen to the named socket and serves RPCs on it.
func serveSocketFile() {
if SocketFile == nil || *SocketFile == "" {
if socketFile == "" {
log.Infof("Not listening on socket file")
return
}
name := *SocketFile
name := socketFile
// try to delete if file exists
if _, err := os.Stat(name); err == nil {
@ -57,5 +58,7 @@ func serveSocketFile() {
// RegisterDefaultSocketFileFlags registers the default flags for listening
// to a socket. This needs to be called before flags are parsed.
func RegisterDefaultSocketFileFlags() {
SocketFile = flag.String("socket_file", "", "Local unix socket file to listen on")
OnParse(func(fs *pflag.FlagSet) {
fs.StringVar(&socketFile, "socket_file", socketFile, "Local unix socket file to listen on")
})
}