forked from hashicorp/terraform-exec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkspace_new.go
83 lines (66 loc) · 2.13 KB
/
workspace_new.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
package tfexec
import (
"context"
"fmt"
"os/exec"
"strconv"
)
type workspaceNewConfig struct {
lock bool
lockTimeout string
copyState string
}
var defaultWorkspaceNewOptions = workspaceNewConfig{
lock: true,
lockTimeout: "0s",
}
// WorkspaceNewCmdOption represents options that are applicable to the WorkspaceNew method.
type WorkspaceNewCmdOption interface {
configureWorkspaceNew(*workspaceNewConfig)
}
func (opt *LockOption) configureWorkspaceNew(conf *workspaceNewConfig) {
conf.lock = opt.lock
}
func (opt *LockTimeoutOption) configureWorkspaceNew(conf *workspaceNewConfig) {
conf.lockTimeout = opt.timeout
}
func (opt *CopyStateOption) configureWorkspaceNew(conf *workspaceNewConfig) {
conf.copyState = opt.path
}
// WorkspaceNew represents the workspace new subcommand to the Terraform CLI.
func (tf *Terraform) WorkspaceNew(ctx context.Context, workspace string, opts ...WorkspaceNewCmdOption) error {
cmd, err := tf.workspaceNewCmd(ctx, workspace, opts...)
if err != nil {
return err
}
return tf.runTerraformCmd(ctx, cmd)
}
func (tf *Terraform) workspaceNewCmd(ctx context.Context, workspace string, opts ...WorkspaceNewCmdOption) (*exec.Cmd, error) {
// TODO: [DIR] param option
c := defaultWorkspaceNewOptions
for _, o := range opts {
switch o.(type) {
case *LockOption, *LockTimeoutOption:
err := tf.compatible(ctx, tf0_12_0, nil)
if err != nil {
return nil, fmt.Errorf("-lock and -lock-timeout were added to workspace new in Terraform 0.12: %w", err)
}
}
o.configureWorkspaceNew(&c)
}
args := []string{"workspace", "new", "-no-color"}
if c.lockTimeout != "" && c.lockTimeout != defaultWorkspaceNewOptions.lockTimeout {
// only pass if not default, so we don't need to worry about the 0.11 version check
args = append(args, "-lock-timeout="+c.lockTimeout)
}
if !c.lock {
// only pass if false, so we don't need to worry about the 0.11 version check
args = append(args, "-lock="+strconv.FormatBool(c.lock))
}
if c.copyState != "" {
args = append(args, "-state="+c.copyState)
}
args = append(args, workspace)
cmd := tf.buildTerraformCmd(ctx, nil, args...)
return cmd, nil
}