diff --git a/pkg/scheduler/adapters/terraform.go b/pkg/scheduler/adapters/terraform.go index 73e4aa3bd59..7c2fccf8bfc 100644 --- a/pkg/scheduler/adapters/terraform.go +++ b/pkg/scheduler/adapters/terraform.go @@ -1902,7 +1902,7 @@ func (o *terraformOutput) finishNode(node *dependency.Node, result TerraformExec } o.groupMu.Lock() defer o.groupMu.Unlock() - label := terraformNodeLabel(node) + label := ioLayer.MaskString(terraformNodeLabel(node)) status := "succeeded" if execErr != nil { status = "failed" @@ -1913,8 +1913,12 @@ func (o *terraformOutput) finishNode(node *dependency.Node, result TerraformExec command = terraformDefaultCommand } writeGroupedOutputMarker(stderr, "\n["+label+"] "+command+" output "+status+"\n") - replayGroupedOutput(ioLayer.MaskWriter(os.Stdout), result.Stdout) - replayGroupedOutput(stderr, result.Stderr) + stdout := ioLayer.NewLinePrefixWriter(label, ioLayer.MaskWriter(os.Stdout), &o.outputMu) + replayGroupedOutput(ioLayer.MaskWriter(stdout), result.Stdout) + _ = stdout.Flush() + stderrOutput := ioLayer.NewLinePrefixWriter(label, stderr, &o.outputMu) + replayGroupedOutput(ioLayer.MaskWriter(stderrOutput), result.Stderr) + _ = stderrOutput.Flush() writeGroupedOutputMarker(stderr, "["+label+"] end "+command+" output\n") } diff --git a/pkg/scheduler/adapters/terraform_test.go b/pkg/scheduler/adapters/terraform_test.go index d4ecd0b3cb2..5691a78888e 100644 --- a/pkg/scheduler/adapters/terraform_test.go +++ b/pkg/scheduler/adapters/terraform_test.go @@ -18,8 +18,10 @@ import ( "github.com/stretchr/testify/require" errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/asciicast" cfg "github.com/cloudposse/atmos/pkg/config" "github.com/cloudposse/atmos/pkg/dependency" + ioLayer "github.com/cloudposse/atmos/pkg/io" provWorkdir "github.com/cloudposse/atmos/pkg/provisioner/workdir" "github.com/cloudposse/atmos/pkg/scheduler" "github.com/cloudposse/atmos/pkg/schema" @@ -2279,10 +2281,18 @@ func TestTerraformOutputFinishNodeGroupedOutput(t *testing.T) { os.Stderr = oldStderr }() + castPath := filepath.Join(t.TempDir(), "grouped.cast") + recorder, err := asciicast.Start(&asciicast.Options{Path: castPath}) + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, recorder.Discard()) }) + t.Cleanup(ioLayer.SetRecorder(recorder)) + require.NoError(t, ioLayer.RegisterPattern(`\[dev/app\] grouped-prefix-context-secret`)) + output := &terraformOutput{command: "plan", logOrder: terraformLogOrderGrouped} + token := "ghp_" + strings.Repeat("a", 36) output.finishNode(&dependency.Node{Component: "app", Stack: "dev"}, TerraformExecutionResult{ - Stdout: "stdout", - Stderr: "stderr", + Stdout: "stdout\n\n\x1b[32mlast stdout\x1b[0m\n" + token + "\ngrouped-prefix-context-secret", + Stderr: "stderr\rprogress\r\nlast stderr\n" + token + "\ngrouped-prefix-context-secret", }, nil) require.NoError(t, stdoutWriter.Close()) @@ -2292,10 +2302,26 @@ func TestTerraformOutputFinishNodeGroupedOutput(t *testing.T) { stderrBytes, err := io.ReadAll(stderrReader) require.NoError(t, err) - require.Contains(t, string(stdoutBytes), "stdout") - require.Contains(t, string(stderrBytes), "plan output succeeded") - require.Contains(t, string(stderrBytes), "stderr") - require.Contains(t, string(stderrBytes), "end plan output") + require.Equal(t, "[dev/app] stdout\n[dev/app] \n[dev/app] \x1b[32mlast stdout\x1b[0m\n"+ + "[dev/app] \n\n", string(stdoutBytes)) + require.Equal(t, "\n[dev/app] plan output succeeded\n"+ + "[dev/app] stderr\n[dev/app] progress\n[dev/app] last stderr\n[dev/app] \n\n"+ + "[dev/app] end plan output\n", string(stderrBytes)) + + require.NoError(t, recorder.Close()) + castBytes, err := os.ReadFile(castPath) + require.NoError(t, err) + recorded := map[string]string{} + for _, line := range bytes.Split(bytes.TrimSpace(castBytes), []byte("\n"))[1:] { + var event [3]json.RawMessage + require.NoError(t, json.Unmarshal(line, &event)) + var stream, content string + require.NoError(t, json.Unmarshal(event[1], &stream)) + require.NoError(t, json.Unmarshal(event[2], &content)) + recorded[stream] += content + } + require.Equal(t, string(stdoutBytes), recorded["o"]) + require.Equal(t, string(stderrBytes), recorded["e"]) } func TestTerraformOutputFinishNodeSkipsNoChangesWhenHidden(t *testing.T) { diff --git a/website/docs/cli/commands/terraform/terraform-apply.mdx b/website/docs/cli/commands/terraform/terraform-apply.mdx index a47efd1ccaf..98fcc88b932 100644 --- a/website/docs/cli/commands/terraform/terraform-apply.mdx +++ b/website/docs/cli/commands/terraform/terraform-apply.mdx @@ -321,7 +321,7 @@ atmos terraform apply vpc -s dev Supported values: - `stream` *(default)* — print log lines as they arrive, interleaved across components - - `grouped` — buffer each component's output and print it as a contiguous block after the component finishes + - `grouped` — buffer each component's output and print it as a contiguous block after the component finishes, prefixing every stdout and stderr line with `[stack/component]`. Saved per-component log files remain unprefixed. **Environment variable:** `ATMOS_TERRAFORM_APPLY_LOG_ORDER` diff --git a/website/docs/cli/commands/terraform/terraform-deploy.mdx b/website/docs/cli/commands/terraform/terraform-deploy.mdx index 93228fbd524..b7dc3b96cb4 100644 --- a/website/docs/cli/commands/terraform/terraform-deploy.mdx +++ b/website/docs/cli/commands/terraform/terraform-deploy.mdx @@ -270,7 +270,7 @@ atmos terraform deploy test/test-component-override-3 -s tenant1-ue2-dev Supported values: - `stream` *(default)* — print log lines as they arrive, interleaved across components - - `grouped` — buffer each component's output and print it as a contiguous block after the component finishes + - `grouped` — buffer each component's output and print it as a contiguous block after the component finishes, prefixing every stdout and stderr line with `[stack/component]`. Saved per-component log files remain unprefixed. **Environment variable:** `ATMOS_TERRAFORM_DEPLOY_LOG_ORDER` diff --git a/website/docs/cli/commands/terraform/terraform-destroy.mdx b/website/docs/cli/commands/terraform/terraform-destroy.mdx index 3af50174196..81818634de8 100644 --- a/website/docs/cli/commands/terraform/terraform-destroy.mdx +++ b/website/docs/cli/commands/terraform/terraform-destroy.mdx @@ -170,7 +170,7 @@ Keep the previous backend, workspace, inputs, and required sources available for Supported values: - `stream` *(default)* — print log lines as they arrive, interleaved across components - - `grouped` — buffer each component's output and print it as a contiguous block after the component finishes + - `grouped` — buffer each component's output and print it as a contiguous block after the component finishes, prefixing every stdout and stderr line with `[stack/component]`. Saved per-component log files remain unprefixed. **Environment variable:** `ATMOS_TERRAFORM_DESTROY_LOG_ORDER` diff --git a/website/docs/cli/commands/terraform/terraform-init.mdx b/website/docs/cli/commands/terraform/terraform-init.mdx index 5ac33f47dbe..d0df8aa4696 100644 --- a/website/docs/cli/commands/terraform/terraform-init.mdx +++ b/website/docs/cli/commands/terraform/terraform-init.mdx @@ -308,7 +308,7 @@ Atmos will: Supported values: - `stream` *(default)* — print log lines as they arrive, interleaved across components - - `grouped` — buffer each component's output and print it as a contiguous block after the component finishes + - `grouped` — buffer each component's output and print it as a contiguous block after the component finishes, prefixing every stdout and stderr line with `[stack/component]`. Saved per-component log files remain unprefixed. **Environment variable:** `ATMOS_TERRAFORM_INIT_LOG_ORDER` diff --git a/website/docs/cli/commands/terraform/terraform-plan.mdx b/website/docs/cli/commands/terraform/terraform-plan.mdx index f377274d4f8..403a7781f24 100644 --- a/website/docs/cli/commands/terraform/terraform-plan.mdx +++ b/website/docs/cli/commands/terraform/terraform-plan.mdx @@ -115,6 +115,8 @@ atmos terraform plan --all -s dev --max-concurrency 4 Concurrent plan output is isolated per component. Use `--log-order grouped` to print each component's logs together after it finishes, or keep the default `stream` mode to see logs as they arrive. To reduce noise from unchanged components, use `--hide=no-changes`. +Grouped output prefixes every stdout and stderr line with `[stack/component]`, just like stream mode, so the originating component stays visible while scrolling. Saved per-component log files remain unprefixed. + ```shell atmos terraform plan --all -s dev \ --max-concurrency 4 \