github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/command/command.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os"
     7  
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/mitchellh/cli"
    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  // DefaultStateFilename is the default filename used for the state file.
    19  const DefaultStateFilename = "terraform.tfstate"
    20  
    21  // DefaultVarsFilename is the default filename used for vars
    22  const DefaultVarsFilename = "terraform.tfvars"
    23  
    24  // DefaultBackupExtension is added to the state file to form the path
    25  const DefaultBackupExtension = ".backup"
    26  
    27  // DefaultParallelism is the limit Terraform places on total parallel
    28  // operations as it walks the dependency graph.
    29  const DefaultParallelism = 10
    30  
    31  // ErrUnsupportedLocalOp is the common error message shown for operations
    32  // that require a backend.Local.
    33  const ErrUnsupportedLocalOp = `The configured backend doesn't support this operation.
    34  
    35  The "backend" in Terraform defines how Terraform operates. The default
    36  backend performs all operations locally on your machine. Your configuration
    37  is configured to use a non-local backend. This backend doesn't support this
    38  operation.
    39  
    40  If you want to use the state from the backend but force all other data
    41  (configuration, variables, etc.) to come locally, you can force local
    42  behavior with the "-local" flag.
    43  `
    44  
    45  // ModulePath returns the path to the root module from the CLI args.
    46  //
    47  // This centralizes the logic for any commands that expect a module path
    48  // on their CLI args. This will verify that only one argument is given
    49  // and that it is a path to configuration.
    50  //
    51  // If your command accepts more than one arg, then change the slice bounds
    52  // to pass validation.
    53  func ModulePath(args []string) (string, error) {
    54  	// TODO: test
    55  
    56  	if len(args) > 1 {
    57  		return "", fmt.Errorf("Too many command line arguments. Configuration path expected.")
    58  	}
    59  
    60  	if len(args) == 0 {
    61  		path, err := os.Getwd()
    62  		if err != nil {
    63  			return "", fmt.Errorf("Error getting pwd: %s", err)
    64  		}
    65  
    66  		return path, nil
    67  	}
    68  
    69  	return args[0], nil
    70  }
    71  
    72  func validateContext(ctx *terraform.Context, ui cli.Ui) bool {
    73  	log.Println("[INFO] Validating the context...")
    74  	ws, es := ctx.Validate()
    75  	log.Printf("[INFO] Validation result: %d warnings, %d errors", len(ws), len(es))
    76  
    77  	if len(ws) > 0 || len(es) > 0 {
    78  		ui.Output(
    79  			"There are warnings and/or errors related to your configuration. Please\n" +
    80  				"fix these before continuing.\n")
    81  
    82  		if len(ws) > 0 {
    83  			ui.Warn("Warnings:\n")
    84  			for _, w := range ws {
    85  				ui.Warn(fmt.Sprintf("  * %s", w))
    86  			}
    87  
    88  			if len(es) > 0 {
    89  				ui.Output("")
    90  			}
    91  		}
    92  
    93  		if len(es) > 0 {
    94  			ui.Error("Errors:\n")
    95  			for _, e := range es {
    96  				ui.Error(fmt.Sprintf("  * %s", e))
    97  			}
    98  			return false
    99  		} else {
   100  			ui.Warn(fmt.Sprintf("\n"+
   101  				"No errors found. Continuing with %d warning(s).\n", len(ws)))
   102  			return true
   103  		}
   104  	}
   105  
   106  	return true
   107  }