github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/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  If you want to use the state from the backend but force all other data
    53  (configuration, variables, etc.) to come locally, you can force local
    54  behavior with the "-local" flag.
    55  `
    56  
    57  // ModulePath returns the path to the root module from the CLI args.
    58  //
    59  // This centralizes the logic for any commands that expect a module path
    60  // on their CLI args. This will verify that only one argument is given
    61  // and that it is a path to configuration.
    62  //
    63  // If your command accepts more than one arg, then change the slice bounds
    64  // to pass validation.
    65  func ModulePath(args []string) (string, error) {
    66  	// TODO: test
    67  
    68  	if len(args) > 1 {
    69  		return "", fmt.Errorf("Too many command line arguments. Configuration path expected.")
    70  	}
    71  
    72  	if len(args) == 0 {
    73  		path, err := os.Getwd()
    74  		if err != nil {
    75  			return "", fmt.Errorf("Error getting pwd: %s", err)
    76  		}
    77  
    78  		return path, nil
    79  	}
    80  
    81  	return args[0], nil
    82  }
    83  
    84  func (m *Meta) validateContext(ctx *terraform.Context) bool {
    85  	log.Println("[INFO] Validating the context...")
    86  	diags := ctx.Validate()
    87  	log.Printf("[INFO] Validation result: %d diagnostics", len(diags))
    88  
    89  	if len(diags) > 0 {
    90  		m.Ui.Output(
    91  			"There are warnings and/or errors related to your configuration. Please\n" +
    92  				"fix these before continuing.\n")
    93  
    94  		m.showDiagnostics(diags)
    95  	}
    96  
    97  	return !diags.HasErrors()
    98  }