github.com/pulumi/terraform@v1.4.0/pkg/command/autocomplete.go (about)

     1  package command
     2  
     3  import (
     4  	"github.com/posener/complete"
     5  )
     6  
     7  // This file contains some re-usable predictors for auto-complete. The
     8  // command-specific autocomplete configurations live within each command's
     9  // own source file, as AutocompleteArgs and AutocompleteFlags methods on each
    10  // Command implementation.
    11  
    12  // For completing the value of boolean flags like -foo false
    13  var completePredictBoolean = complete.PredictSet("true", "false")
    14  
    15  // We don't currently have a real predictor for module sources, but
    16  // we'll probably add one later.
    17  var completePredictModuleSource = complete.PredictAnything
    18  
    19  type completePredictSequence []complete.Predictor
    20  
    21  func (s completePredictSequence) Predict(a complete.Args) []string {
    22  	// Nested subcommands do not require any placeholder entry for their subcommand name.
    23  	idx := len(a.Completed)
    24  	if idx >= len(s) {
    25  		return nil
    26  	}
    27  
    28  	return s[idx].Predict(a)
    29  }
    30  
    31  func (m *Meta) completePredictWorkspaceName() complete.Predictor {
    32  	return complete.PredictFunc(func(a complete.Args) []string {
    33  		// There are lot of things that can fail in here, so if we encounter
    34  		// any error then we'll just return nothing and not support autocomplete
    35  		// until whatever error is fixed. (The user can't actually see the error
    36  		// here, but other commands should produce a user-visible error before
    37  		// too long.)
    38  
    39  		// We assume here that we want to autocomplete for the current working
    40  		// directory, since we don't have enough context to know where to
    41  		// find any config path argument, and it might be _after_ the argument
    42  		// we're trying to complete here anyway.
    43  		configPath, err := ModulePath(nil)
    44  		if err != nil {
    45  			return nil
    46  		}
    47  
    48  		backendConfig, diags := m.loadBackendConfig(configPath)
    49  		if diags.HasErrors() {
    50  			return nil
    51  		}
    52  
    53  		b, diags := m.Backend(&BackendOpts{
    54  			Config: backendConfig,
    55  		})
    56  		if diags.HasErrors() {
    57  			return nil
    58  		}
    59  
    60  		names, _ := b.Workspaces()
    61  		return names
    62  	})
    63  }