2014-07-01 19:02:13 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-09-04 00:26:49 +02:00
|
|
|
"log"
|
2017-01-19 05:50:45 +01:00
|
|
|
"os"
|
2014-07-01 19:02:13 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2014-07-03 22:12:45 +02:00
|
|
|
"github.com/mitchellh/cli"
|
2014-07-01 19:02:13 +02:00
|
|
|
)
|
|
|
|
|
2014-09-29 20:24:16 +02:00
|
|
|
// Set to true when we're testing
|
|
|
|
var test bool = false
|
|
|
|
|
2015-02-22 01:04:32 +01:00
|
|
|
// DefaultDataDir is the default directory for storing local data.
|
|
|
|
const DefaultDataDir = ".terraform"
|
|
|
|
|
2014-07-12 06:03:56 +02:00
|
|
|
// DefaultStateFilename is the default filename used for the state file.
|
|
|
|
const DefaultStateFilename = "terraform.tfstate"
|
|
|
|
|
2014-08-05 18:32:01 +02:00
|
|
|
// DefaultVarsFilename is the default filename used for vars
|
|
|
|
const DefaultVarsFilename = "terraform.tfvars"
|
|
|
|
|
2015-09-11 20:56:20 +02:00
|
|
|
// DefaultBackupExtension is added to the state file to form the path
|
|
|
|
const DefaultBackupExtension = ".backup"
|
2014-07-28 00:09:04 +02:00
|
|
|
|
2015-10-05 22:06:08 +02:00
|
|
|
// DefaultParallelism is the limit Terraform places on total parallel
|
|
|
|
// operations as it walks the dependency graph.
|
|
|
|
const DefaultParallelism = 10
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
// ErrUnsupportedLocalOp is the common error message shown for operations
|
|
|
|
// that require a backend.Local.
|
|
|
|
const ErrUnsupportedLocalOp = `The configured backend doesn't support this operation.
|
|
|
|
|
|
|
|
The "backend" in Terraform defines how Terraform operates. The default
|
|
|
|
backend performs all operations locally on your machine. Your configuration
|
|
|
|
is configured to use a non-local backend. This backend doesn't support this
|
|
|
|
operation.
|
|
|
|
|
|
|
|
If you want to use the state from the backend but force all other data
|
|
|
|
(configuration, variables, etc.) to come locally, you can force local
|
|
|
|
behavior with the "-local" flag.
|
|
|
|
`
|
|
|
|
|
|
|
|
// ModulePath returns the path to the root module from the CLI args.
|
|
|
|
//
|
|
|
|
// This centralizes the logic for any commands that expect a module path
|
|
|
|
// on their CLI args. This will verify that only one argument is given
|
|
|
|
// and that it is a path to configuration.
|
|
|
|
//
|
|
|
|
// If your command accepts more than one arg, then change the slice bounds
|
|
|
|
// to pass validation.
|
|
|
|
func ModulePath(args []string) (string, error) {
|
|
|
|
// TODO: test
|
|
|
|
|
|
|
|
if len(args) > 1 {
|
|
|
|
return "", fmt.Errorf("Too many command line arguments. Configuration path expected.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
path, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error getting pwd: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return args[0], nil
|
|
|
|
}
|
|
|
|
|
2014-07-03 22:12:45 +02:00
|
|
|
func validateContext(ctx *terraform.Context, ui cli.Ui) bool {
|
2016-09-04 00:26:49 +02:00
|
|
|
log.Println("[INFO] Validating the context...")
|
|
|
|
ws, es := ctx.Validate()
|
|
|
|
log.Printf("[INFO] Validation result: %d warnings, %d errors", len(ws), len(es))
|
|
|
|
|
|
|
|
if len(ws) > 0 || len(es) > 0 {
|
2014-07-03 22:12:45 +02:00
|
|
|
ui.Output(
|
|
|
|
"There are warnings and/or errors related to your configuration. Please\n" +
|
|
|
|
"fix these before continuing.\n")
|
|
|
|
|
|
|
|
if len(ws) > 0 {
|
2015-03-05 21:22:34 +01:00
|
|
|
ui.Warn("Warnings:\n")
|
2014-07-03 22:12:45 +02:00
|
|
|
for _, w := range ws {
|
2015-03-05 21:22:34 +01:00
|
|
|
ui.Warn(fmt.Sprintf(" * %s", w))
|
2014-07-03 22:12:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(es) > 0 {
|
|
|
|
ui.Output("")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(es) > 0 {
|
2015-03-05 21:22:34 +01:00
|
|
|
ui.Error("Errors:\n")
|
2014-07-03 22:12:45 +02:00
|
|
|
for _, e := range es {
|
2015-03-05 21:22:34 +01:00
|
|
|
ui.Error(fmt.Sprintf(" * %s", e))
|
2014-07-03 22:12:45 +02:00
|
|
|
}
|
2015-03-05 21:22:34 +01:00
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
ui.Warn(fmt.Sprintf("\n"+
|
|
|
|
"No errors found. Continuing with %d warning(s).\n", len(ws)))
|
|
|
|
return true
|
2014-07-03 22:12:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|