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

Fix: Flags reported as falsy despite being set by the user when invoking a connector #234

Merged
merged 3 commits into from
Oct 2, 2024
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
33 changes: 30 additions & 3 deletions pkg/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,16 @@ func MakeMainCommand(
getconnector GetConnectorFunc,
opts ...connectorrunner.Option,
) func(*cobra.Command, []string) error {
return func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
// NOTE(shackra): bind all the flags (persistent and
// regular) with our instance of Viper, doing this
// anywhere else may fail to communicate to Viper the
// values gathered by Cobra.
err := v.BindPFlags(cmd.Flags())
if err != nil {
return err
}

runCtx, err := initLogger(
ctx,
name,
Expand Down Expand Up @@ -176,7 +185,16 @@ func MakeGRPCServerCommand(
confschema field.Configuration,
getconnector GetConnectorFunc,
) func(*cobra.Command, []string) error {
return func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
// NOTE(shackra): bind all the flags (persistent and
// regular) with our instance of Viper, doing this
// anywhere else may fail to communicate to Viper the
// values gathered by Cobra.
err := v.BindPFlags(cmd.Flags())
if err != nil {
return err
}

runCtx, err := initLogger(
ctx,
name,
Expand Down Expand Up @@ -281,7 +299,16 @@ func MakeCapabilitiesCommand(
confschema field.Configuration,
getconnector GetConnectorFunc,
) func(*cobra.Command, []string) error {
return func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
// NOTE(shackra): bind all the flags (persistent and
// regular) with our instance of Viper, doing this
// anywhere else may fail to communicate to Viper the
// values gathered by Cobra.
err := v.BindPFlags(cmd.Flags())
if err != nil {
return err
}

runCtx, err := initLogger(
ctx,
name,
Expand Down
24 changes: 3 additions & 21 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,48 +88,30 @@ func DefineConfiguration(

mainCMD.AddCommand(cli.AdditionalCommands(connectorName, schema.Fields)...)

if err := v.BindPFlags(mainCMD.PersistentFlags()); err != nil {
return nil, nil, err
}
if err := v.BindPFlags(mainCMD.Flags()); err != nil {
return nil, nil, err
}
// NOTE(shackra): Set all values from Viper to the flags so
// that Cobra won't complain that a flag is missing in case we
// pass values through environment variables

// main subcommand
mainCMD.Flags().VisitAll(func(f *pflag.Flag) {
if v.IsSet(f.Name) {
_ = mainCMD.Flags().Set(f.Name, v.GetString(f.Name))
}
})
mainCMD.PersistentFlags().VisitAll(func(f *pflag.Flag) {
if v.IsSet(f.Name) {
_ = mainCMD.PersistentFlags().Set(f.Name, v.GetString(f.Name))
}
})

// children process subcommand
grpcServerCmd.Flags().VisitAll(func(f *pflag.Flag) {
if v.IsSet(f.Name) {
_ = grpcServerCmd.Flags().Set(f.Name, v.GetString(f.Name))
}
})
grpcServerCmd.PersistentFlags().VisitAll(func(f *pflag.Flag) {
if v.IsSet(f.Name) {
_ = grpcServerCmd.PersistentFlags().Set(f.Name, v.GetString(f.Name))
}
})

// capabilities subcommand
capabilitiesCmd.Flags().VisitAll(func(f *pflag.Flag) {
if v.IsSet(f.Name) {
_ = capabilitiesCmd.Flags().Set(f.Name, v.GetString(f.Name))
}
})
capabilitiesCmd.PersistentFlags().VisitAll(func(f *pflag.Flag) {
if v.IsSet(f.Name) {
_ = capabilitiesCmd.PersistentFlags().Set(f.Name, v.GetString(f.Name))
}
})

return v, mainCMD, nil
}
Expand Down
Loading