github.com/alexaandru/terraform@v0.11.1-0.20171120185746-28632790b723/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  	"github.com/mitchellh/cli"
    11  )
    12  
    13  // Set to true when we're testing
    14  var test bool = false
    15  
    16  // DefaultDataDir is the default directory for storing local data.
    17  const DefaultDataDir = ".terraform"
    18  
    19  // PluginPathFile is the name of the file in the data dir which stores the list
    20  // of directories supplied by the user with the `-plugin-dir` flag during init.
    21  const PluginPathFile = "plugin_path"
    22  
    23  // pluginMachineName is the directory name used in new plugin paths.
    24  const pluginMachineName = runtime.GOOS + "_" + runtime.GOARCH
    25  
    26  // DefaultPluginVendorDir is the location in the config directory to look for
    27  // user-added plugin binaries. Terraform only reads from this path if it
    28  // exists, it is never created by terraform.
    29  const DefaultPluginVendorDir = "terraform.d/plugins/" + pluginMachineName
    30  
    31  // DefaultStateFilename is the default filename used for the state file.
    32  const DefaultStateFilename = "terraform.tfstate"
    33  
    34  // DefaultVarsFilename is the default filename used for vars
    35  const DefaultVarsFilename = "terraform.tfvars"
    36  
    37  // DefaultBackupExtension is added to the state file to form the path
    38  const DefaultBackupExtension = ".backup"
    39  
    40  // DefaultParallelism is the limit Terraform places on total parallel
    41  // operations as it walks the dependency graph.
    42  const DefaultParallelism = 10
    43  
    44  // ErrUnsupportedLocalOp is the common error message shown for operations
    45  // that require a backend.Local.
    46  const ErrUnsupportedLocalOp = `The configured backend doesn't support this operation.
    47  
    48  The "backend" in Terraform defines how Terraform operates. The default
    49  backend performs all operations locally on your machine. Your configuration
    50  is configured to use a non-local backend. This backend doesn't support this
    51  operation.
    52  
    53  If you want to use the state from the backend but force all other data
    54  (configuration, variables, etc.) to come locally, you can force local
    55  behavior with the "-local" flag.
    56  `
    57  
    58  // ModulePath returns the path to the root module from the CLI args.
    59  //
    60  // This centralizes the logic for any commands that expect a module path
    61  // on their CLI args. This will verify that only one argument is given
    62  // and that it is a path to configuration.
    63  //
    64  // If your command accepts more than one arg, then change the slice bounds
    65  // to pass validation.
    66  func ModulePath(args []string) (string, error) {
    67  	// TODO: test
    68  
    69  	if len(args) > 1 {
    70  		return "", fmt.Errorf("Too many command line arguments. Configuration path expected.")
    71  	}
    72  
    73  	if len(args) == 0 {
    74  		path, err := os.Getwd()
    75  		if err != nil {
    76  			return "", fmt.Errorf("Error getting pwd: %s", err)
    77  		}
    78  
    79  		return path, nil
    80  	}
    81  
    82  	return args[0], nil
    83  }
    84  
    85  func validateContext(ctx *terraform.Context, ui cli.Ui) bool {
    86  	log.Println("[INFO] Validating the context...")
    87  	ws, es := ctx.Validate()
    88  	log.Printf("[INFO] Validation result: %d warnings, %d errors", len(ws), len(es))
    89  
    90  	if len(ws) > 0 || len(es) > 0 {
    91  		ui.Output(
    92  			"There are warnings and/or errors related to your configuration. Please\n" +
    93  				"fix these before continuing.\n")
    94  
    95  		if len(ws) > 0 {
    96  			ui.Warn("Warnings:\n")
    97  			for _, w := range ws {
    98  				ui.Warn(fmt.Sprintf("  * %s", w))
    99  			}
   100  
   101  			if len(es) > 0 {
   102  				ui.Output("")
   103  			}
   104  		}
   105  
   106  		if len(es) > 0 {
   107  			ui.Error("Errors:\n")
   108  			for _, e := range es {
   109  				ui.Error(fmt.Sprintf("  * %s", e))
   110  			}
   111  			return false
   112  		} else {
   113  			ui.Warn(fmt.Sprintf("\n"+
   114  				"No errors found. Continuing with %d warning(s).\n", len(ws)))
   115  			return true
   116  		}
   117  	}
   118  
   119  	return true
   120  }