github.com/gmlexx/terraform@v0.9.6-0.20170514090029-1532b2a67680/command/meta_new.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "os" 6 "strconv" 7 8 "github.com/hashicorp/errwrap" 9 "github.com/hashicorp/terraform/config" 10 "github.com/hashicorp/terraform/config/module" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 // NOTE: Temporary file until this branch is cleaned up. 15 16 // Input returns whether or not input asking is enabled. 17 func (m *Meta) Input() bool { 18 if test || !m.input { 19 return false 20 } 21 22 if envVar := os.Getenv(InputModeEnvVar); envVar != "" { 23 if v, err := strconv.ParseBool(envVar); err == nil && !v { 24 return false 25 } 26 } 27 28 return true 29 } 30 31 // Module loads the module tree for the given root path. 32 // 33 // It expects the modules to already be downloaded. This will never 34 // download any modules. 35 func (m *Meta) Module(path string) (*module.Tree, error) { 36 mod, err := module.NewTreeModule("", path) 37 if err != nil { 38 // Check for the error where we have no config files 39 if errwrap.ContainsType(err, new(config.ErrNoConfigsFound)) { 40 return nil, nil 41 } 42 43 return nil, err 44 } 45 46 err = mod.Load(m.moduleStorage(m.DataDir()), module.GetModeNone) 47 if err != nil { 48 return nil, fmt.Errorf("Error loading modules: %s", err) 49 } 50 51 return mod, nil 52 } 53 54 // Plan returns the plan for the given path. 55 // 56 // This only has an effect if the path itself looks like a plan. 57 // If error is nil and the plan is nil, then the path didn't look like 58 // a plan. 59 // 60 // Error will be non-nil if path looks like a plan and loading the plan 61 // failed. 62 func (m *Meta) Plan(path string) (*terraform.Plan, error) { 63 // Open the path no matter if its a directory or file 64 f, err := os.Open(path) 65 defer f.Close() 66 if err != nil { 67 return nil, fmt.Errorf( 68 "Failed to load Terraform configuration or plan: %s", err) 69 } 70 71 // Stat it so we can check if its a directory 72 fi, err := f.Stat() 73 if err != nil { 74 return nil, fmt.Errorf( 75 "Failed to load Terraform configuration or plan: %s", err) 76 } 77 78 // If this path is a directory, then it can't be a plan. Not an error. 79 if fi.IsDir() { 80 return nil, nil 81 } 82 83 // Read the plan 84 p, err := terraform.ReadPlan(f) 85 if err != nil { 86 return nil, err 87 } 88 89 // We do a validation here that seems odd but if any plan is given, 90 // we must not have set any extra variables. The plan itself contains 91 // the variables and those aren't overwritten. 92 if len(m.variables) > 0 { 93 return nil, fmt.Errorf( 94 "You can't set variables with the '-var' or '-var-file' flag\n" + 95 "when you're applying a plan file. The variables used when\n" + 96 "the plan was created will be used. If you wish to use different\n" + 97 "variable values, create a new plan file.") 98 } 99 100 return p, nil 101 }