/
output.go
63 lines (49 loc) · 1.47 KB
/
output.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
package tfexec
import (
"context"
"encoding/json"
"os/exec"
)
type outputConfig struct {
state string
json bool
}
var defaultOutputOptions = outputConfig{}
// OutputOption represents options used in the Output method.
type OutputOption interface {
configureOutput(*outputConfig)
}
func (opt *StateOption) configureOutput(conf *outputConfig) {
conf.state = opt.path
}
// OutputMeta represents the JSON output of 'terraform output -json',
// which resembles state format version 3 due to a historical accident.
// Please see hashicorp/terraform/command/output.go.
// TODO KEM: Should this type be in terraform-json?
type OutputMeta struct {
Sensitive bool `json:"sensitive"`
Type json.RawMessage `json:"type"`
Value json.RawMessage `json:"value"`
}
// Output represents the terraform output subcommand.
func (tf *Terraform) Output(ctx context.Context, opts ...OutputOption) (map[string]OutputMeta, error) {
outputCmd := tf.outputCmd(ctx, opts...)
outputs := map[string]OutputMeta{}
err := tf.runTerraformCmdJSON(ctx, outputCmd, &outputs)
if err != nil {
return nil, err
}
return outputs, nil
}
func (tf *Terraform) outputCmd(ctx context.Context, opts ...OutputOption) *exec.Cmd {
c := defaultOutputOptions
for _, o := range opts {
o.configureOutput(&c)
}
args := []string{"output", "-no-color", "-json"}
// string opts: only pass if set
if c.state != "" {
args = append(args, "-state="+c.state)
}
return tf.buildTerraformCmd(ctx, nil, args...)
}