-
Notifications
You must be signed in to change notification settings - Fork 4
/
config_variables.go
130 lines (105 loc) · 3.02 KB
/
config_variables.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
/*
* Driver implementation (Terraform configuration)
* -----------------------------------------------
*/
import (
"fmt"
"os"
"path"
"strings"
"github.com/docker/machine/libmachine/log"
)
func (driver *Driver) getVariablesFileName() (string, error) {
localConfigDir, err := driver.getConfigDir()
if err != nil {
return "", err
}
return path.Join(localConfigDir, "tfvars.json"), nil
}
// Read Terraform variables from tfvars.json (if it exists)
func (driver *Driver) readVariables() error {
variablesFileName, err := driver.getVariablesFileName()
if err != nil {
return err
}
_, err = os.Stat(variablesFileName)
if os.IsNotExist(err) {
return nil
} else if err != nil {
return err
}
log.Debugf("Reading Terraform variables from '%s'...",
variablesFileName,
)
driver.ConfigVariables.Clear()
err = driver.ConfigVariables.Read(variablesFileName)
if err != nil {
return err
}
return nil
}
// Read all additional variables (from the command line, or from a file)
func (driver *Driver) readAdditionalVariables() error {
err := driver.readAdditionalVariablesInline()
if err != nil {
return err
}
return driver.readAdditionalVariablesFile()
}
// Read additional variables passed in on the command-line (--terraform-variable a=b --terraform-variable c=d)
func (driver *Driver) readAdditionalVariablesInline() error {
for _, additionalVariable := range driver.AdditionalVariablesInline {
variableNameAndValue := strings.SplitN(additionalVariable, "=", 2)
if len(variableNameAndValue) != 2 {
return fmt.Errorf("Invalid format for additional variable '%s", additionalVariable)
}
// Don't overwrite existing entries.
_, variableExists := driver.ConfigVariables[variableNameAndValue[0]]
if variableExists {
continue
}
driver.ConfigVariables[variableNameAndValue[0]] = variableNameAndValue[1]
}
return nil
}
// Read Terraform variables from the file passed in on the command-line.
func (driver *Driver) readAdditionalVariablesFile() error {
if driver.AdditionalVariablesFile == "" {
return nil // Nothing to do
}
variablesFileName := driver.AdditionalVariablesFile
if !path.IsAbs(variablesFileName) {
workingDirectory, err := os.Getwd()
if err != nil {
return err
}
variablesFileName = path.Join(workingDirectory, variablesFileName)
}
log.Debugf("Reading additional Terraform variables from '%s'...", variablesFileName)
// This operation is additive (preserves exising variables).
err := driver.ConfigVariables.Read(variablesFileName)
if err != nil {
return fmt.Errorf("Unable to read additional variables from '%s': %s",
variablesFileName,
err.Error(),
)
}
return nil
}
// Write Terraform variables to tfvars.json
func (driver *Driver) writeVariables() error {
variablesFileName, err := driver.getVariablesFileName()
if err != nil {
return err
}
log.Debugf("Writing %d Terraform variables to '%s'...",
len(driver.ConfigVariables),
variablesFileName,
)
err = driver.ConfigVariables.Write(variablesFileName)
if err != nil {
return err
}
return nil
}