Skip to content

Backport of Add cli autocomplete into release/0.0.x#1514

Closed
hc-github-team-consul-ecosystem wants to merge 4 commits intorelease/0.0.xfrom
backport/add-cli-autocomplete/entirely-refined-pup
Closed

Backport of Add cli autocomplete into release/0.0.x#1514
hc-github-team-consul-ecosystem wants to merge 4 commits intorelease/0.0.xfrom
backport/add-cli-autocomplete/entirely-refined-pup

Conversation

@hc-github-team-consul-ecosystem
Copy link
Copy Markdown
Contributor

Backport

This PR is auto-generated from #1501 to be assessed for backporting due to the inclusion of the label backport/0.0.x.

WARNING automatic cherry-pick of commits failed. Commits will require human attention.

merge conflict error: POST https://api.github.com/repos/hashicorp/consul-k8s/merges: 409 Merge conflict []

The below text is copied from the body of the original PR.


Changes proposed in this PR:

  • see description

How I've tested this PR:

  • ran through auto-completions tabbing through commands
  • unit tests written to make sure that number of flags matches the number of auto-completion suggestions

How I expect reviewers to test this PR:

  • run consul-k8s, activate autocomplete, and try it out

Checklist:

  • Tests added
  • CHANGELOG entry added

    HashiCorp engineers only, community PRs should not add a changelog entry.
    Entries should use present tense (e.g. Add support for...)

Description

  • Added tab autocompletion to the consul-k8s CLI. Commands are automatically supported, but flags require implementing the completion interface per command

example for consul-k8s install:

// AutocompleteFlags returns a mapping of supported flags and autocomplete
// options for this command. The map key for the Flags map should be the
// complete flag such as "-foo" or "--foo".
func (c *Command) AutocompleteFlags() complete.Flags {
	return complete.Flags{
		fmt.Sprintf("-%s", flagNamePreset):          complete.PredictNothing,
		fmt.Sprintf("-%s", flagNameNamespace):       complete.PredictNothing,
		fmt.Sprintf("-%s", flagNameDryRun):          complete.PredictNothing,
		fmt.Sprintf("-%s", flagNameAutoApprove):     complete.PredictNothing,
		fmt.Sprintf("-%s", flagNameConfigFile):      complete.PredictFiles("*"),
		fmt.Sprintf("-%s", flagNameSetStringValues): complete.PredictNothing,
		fmt.Sprintf("-%s", flagNameSetValues):       complete.PredictNothing,
		fmt.Sprintf("-%s", flagNameFileValues):      complete.PredictFiles("*"),
		fmt.Sprintf("-%s", flagNameTimeout):         complete.PredictNothing,
		fmt.Sprintf("-%s", flagNameVerbose):         complete.PredictNothing,
		fmt.Sprintf("-%s", flagNameWait):            complete.PredictNothing,
		fmt.Sprintf("-%s", flagNameContext):         complete.PredictNothing,
		fmt.Sprintf("-%s", flagNameKubeconfig):      complete.PredictNothing,
	}
}

// AutocompleteArgs returns the argument predictor for this command.
// Since argument completion is not supported, this will return
// complete.PredictNothing.
func (c *Command) AutocompleteArgs() complete.Predictor {
	return complete.PredictNothing
}
  • complete.PredictNothing instructs that no completion should be offered
  • complete.PredictFiles("*") instructs that completion should suggest any file in the running directory

Usage

Enable autocompletion by running:

$ consul-k8s -autocomplete-install

You need to open a new console for this to take effect.

Disable autocompletion by running:

$ consul-k8s -autocomplete-install

You need to open a new console for this to take effect.

When you start typing a consul-k8s command, press the <tab> character to show a list of available completions. Type -<tab> to show available flag completions.

Example command complete

image

Example flag complete

image

Shell Support

Autocompletion is supported via the posener/complete library which is included in the mitchell/cli library. This library supports bash, zsh and fish.

Future Support

Right now we only support basic completions (commands, flags, or files) add more complex completions for example:

  • parse flags and make pod suggestions i.e. consul-k8s proxy read -n consul <tab> will provide a list of pods in the consul namespace
  • provide set value suggestions based on those in the helm values file

Overview of commits

@hc-github-team-consul-ecosystem hc-github-team-consul-ecosystem force-pushed the backport/add-cli-autocomplete/entirely-refined-pup branch from 722c768 to 21fdaff Compare September 16, 2022 19:30
@github-actions github-actions bot deleted the backport/add-cli-autocomplete/entirely-refined-pup branch September 1, 2025 06:07
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.

2 participants