Skip to content

Commit 04d88b0

Browse files
committed
config: move things out into more files
1 parent c1dea5e commit 04d88b0

2 files changed

Lines changed: 75 additions & 74 deletions

File tree

‎config/import_tree.go‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// configurable is an interface that must be implemented by any configuration
8+
// formats of Terraform in order to return a *Config.
9+
type configurable interface {
10+
Config() (*Config, error)
11+
}
12+
13+
// importTree is the result of the first-pass load of the configuration
14+
// files. It is a tree of raw configurables and then any children (their
15+
// imports).
16+
//
17+
// An importTree can be turned into a configTree.
18+
type importTree struct {
19+
Path string
20+
Raw configurable
21+
Children []*importTree
22+
}
23+
24+
// This is the function type that must be implemented by the configuration
25+
// file loader to turn a single file into a configurable and any additional
26+
// imports.
27+
type fileLoaderFunc func(path string) (configurable, []string, error)
28+
29+
// loadTree takes a single file and loads the entire importTree for that
30+
// file. This function detects what kind of configuration file it is an
31+
// executes the proper fileLoaderFunc.
32+
func loadTree(root string) (*importTree, error) {
33+
c, imps, err := loadFileLibucl(root)
34+
if err != nil {
35+
return nil, err
36+
}
37+
38+
children := make([]*importTree, len(imps))
39+
for i, imp := range imps {
40+
t, err := loadTree(imp)
41+
if err != nil {
42+
return nil, err
43+
}
44+
45+
children[i] = t
46+
}
47+
48+
return &importTree{
49+
Path: root,
50+
Raw: c,
51+
Children: children,
52+
}, nil
53+
}
54+
55+
// ConfigTree traverses the importTree and turns each node into a *Config
56+
// object, ultimately returning a *configTree.
57+
func (t *importTree) ConfigTree() (*configTree, error) {
58+
config, err := t.Raw.Config()
59+
if err != nil {
60+
return nil, fmt.Errorf(
61+
"Error loading %s: %s",
62+
t.Path,
63+
err)
64+
}
65+
66+
// Build our result
67+
result := &configTree{
68+
Path: t.Path,
69+
Config: config,
70+
}
71+
72+
// TODO: Follow children and load them
73+
74+
return result, nil
75+
}

‎config/loader.go‎

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package 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

Comments
 (0)