-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathinit.go
179 lines (148 loc) · 4.23 KB
/
init.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package tfexec
import (
"context"
"fmt"
"os/exec"
)
type initConfig struct {
backend bool
backendConfig []string
dir string
forceCopy bool
fromModule string
get bool
getPlugins bool
lock bool
lockTimeout string
pluginDir []string
reattachInfo ReattachInfo
reconfigure bool
upgrade bool
verifyPlugins bool
}
var defaultInitOptions = initConfig{
backend: true,
forceCopy: false,
get: true,
getPlugins: true,
lock: true,
lockTimeout: "0s",
reconfigure: false,
upgrade: false,
verifyPlugins: true,
}
// InitOption represents options used in the Init method.
type InitOption interface {
configureInit(*initConfig)
}
func (opt *BackendOption) configureInit(conf *initConfig) {
conf.backend = opt.backend
}
func (opt *BackendConfigOption) configureInit(conf *initConfig) {
conf.backendConfig = append(conf.backendConfig, opt.path)
}
func (opt *DirOption) configureInit(conf *initConfig) {
conf.dir = opt.path
}
func (opt *FromModuleOption) configureInit(conf *initConfig) {
conf.fromModule = opt.source
}
func (opt *GetOption) configureInit(conf *initConfig) {
conf.get = opt.get
}
func (opt *GetPluginsOption) configureInit(conf *initConfig) {
conf.getPlugins = opt.getPlugins
}
func (opt *LockOption) configureInit(conf *initConfig) {
conf.lock = opt.lock
}
func (opt *LockTimeoutOption) configureInit(conf *initConfig) {
conf.lockTimeout = opt.timeout
}
func (opt *PluginDirOption) configureInit(conf *initConfig) {
conf.pluginDir = append(conf.pluginDir, opt.pluginDir)
}
func (opt *ReattachOption) configureInit(conf *initConfig) {
conf.reattachInfo = opt.info
}
func (opt *ReconfigureOption) configureInit(conf *initConfig) {
conf.reconfigure = opt.reconfigure
}
func (opt *UpgradeOption) configureInit(conf *initConfig) {
conf.upgrade = opt.upgrade
}
func (opt *VerifyPluginsOption) configureInit(conf *initConfig) {
conf.verifyPlugins = opt.verifyPlugins
}
// Init represents the terraform init subcommand.
func (tf *Terraform) Init(ctx context.Context, opts ...InitOption) error {
cmd, err := tf.initCmd(ctx, opts...)
if err != nil {
return err
}
return tf.runTerraformCmd(ctx, cmd)
}
func (tf *Terraform) initCmd(ctx context.Context, opts ...InitOption) (*exec.Cmd, error) {
c := defaultInitOptions
for _, o := range opts {
switch o.(type) {
case *LockOption, *LockTimeoutOption, *VerifyPluginsOption, *GetPluginsOption:
err := tf.compatible(ctx, nil, tf0_15_0)
if err != nil {
return nil, fmt.Errorf("-lock, -lock-timeout, -verify-plugins, and -get-plugins options are no longer available as of Terraform 0.15: %w", err)
}
}
o.configureInit(&c)
}
args := []string{"init", "-no-color", "-force-copy", "-input=false"}
// string opts: only pass if set
if c.fromModule != "" {
args = append(args, "-from-module="+c.fromModule)
}
// string opts removed in 0.15: pass if set and <0.15
err := tf.compatible(ctx, nil, tf0_15_0)
if err == nil {
if c.lockTimeout != "" {
args = append(args, "-lock-timeout="+c.lockTimeout)
}
}
// boolean opts: always pass
args = append(args, "-backend="+fmt.Sprint(c.backend))
args = append(args, "-get="+fmt.Sprint(c.get))
args = append(args, "-upgrade="+fmt.Sprint(c.upgrade))
// boolean opts removed in 0.15: pass if <0.15
err = tf.compatible(ctx, nil, tf0_15_0)
if err == nil {
args = append(args, "-lock="+fmt.Sprint(c.lock))
args = append(args, "-get-plugins="+fmt.Sprint(c.getPlugins))
args = append(args, "-verify-plugins="+fmt.Sprint(c.verifyPlugins))
}
// unary flags: pass if true
if c.reconfigure {
args = append(args, "-reconfigure")
}
// string slice opts: split into separate args
if c.backendConfig != nil {
for _, bc := range c.backendConfig {
args = append(args, "-backend-config="+bc)
}
}
if c.pluginDir != nil {
for _, pd := range c.pluginDir {
args = append(args, "-plugin-dir="+pd)
}
}
// optional positional argument
if c.dir != "" {
args = append(args, c.dir)
}
mergeEnv := map[string]string{}
if c.reattachInfo != nil {
reattachStr, err := c.reattachInfo.marshalString()
if err != nil {
return nil, err
}
mergeEnv[reattachEnvVar] = reattachStr
}
return tf.buildTerraformCmd(ctx, mergeEnv, args...), nil
}