From e4f18c3f4db40e19a3d6e28433651698e88e65fe Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Mon, 25 Sep 2017 18:59:29 -0700 Subject: [PATCH] command: multi-argument autocomplete helper The predictors built in to the "complete" package assume that the same type of argument is repeated indefinitely, but most Terraform commands don't work like that, so this helper allows us to define a sequence of predictors that apply to each argument in turn. --- command/autocomplete.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/command/autocomplete.go b/command/autocomplete.go index 15099fa52..b18e80465 100644 --- a/command/autocomplete.go +++ b/command/autocomplete.go @@ -15,3 +15,19 @@ var completePredictBoolean = complete.PredictSet("true", "false") // We don't currently have a real predictor for module sources, but // we'll probably add one later. var completePredictModuleSource = complete.PredictAnything + +type completePredictSequence []complete.Predictor + +func (s completePredictSequence) Predict(a complete.Args) []string { + // Only one level of command is stripped off the prefix of a.Completed + // here, so nested subcommands like "workspace new" will need to provide + // dummy entries (e.g. complete.PredictNothing) as placeholders for + // all but the first subcommand. For example, "workspace new" needs + // one placeholder for the argument "new". + idx := len(a.Completed) + if idx >= len(s) { + return nil + } + + return s[idx].Predict(a) +}