github.com/magodo/terraform@v0.11.12-beta1/command/command.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "log" 6 "os" 7 "runtime" 8 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 // Set to true when we're testing 13 var test bool = false 14 15 // DefaultDataDir is the default directory for storing local data. 16 const DefaultDataDir = ".terraform" 17 18 // PluginPathFile is the name of the file in the data dir which stores the list 19 // of directories supplied by the user with the `-plugin-dir` flag during init. 20 const PluginPathFile = "plugin_path" 21 22 // pluginMachineName is the directory name used in new plugin paths. 23 const pluginMachineName = runtime.GOOS + "_" + runtime.GOARCH 24 25 // DefaultPluginVendorDir is the location in the config directory to look for 26 // user-added plugin binaries. Terraform only reads from this path if it 27 // exists, it is never created by terraform. 28 const DefaultPluginVendorDir = "terraform.d/plugins/" + pluginMachineName 29 30 // DefaultStateFilename is the default filename used for the state file. 31 const DefaultStateFilename = "terraform.tfstate" 32 33 // DefaultVarsFilename is the default filename used for vars 34 const DefaultVarsFilename = "terraform.tfvars" 35 36 // DefaultBackupExtension is added to the state file to form the path 37 const DefaultBackupExtension = ".backup" 38 39 // DefaultParallelism is the limit Terraform places on total parallel 40 // operations as it walks the dependency graph. 41 const DefaultParallelism = 10 42 43 // ErrUnsupportedLocalOp is the common error message shown for operations 44 // that require a backend.Local. 45 const ErrUnsupportedLocalOp = `The configured backend doesn't support this operation. 46 47 The "backend" in Terraform defines how Terraform operates. The default 48 backend performs all operations locally on your machine. Your configuration 49 is configured to use a non-local backend. This backend doesn't support this 50 operation. 51 ` 52 53 // ModulePath returns the path to the root module from the CLI args. 54 // 55 // This centralizes the logic for any commands that expect a module path 56 // on their CLI args. This will verify that only one argument is given 57 // and that it is a path to configuration. 58 // 59 // If your command accepts more than one arg, then change the slice bounds 60 // to pass validation. 61 func ModulePath(args []string) (string, error) { 62 // TODO: test 63 64 if len(args) > 1 { 65 return "", fmt.Errorf("Too many command line arguments. Configuration path expected.") 66 } 67 68 if len(args) == 0 { 69 path, err := os.Getwd() 70 if err != nil { 71 return "", fmt.Errorf("Error getting pwd: %s", err) 72 } 73 74 return path, nil 75 } 76 77 return args[0], nil 78 } 79 80 func (m *Meta) validateContext(ctx *terraform.Context) bool { 81 log.Println("[INFO] Validating the context...") 82 diags := ctx.Validate() 83 log.Printf("[INFO] Validation result: %d diagnostics", len(diags)) 84 85 if len(diags) > 0 { 86 m.Ui.Output( 87 "There are warnings and/or errors related to your configuration. Please\n" + 88 "fix these before continuing.\n") 89 90 m.showDiagnostics(diags) 91 } 92 93 return !diags.HasErrors() 94 }