Skip to content

Commit 094c4d3

Browse files
committed
fix(lint): use exec.CommandContext instead of exec.Command
Replace all exec.Command calls with exec.CommandContext to satisfy the noctx linter rule. This enables proper cancellation and prevents resource leaks. Signed-off-by: Alper Rifat Ulucinar <ulucinar@users.noreply.github.com>
1 parent 0481151 commit 094c4d3

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

internal/opentofu/opentofu.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ var rwmutex = &sync.RWMutex{}
174174
// Init initializes a tofu configuration.
175175
func (h Harness) Init(ctx context.Context, o ...InitOption) error {
176176
args := append([]string{"init", "-input=false", "-no-color"}, InitArgsToString(o)...)
177-
cmd := exec.Command(h.Path, args...) //nolint:gosec
177+
cmd := exec.CommandContext(ctx, h.Path, args...) //nolint:gosec
178178
cmd.Dir = h.Dir
179179
for _, e := range os.Environ() {
180180
if strings.Contains(e, "TF_PLUGIN_CACHE_DIR") {
@@ -203,7 +203,7 @@ func (h Harness) Init(ctx context.Context, o ...InitOption) error {
203203
// but isn't is deemed invalid. Attempts to initialise an invalid configuration
204204
// will result in errors, which are not available in a machine readable format.
205205
func (h Harness) Validate(ctx context.Context) error {
206-
cmd := exec.Command(h.Path, "validate", "-json") //nolint:gosec
206+
cmd := exec.CommandContext(ctx, h.Path, "validate", "-json") //nolint:gosec
207207
cmd.Dir = h.Dir
208208
if len(h.Envs) > 0 {
209209
cmd.Env = append(os.Environ(), h.Envs...)
@@ -238,7 +238,7 @@ func (h Harness) Validate(ctx context.Context) error {
238238
// Workspace selects the named tofu workspace. The workspace will be
239239
// created if it does not exist.
240240
func (h Harness) Workspace(ctx context.Context, name string) error {
241-
cmd := exec.Command(h.Path, "workspace", "select", "-no-color", name) //nolint:gosec
241+
cmd := exec.CommandContext(ctx, h.Path, "workspace", "select", "-no-color", name) //nolint:gosec
242242
cmd.Dir = h.Dir
243243
if len(h.Envs) > 0 {
244244
cmd.Env = append(os.Environ(), h.Envs...)
@@ -252,7 +252,7 @@ func (h Harness) Workspace(ctx context.Context, name string) error {
252252
// We weren't able to select a workspace. We assume this was because the
253253
// workspace doesn't exist, which causes tofu to return non-zero. This
254254
// is somewhat optimistic, but it shouldn't hurt to try.
255-
cmd = exec.Command(h.Path, "workspace", "new", "-no-color", name) //nolint:gosec
255+
cmd = exec.CommandContext(ctx, h.Path, "workspace", "new", "-no-color", name) //nolint:gosec
256256
cmd.Dir = h.Dir
257257
if len(h.Envs) > 0 {
258258
cmd.Env = append(os.Environ(), h.Envs...)
@@ -269,7 +269,7 @@ func (h Harness) Workspace(ctx context.Context, name string) error {
269269

270270
// DeleteCurrentWorkspace deletes the current tofu workspace if it is not the default.
271271
func (h Harness) DeleteCurrentWorkspace(ctx context.Context) error {
272-
cmd := exec.Command(h.Path, "workspace", "show", "-no-color") //nolint:gosec
272+
cmd := exec.CommandContext(ctx, h.Path, "workspace", "show", "-no-color") //nolint:gosec
273273
cmd.Dir = h.Dir
274274
if len(h.Envs) > 0 {
275275
cmd.Env = append(os.Environ(), h.Envs...)
@@ -289,7 +289,7 @@ func (h Harness) DeleteCurrentWorkspace(ctx context.Context) error {
289289
if err != nil {
290290
return Classify(err)
291291
}
292-
cmd = exec.Command(h.Path, "workspace", "delete", "-no-color", name) //nolint:gosec
292+
cmd = exec.CommandContext(ctx, h.Path, "workspace", "delete", "-no-color", name) //nolint:gosec
293293
cmd.Dir = h.Dir
294294
if len(h.Envs) > 0 {
295295
cmd.Env = append(os.Environ(), h.Envs...)
@@ -312,7 +312,7 @@ func (h Harness) DeleteCurrentWorkspace(ctx context.Context) error {
312312
// GenerateChecksum calculates the md5sum of the workspace (excluding installed providers) to see if opentofu init needs to run
313313
func (h Harness) GenerateChecksum(ctx context.Context) (string, error) {
314314
command := "/usr/bin/find . -path ./.git -prune -o -path ./.opentofu/providers -prune -o -type f -exec /usr/bin/md5sum {} + | LC_ALL=C /usr/bin/sort | /usr/bin/md5sum | /usr/bin/awk '{print $1}'"
315-
cmd := exec.Command("/bin/sh", "-c", command) //nolint:gosec
315+
cmd := exec.CommandContext(ctx, "/bin/sh", "-c", command) //nolint:gosec
316316
cmd.Dir = h.Dir
317317

318318
checksum, err := runCommand(ctx, cmd)
@@ -399,7 +399,7 @@ func (o Output) JSONValue() ([]byte, error) {
399399

400400
// Outputs extracts outputs from Terraform state.
401401
func (h Harness) Outputs(ctx context.Context) ([]Output, error) {
402-
cmd := exec.Command(h.Path, "output", "-json") //nolint:gosec
402+
cmd := exec.CommandContext(ctx, h.Path, "output", "-json") //nolint:gosec
403403
cmd.Dir = h.Dir
404404
if len(h.Envs) > 0 {
405405
cmd.Env = append(os.Environ(), h.Envs...)
@@ -459,7 +459,7 @@ func (h Harness) Outputs(ctx context.Context) ([]Output, error) {
459459

460460
// Resources returns a list of resources in the Terraform state.
461461
func (h Harness) Resources(ctx context.Context) ([]string, error) {
462-
cmd := exec.Command(h.Path, "state", "list") //nolint:gosec
462+
cmd := exec.CommandContext(ctx, h.Path, "state", "list") //nolint:gosec
463463
cmd.Dir = h.Dir
464464
if len(h.Envs) > 0 {
465465
cmd.Env = append(os.Environ(), h.Envs...)
@@ -545,7 +545,7 @@ func (h Harness) Diff(ctx context.Context, o ...Option) (bool, error) {
545545
}
546546

547547
args := append([]string{"plan", "-no-color", "-input=false", "-detailed-exitcode", "-lock=false"}, ao.args...)
548-
cmd := exec.Command(h.Path, args...) //nolint:gosec
548+
cmd := exec.CommandContext(ctx, h.Path, args...) //nolint:gosec
549549
cmd.Dir = h.Dir
550550
if len(h.Envs) > 0 {
551551
cmd.Env = append(os.Environ(), h.Envs...)
@@ -590,7 +590,7 @@ func (h Harness) Apply(ctx context.Context, o ...Option) error {
590590
}
591591

592592
args := append([]string{"apply", "-no-color", "-auto-approve", "-input=false"}, ao.args...)
593-
cmd := exec.Command(h.Path, args...) //nolint:gosec
593+
cmd := exec.CommandContext(ctx, h.Path, args...) //nolint:gosec
594594
cmd.Dir = h.Dir
595595
if len(h.Envs) > 0 {
596596
cmd.Env = append(os.Environ(), h.Envs...)
@@ -635,7 +635,7 @@ func (h Harness) Destroy(ctx context.Context, o ...Option) error {
635635
}
636636

637637
args := append([]string{"destroy", "-no-color", "-auto-approve", "-input=false"}, do.args...)
638-
cmd := exec.Command(h.Path, args...) //nolint:gosec
638+
cmd := exec.CommandContext(ctx, h.Path, args...) //nolint:gosec
639639
cmd.Dir = h.Dir
640640
if len(h.Envs) > 0 {
641641
cmd.Env = append(os.Environ(), h.Envs...)

0 commit comments

Comments
 (0)