|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package tfexec |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "iter" |
| 10 | + "os/exec" |
| 11 | +) |
| 12 | + |
| 13 | +type queryConfig struct { |
| 14 | + dir string |
| 15 | + generateConfig string |
| 16 | + reattachInfo ReattachInfo |
| 17 | + vars []string |
| 18 | + varFiles []string |
| 19 | +} |
| 20 | + |
| 21 | +var defaultQueryOptions = queryConfig{} |
| 22 | + |
| 23 | +// QueryOption represents options used in the Query method. |
| 24 | +type QueryOption interface { |
| 25 | + configureQuery(*queryConfig) |
| 26 | +} |
| 27 | + |
| 28 | +func (opt *DirOption) configureQuery(conf *queryConfig) { |
| 29 | + conf.dir = opt.path |
| 30 | +} |
| 31 | + |
| 32 | +func (opt *GenerateConfigOutOption) configureQuery(conf *queryConfig) { |
| 33 | + conf.generateConfig = opt.path |
| 34 | +} |
| 35 | + |
| 36 | +func (opt *ReattachOption) configureQuery(conf *queryConfig) { |
| 37 | + conf.reattachInfo = opt.info |
| 38 | +} |
| 39 | + |
| 40 | +func (opt *VarFileOption) configureQuery(conf *queryConfig) { |
| 41 | + conf.varFiles = append(conf.varFiles, opt.path) |
| 42 | +} |
| 43 | + |
| 44 | +func (opt *VarOption) configureQuery(conf *queryConfig) { |
| 45 | + conf.vars = append(conf.vars, opt.assignment) |
| 46 | +} |
| 47 | + |
| 48 | +// QueryJSON executes `terraform query` with the specified options as well as the |
| 49 | +// `-json` flag and waits for it to complete. |
| 50 | +// |
| 51 | +// Using the `-json` flag will result in |
| 52 | +// [machine-readable](https://developer.hashicorp.com/terraform/internals/machine-readable-ui) |
| 53 | +// JSON being written to the supplied `io.Writer`. |
| 54 | +// |
| 55 | +// The returned error is nil if `terraform query` has been executed and exits |
| 56 | +// with 0. |
| 57 | +// |
| 58 | +// QueryJSON is likely to be removed in a future major version in favour of |
| 59 | +// query returning JSON by default. |
| 60 | +func (tf *Terraform) QueryJSON(ctx context.Context, opts ...QueryOption) (iter.Seq[NextMessage], error) { |
| 61 | + err := tf.compatible(ctx, tf1_14_0, nil) |
| 62 | + if err != nil { |
| 63 | + return nil, fmt.Errorf("terraform query -json was added in 1.14.0: %w", err) |
| 64 | + } |
| 65 | + |
| 66 | + queryCmd, err := tf.queryJSONCmd(ctx, opts...) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + |
| 71 | + return tf.runTerraformCmdJSONLog(ctx, queryCmd), nil |
| 72 | +} |
| 73 | + |
| 74 | +func (tf *Terraform) queryJSONCmd(ctx context.Context, opts ...QueryOption) (*exec.Cmd, error) { |
| 75 | + c := defaultQueryOptions |
| 76 | + |
| 77 | + for _, o := range opts { |
| 78 | + o.configureQuery(&c) |
| 79 | + } |
| 80 | + |
| 81 | + args, err := tf.buildQueryArgs(ctx, c) |
| 82 | + if err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + |
| 86 | + args = append(args, "-json") |
| 87 | + |
| 88 | + return tf.buildQueryCmd(ctx, c, args) |
| 89 | +} |
| 90 | + |
| 91 | +func (tf *Terraform) buildQueryArgs(ctx context.Context, c queryConfig) ([]string, error) { |
| 92 | + args := []string{"query", "-no-color"} |
| 93 | + |
| 94 | + if c.generateConfig != "" { |
| 95 | + args = append(args, "-generate-config-out="+c.generateConfig) |
| 96 | + } |
| 97 | + |
| 98 | + for _, vf := range c.varFiles { |
| 99 | + args = append(args, "-var-file="+vf) |
| 100 | + } |
| 101 | + |
| 102 | + if c.vars != nil { |
| 103 | + for _, v := range c.vars { |
| 104 | + args = append(args, "-var", v) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return args, nil |
| 109 | +} |
| 110 | + |
| 111 | +func (tf *Terraform) buildQueryCmd(ctx context.Context, c queryConfig, args []string) (*exec.Cmd, error) { |
| 112 | + // optional positional argument |
| 113 | + if c.dir != "" { |
| 114 | + args = append(args, c.dir) |
| 115 | + } |
| 116 | + |
| 117 | + mergeEnv := map[string]string{} |
| 118 | + if c.reattachInfo != nil { |
| 119 | + reattachStr, err := c.reattachInfo.marshalString() |
| 120 | + if err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + mergeEnv[reattachEnvVar] = reattachStr |
| 124 | + } |
| 125 | + |
| 126 | + return tf.buildTerraformCmd(ctx, mergeEnv, args...), nil |
| 127 | +} |
0 commit comments