execParseJSON must only be called while the lock is held.
(ctx, killCtx context.Context, args, env []string, v interface{})
| 119 | |
| 120 | // execParseJSON must only be called while the lock is held. |
| 121 | func (e *executor) execParseJSON(ctx, killCtx context.Context, args, env []string, v interface{}) error { |
| 122 | ctx, span := e.server.startTrace(ctx, fmt.Sprintf("exec - terraform %s", args[0])) |
| 123 | defer span.End() |
| 124 | span.SetAttributes(attribute.StringSlice("args", args)) |
| 125 | |
| 126 | if ctx.Err() != nil { |
| 127 | return ctx.Err() |
| 128 | } |
| 129 | |
| 130 | // #nosec |
| 131 | cmd := exec.CommandContext(killCtx, e.binaryPath, args...) |
| 132 | cmd.Dir = e.files.WorkDirectory() |
| 133 | cmd.Env = env |
| 134 | out := &bytes.Buffer{} |
| 135 | stdErr := &bytes.Buffer{} |
| 136 | cmd.Stdout = out |
| 137 | cmd.Stderr = stdErr |
| 138 | |
| 139 | e.server.logger.Debug(ctx, "executing terraform command with JSON result", |
| 140 | slog.F("binary_path", e.binaryPath), |
| 141 | slog.F("args", args), |
| 142 | ) |
| 143 | err := cmd.Start() |
| 144 | if err != nil { |
| 145 | return err |
| 146 | } |
| 147 | interruptCommandOnCancel(ctx, killCtx, e.logger, cmd) |
| 148 | |
| 149 | err = cmd.Wait() |
| 150 | if err != nil { |
| 151 | errString, _ := io.ReadAll(stdErr) |
| 152 | return xerrors.Errorf("%s: %w", errString, err) |
| 153 | } |
| 154 | |
| 155 | dec := json.NewDecoder(out) |
| 156 | dec.UseNumber() |
| 157 | err = dec.Decode(v) |
| 158 | if err != nil { |
| 159 | return xerrors.Errorf("decode terraform json: %w", err) |
| 160 | } |
| 161 | return nil |
| 162 | } |
| 163 | |
| 164 | func (e *executor) checkMinVersion(ctx context.Context) error { |
| 165 | v, err := e.version(ctx) |
no test coverage detected