This way, we can also return the usage to the vtworker RPC client.
Do not exit (or panic) when flags.parse() returned an error unless we are run from the command line.
Commands now return the error of the flag parsing.
Commands now show the usage if an optional parameter is missing.
Unlike os.Exit(), exit.Return() supports to run all deferred functions from the *same* goroutine.
Please note that this doesn't have any effect in vtworker and did not in the past: While the main goroutine has several defer calls to close opened resources, they won't be triggered because the vtworker command is executed in a different go routine. (The main go routine is blocked by servenv.RunDefault())
I moved all code relevant for command parsing and running a command from
"cmd/vtworker" to "vt/worker" because I ran into an import cycle with
the gRPC interface:
cmd/vtworker [plugin_grpcvtworkerserver.go]
=> imports vt/worker/grpcvtworkerserver [server.go]
=> imports cmd/vtworker [command.go]
The only alternative would have been to move the gRPC vtworker server
into the cmd/vtworker package as well?
vtctld does not have the same problem because all its command line
parsing code is already below vt/vtctl and not cmd/vtctl.
As part of the change, I've moved the global variables of vtworker.go
into a new struct "WorkerInstance" which encapsulates the complete
execution state of vtworker. If you prefer, we can also move this into a
separate package.
(Note: In instance.go, setAndStartWorker() and InstallSignalHandlers()
were moved from vtworker.go)
This is so we can have background operations that log,
and keep the logs. We can then display them on a web page
or on the console depending on the usage.
(this is part of vtctl refactor).
lameduckMode flag is not local (not used anywhere else).
Allowing default flags and non-default flags to be used.
Always using default flags in our applications.