11package config
22
3- import (
4- "fmt"
5- )
6-
73// Load loads the Terraform configuration from a given file.
84//
95// This file can be any format that Terraform recognizes, and import any
@@ -21,73 +17,3 @@ func Load(path string) (*Config, error) {
2117
2218 return configTree .Flatten ()
2319}
24-
25- // configurable is an interface that must be implemented by any configuration
26- // formats of Terraform in order to return a *Config.
27- type configurable interface {
28- Config () (* Config , error )
29- }
30-
31- // importTree is the result of the first-pass load of the configuration
32- // files. It is a tree of raw configurables and then any children (their
33- // imports).
34- //
35- // An importTree can be turned into a configTree.
36- type importTree struct {
37- Path string
38- Raw configurable
39- Children []* importTree
40- }
41-
42- // This is the function type that must be implemented by the configuration
43- // file loader to turn a single file into a configurable and any additional
44- // imports.
45- type fileLoaderFunc func (path string ) (configurable , []string , error )
46-
47- // loadTree takes a single file and loads the entire importTree for that
48- // file. This function detects what kind of configuration file it is an
49- // executes the proper fileLoaderFunc.
50- func loadTree (root string ) (* importTree , error ) {
51- c , imps , err := loadFileLibucl (root )
52- if err != nil {
53- return nil , err
54- }
55-
56- children := make ([]* importTree , len (imps ))
57- for i , imp := range imps {
58- t , err := loadTree (imp )
59- if err != nil {
60- return nil , err
61- }
62-
63- children [i ] = t
64- }
65-
66- return & importTree {
67- Path : root ,
68- Raw : c ,
69- Children : children ,
70- }, nil
71- }
72-
73- // ConfigTree traverses the importTree and turns each node into a *Config
74- // object, ultimately returning a *configTree.
75- func (t * importTree ) ConfigTree () (* configTree , error ) {
76- config , err := t .Raw .Config ()
77- if err != nil {
78- return nil , fmt .Errorf (
79- "Error loading %s: %s" ,
80- t .Path ,
81- err )
82- }
83-
84- // Build our result
85- result := & configTree {
86- Path : t .Path ,
87- Config : config ,
88- }
89-
90- // TODO: Follow children and load them
91-
92- return result , nil
93- }
0 commit comments