Skip to content

Commit

Permalink
Fix servenv of incorrect variable for moveFlags
Browse files Browse the repository at this point in the history
- It looks like cmd.Use was accidenatlly used instead of `cmd.Name()`
- `cmd.Use` is actaully the help text, not the name of the command
- This was not allowing commands to make use of registered/available flags for their use (IE --grpc_...)

Extra:
- Since a lot of these flags are initialized by functional callback, we should not rely on go-lang `init()` as the order can change and be somewhat arbitrary
- Refactored `init()` to `initializedFlags()` and called at runtime for vtclient

Signed-off-by: Kyle Johnson <kylej@hubspot.com>
  • Loading branch information
Kyle Johnson authored and corbantek committed Feb 14, 2025
1 parent 70114ad commit 38e2359
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go/cmd/vtclient/cli/vtclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var (
seqChan = make(chan int, 10)
)

func init() {
func InitializeFlags() {
servenv.MoveFlagsToCobraCommand(Main)

Main.Flags().StringVar(&server, "server", server, "vtgate server to connect to")
Expand Down
1 change: 1 addition & 0 deletions go/cmd/vtclient/vtclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
)

func main() {
cli.InitializeFlags()
if err := cli.Main.Execute(); err != nil {
log.Exit(err)
}
Expand Down
4 changes: 2 additions & 2 deletions go/vt/servenv/servenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func ParseFlagsForTests(cmd string) {
// the given cobra command, then copies over the glog flags that otherwise
// require manual transferring.
func MoveFlagsToCobraCommand(cmd *cobra.Command) {
moveFlags(cmd.Use, cmd.Flags())
moveFlags(cmd.Name(), cmd.Flags())
}

// MovePersistentFlagsToCobraCommand functions exactly like MoveFlagsToCobraCommand,
Expand All @@ -347,7 +347,7 @@ func MoveFlagsToCobraCommand(cmd *cobra.Command) {
// Useful for transferring flags to a parent command whose subcommands should
// inherit the servenv-registered flags.
func MovePersistentFlagsToCobraCommand(cmd *cobra.Command) {
moveFlags(cmd.Use, cmd.PersistentFlags())
moveFlags(cmd.Name(), cmd.PersistentFlags())
}

func moveFlags(name string, fs *pflag.FlagSet) {
Expand Down

0 comments on commit 38e2359

Please sign in to comment.