Skip to content

feat(completion): static completion for suitable flags#686

Merged
craicoverflow merged 4 commits intomainfrom
static_flag_comps
May 25, 2021
Merged

feat(completion): static completion for suitable flags#686
craicoverflow merged 4 commits intomainfrom
static_flag_comps

Conversation

@rkpattnaik780
Copy link
Contributor

Adds completions for flags such as --output and --file-format.

Closes #682

Verification Steps

  1. Press tab after typing in the following command:
./rhoas kafka consumergroup describe -o
  1. It should suggest the valid options for the output flag - json, yaml and yml

Screenshot from 2021-05-24 16-51-01

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation change
  • Other (please specify)

Checklist

  • Documentation added for the feature
  • CI and all relevant tests are passing
  • Code Review completed
  • Verified independently by reviewer

Copy link
Contributor

@craicoverflow craicoverflow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified as working but would like to see deduplication. Re-request my review when that is done! :)

return cmdutil.FilterValidConsumerGroupIDs(f, toComplete)
})

_ = cmd.RegisterFlagCompletionFunc("output", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work but this is being duplicated in every file and they are the same. Could you extract this into the flagutil package?

cmd.Flags().StringVar(&opts.fileFormat, "file-format", "", opts.localizer.MustLocalize("serviceAccount.common.flag.fileFormat.description"))
cmd.Flags().BoolVarP(&opts.force, "yes", "y", false, opts.localizer.MustLocalize("serviceAccount.resetCredentials.flag.yes.description"))

_ = cmd.RegisterFlagCompletionFunc("file-format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also duplicated. I would say any one with more than one occurrence can be extracted to flagutil.

Copy link
Contributor

@craicoverflow craicoverflow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work 👍🏻

There may be more flags worth doing this for too.

return cmdutil.FilterValidConsumerGroupIDs(f, toComplete)
})

flagutil.EnableStaticFlagCompletion(cmd, "output", flagutil.ValidOutputFormats)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: this could be further simplified by having a specific util for "output". There is still repetition here and the values will likely always be the same for the value flag name and the valid values.

Something like flagutil.RegisterOutputFlagCompletion()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a separate method for --output will indeed reduce repetition. I will suggest to have RegisterOutputFlagCompletion for output flag and RegisterOutputFlagCompletion for other flags, else we'll have to define a new method for every flag that will require static completion. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should define one for every flag, but for the extremly common ones like this it is good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kept the EnableStaticFlagCompletion with a separate method to enable output flag.

@craicoverflow craicoverflow merged commit a83de50 into main May 25, 2021
@craicoverflow craicoverflow deleted the static_flag_comps branch May 25, 2021 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add auto completion for flags

2 participants