aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/terraform/config/import_tree.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/hashicorp/terraform/config/import_tree.go')
-rw-r--r--vendor/github.com/hashicorp/terraform/config/import_tree.go54
1 files changed, 46 insertions, 8 deletions
diff --git a/vendor/github.com/hashicorp/terraform/config/import_tree.go b/vendor/github.com/hashicorp/terraform/config/import_tree.go
index 37ec11a..08cbc77 100644
--- a/vendor/github.com/hashicorp/terraform/config/import_tree.go
+++ b/vendor/github.com/hashicorp/terraform/config/import_tree.go
@@ -1,8 +1,12 @@
1package config 1package config
2 2
3import ( 3import (
4 "bufio"
4 "fmt" 5 "fmt"
5 "io" 6 "io"
7 "os"
8
9 "github.com/hashicorp/errwrap"
6) 10)
7 11
8// configurable is an interface that must be implemented by any configuration 12// configurable is an interface that must be implemented by any configuration
@@ -27,15 +31,52 @@ type importTree struct {
27// imports. 31// imports.
28type fileLoaderFunc func(path string) (configurable, []string, error) 32type fileLoaderFunc func(path string) (configurable, []string, error)
29 33
34// Set this to a non-empty value at link time to enable the HCL2 experiment.
35// This is not currently enabled for release builds.
36//
37// For example:
38// go install -ldflags="-X github.com/hashicorp/terraform/config.enableHCL2Experiment=true" github.com/hashicorp/terraform
39var enableHCL2Experiment = ""
40
30// loadTree takes a single file and loads the entire importTree for that 41// loadTree takes a single file and loads the entire importTree for that
31// file. This function detects what kind of configuration file it is an 42// file. This function detects what kind of configuration file it is an
32// executes the proper fileLoaderFunc. 43// executes the proper fileLoaderFunc.
33func loadTree(root string) (*importTree, error) { 44func loadTree(root string) (*importTree, error) {
34 var f fileLoaderFunc 45 var f fileLoaderFunc
35 switch ext(root) { 46
36 case ".tf", ".tf.json": 47 // HCL2 experiment is currently activated at build time via the linker.
37 f = loadFileHcl 48 // See the comment on this variable for more information.
38 default: 49 if enableHCL2Experiment == "" {
50 // Main-line behavior: always use the original HCL parser
51 switch ext(root) {
52 case ".tf", ".tf.json":
53 f = loadFileHcl
54 default:
55 }
56 } else {
57 // Experimental behavior: use the HCL2 parser if the opt-in comment
58 // is present.
59 switch ext(root) {
60 case ".tf":
61 // We need to sniff the file for the opt-in comment line to decide
62 // if the file is participating in the HCL2 experiment.
63 cf, err := os.Open(root)
64 if err != nil {
65 return nil, err
66 }
67 sc := bufio.NewScanner(cf)
68 for sc.Scan() {
69 if sc.Text() == "#terraform:hcl2" {
70 f = globalHCL2Loader.loadFile
71 }
72 }
73 if f == nil {
74 f = loadFileHcl
75 }
76 case ".tf.json":
77 f = loadFileHcl
78 default:
79 }
39 } 80 }
40 81
41 if f == nil { 82 if f == nil {
@@ -86,10 +127,7 @@ func (t *importTree) Close() error {
86func (t *importTree) ConfigTree() (*configTree, error) { 127func (t *importTree) ConfigTree() (*configTree, error) {
87 config, err := t.Raw.Config() 128 config, err := t.Raw.Config()
88 if err != nil { 129 if err != nil {
89 return nil, fmt.Errorf( 130 return nil, errwrap.Wrapf(fmt.Sprintf("Error loading %s: {{err}}", t.Path), err)
90 "Error loading %s: %s",
91 t.Path,
92 err)
93 } 131 }
94 132
95 // Build our result 133 // Build our result