github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/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 // Only one level of command is stripped off the prefix of a.Completed 23 // here, so nested subcommands like "workspace new" will need to provide 24 // dummy entries (e.g. complete.PredictNothing) as placeholders for 25 // all but the first subcommand. For example, "workspace new" needs 26 // one placeholder for the argument "new". 27 idx := len(a.Completed) 28 if idx >= len(s) { 29 return nil 30 } 31 32 return s[idx].Predict(a) 33 } 34 35 func (m *Meta) completePredictWorkspaceName() complete.Predictor { 36 return complete.PredictFunc(func(a complete.Args) []string { 37 // There are lot of things that can fail in here, so if we encounter 38 // any error then we'll just return nothing and not support autocomplete 39 // until whatever error is fixed. (The user can't actually see the error 40 // here, but other commands should produce a user-visible error before 41 // too long.) 42 43 // We assume here that we want to autocomplete for the current working 44 // directory, since we don't have enough context to know where to 45 // find any config path argument, and it might be _after_ the argument 46 // we're trying to complete here anyway. 47 configPath, err := ModulePath(nil) 48 if err != nil { 49 return nil 50 } 51 52 cfg, err := m.Config(configPath) 53 if err != nil { 54 return nil 55 } 56 57 b, err := m.Backend(&BackendOpts{ 58 Config: cfg, 59 }) 60 if err != nil { 61 return nil 62 } 63 64 names, _ := b.States() 65 return names 66 }) 67 }