/
upgrade013.go
71 lines (55 loc) · 1.58 KB
/
upgrade013.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfexec
import (
"context"
"fmt"
"os/exec"
)
type upgrade013Config struct {
dir string
reattachInfo ReattachInfo
}
var defaultUpgrade013Options = upgrade013Config{}
// Upgrade013Option represents options used in the Destroy method.
type Upgrade013Option interface {
configureUpgrade013(*upgrade013Config)
}
func (opt *DirOption) configureUpgrade013(conf *upgrade013Config) {
conf.dir = opt.path
}
func (opt *ReattachOption) configureUpgrade013(conf *upgrade013Config) {
conf.reattachInfo = opt.info
}
// Upgrade013 represents the terraform 0.13upgrade subcommand.
func (tf *Terraform) Upgrade013(ctx context.Context, opts ...Upgrade013Option) error {
cmd, err := tf.upgrade013Cmd(ctx, opts...)
if err != nil {
return err
}
return tf.runTerraformCmd(ctx, cmd)
}
func (tf *Terraform) upgrade013Cmd(ctx context.Context, opts ...Upgrade013Option) (*exec.Cmd, error) {
err := tf.compatible(ctx, tf0_13_0, tf0_14_0)
if err != nil {
return nil, fmt.Errorf("terraform 0.13upgrade is only supported in 0.13 releases: %w", err)
}
c := defaultUpgrade013Options
for _, o := range opts {
o.configureUpgrade013(&c)
}
args := []string{"0.13upgrade", "-no-color", "-yes"}
// 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
}