From 4bee545c68bb35ae9a58b62e77068cd3bb2acf4f Mon Sep 17 00:00:00 2001 From: chelkyl <14041823+chelkyl@users.noreply.github.com> Date: Sun, 27 Feb 2022 20:37:19 -0600 Subject: [PATCH 1/9] feat: add --silence-cached-logs CLI option to hide logs of all cached tasks --- cli/internal/run/run.go | 21 +++++++++++++++---- .../docs/reference/command-line-reference.mdx | 8 +++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/cli/internal/run/run.go b/cli/internal/run/run.go index 0c5a4b8ae4c46..3be440918c39b 100644 --- a/cli/internal/run/run.go +++ b/cli/internal/run/run.go @@ -121,6 +121,8 @@ Options: (default false) --no-cache Avoid saving task results to the cache. Useful for development/watch tasks. (default false) + --silence-cached-logs Do not replay logs of all cached tasks. Useful for + reducing output in the terminal. (default false) `) return strings.TrimSpace(helpText) } @@ -542,7 +544,7 @@ func (c *RunCommand) runOperation(g *completeGraph, rs *runSpec, backend *api.La } else if hit { if rs.Opts.stream && fs.FileExists(filepath.Join(rs.Opts.cwd, logFileName)) { logReplayWaitGroup.Add(1) - go replayLogs(targetLogger, targetBaseUI, rs.Opts, logFileName, hash, &logReplayWaitGroup, false) + go replayLogs(targetLogger, targetBaseUI, rs.Opts, logFileName, hash, &logReplayWaitGroup, false, rs.Opts.silenceCached) } targetLogger.Debug("done", "status", "complete", "duration", time.Since(cmdTime)) tracer(TargetCached, nil) @@ -796,6 +798,8 @@ type RunOptions struct { passThroughArgs []string // Restrict execution to only the listed task names. Default false only bool + // Do not replay cached tasks + silenceCached bool } func getDefaultRunOptions() *RunOptions { @@ -811,6 +815,7 @@ func getDefaultRunOptions() *RunOptions { forceExecution: false, stream: true, only: false, + silenceCached: false, } } @@ -906,6 +911,8 @@ func parseRunArgs(args []string, output cli.Ui) (*RunOptions, error) { runOptions.includeDependencies = true case strings.HasPrefix(arg, "--only"): runOptions.only = true + case strings.HasPrefix(arg, "--silence-cached-logs"): + runOptions.silenceCached = true case strings.HasPrefix(arg, "--team"): case strings.HasPrefix(arg, "--token"): case strings.HasPrefix(arg, "--api"): @@ -997,7 +1004,7 @@ func hasGraphViz() bool { } // Replay logs will try to replay logs back to the stdout -func replayLogs(logger hclog.Logger, prefixUi cli.Ui, runOptions *RunOptions, logFileName, hash string, wg *sync.WaitGroup, silent bool) { +func replayLogs(logger hclog.Logger, prefixUi cli.Ui, runOptions *RunOptions, logFileName, hash string, wg *sync.WaitGroup, silent bool, silentOutput bool) { defer wg.Done() logger.Debug("start replaying logs") f, err := os.Open(filepath.Join(runOptions.cwd, logFileName)) @@ -1007,8 +1014,14 @@ func replayLogs(logger hclog.Logger, prefixUi cli.Ui, runOptions *RunOptions, lo } defer f.Close() scan := bufio.NewScanner(f) - for scan.Scan() { - prefixUi.Output(ui.StripAnsi(string(scan.Bytes()))) //Writing to Stdout + if silentOutput { + //Writing to Stdout only the "cache hit, replaying output" line + scan.Scan() + prefixUi.Output(ui.StripAnsi(string(scan.Bytes()))) + } else { + for scan.Scan() { + prefixUi.Output(ui.StripAnsi(string(scan.Bytes()))) //Writing to Stdout + } } logger.Debug("finish replaying logs") } diff --git a/docs/pages/docs/reference/command-line-reference.mdx b/docs/pages/docs/reference/command-line-reference.mdx index bcb04927e8c8f..aaedaa936dbe1 100644 --- a/docs/pages/docs/reference/command-line-reference.mdx +++ b/docs/pages/docs/reference/command-line-reference.mdx @@ -169,6 +169,14 @@ turbo run build --no-cache turbo run dev --parallel --no-cache ``` +#### `--silence-cached-logs` + +Default false. Do not replay cached logs of all tasks. This is useful for minimizing output in the terminal to only show what changed. + +```shell +turbo run build --silence-cached-logs +``` + #### `--only` Default false. Restricts execution to only include specified tasks. This is very similar to how how `lerna` or `pnpm` run tasks by default. From b1201034a11cbb0fea8a131fb6d994dc0e9e3dae Mon Sep 17 00:00:00 2001 From: chelkyl <14041823+chelkyl@users.noreply.github.com> Date: Sat, 5 Mar 2022 00:20:51 -0600 Subject: [PATCH 2/9] refactor: change flag to input option less single-feature flags and more expandable --- cli/internal/run/run.go | 24 ++++++++++++------- .../docs/reference/command-line-reference.mdx | 9 ++++--- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/cli/internal/run/run.go b/cli/internal/run/run.go index 3be440918c39b..060ddf9afba77 100644 --- a/cli/internal/run/run.go +++ b/cli/internal/run/run.go @@ -121,8 +121,9 @@ Options: (default false) --no-cache Avoid saving task results to the cache. Useful for development/watch tasks. (default false) - --silence-cached-logs Do not replay logs of all cached tasks. Useful for - reducing output in the terminal. (default false) + --progress Set type of progress output (standard|reduced). + Use reduced to hide cached task logs. + (default standard) `) return strings.TrimSpace(helpText) } @@ -544,7 +545,7 @@ func (c *RunCommand) runOperation(g *completeGraph, rs *runSpec, backend *api.La } else if hit { if rs.Opts.stream && fs.FileExists(filepath.Join(rs.Opts.cwd, logFileName)) { logReplayWaitGroup.Add(1) - go replayLogs(targetLogger, targetBaseUI, rs.Opts, logFileName, hash, &logReplayWaitGroup, false, rs.Opts.silenceCached) + go replayLogs(targetLogger, targetBaseUI, rs.Opts, logFileName, hash, &logReplayWaitGroup, false, rs.Opts.progressType == "reduced") } targetLogger.Debug("done", "status", "complete", "duration", time.Since(cmdTime)) tracer(TargetCached, nil) @@ -798,8 +799,8 @@ type RunOptions struct { passThroughArgs []string // Restrict execution to only the listed task names. Default false only bool - // Do not replay cached tasks - silenceCached bool + // Task logs output mode + progressType string } func getDefaultRunOptions() *RunOptions { @@ -815,7 +816,7 @@ func getDefaultRunOptions() *RunOptions { forceExecution: false, stream: true, only: false, - silenceCached: false, + progressType: "standard", } } @@ -911,8 +912,15 @@ func parseRunArgs(args []string, output cli.Ui) (*RunOptions, error) { runOptions.includeDependencies = true case strings.HasPrefix(arg, "--only"): runOptions.only = true - case strings.HasPrefix(arg, "--silence-cached-logs"): - runOptions.silenceCached = true + case strings.HasPrefix(arg, "--progress"): + if len(arg[len("--progress="):]) > 0 { + progressType := arg[len("--progress="):] + if progressType != "standard" && progressType != "reduced" { + output.Warn(fmt.Sprintf("[WARNING] unknown value %v for --progress CLI flag. Falling back to standard", progressType)) + progressType = "standard" + } + runOptions.progressType = progressType + } case strings.HasPrefix(arg, "--team"): case strings.HasPrefix(arg, "--token"): case strings.HasPrefix(arg, "--api"): diff --git a/docs/pages/docs/reference/command-line-reference.mdx b/docs/pages/docs/reference/command-line-reference.mdx index aaedaa936dbe1..5913174ec1d30 100644 --- a/docs/pages/docs/reference/command-line-reference.mdx +++ b/docs/pages/docs/reference/command-line-reference.mdx @@ -169,12 +169,15 @@ turbo run build --no-cache turbo run dev --parallel --no-cache ``` -#### `--silence-cached-logs` +#### `--progress` -Default false. Do not replay cached logs of all tasks. This is useful for minimizing output in the terminal to only show what changed. +`type: string` + +Default standard. Set type of progress output. Use standard for normal output. Use reduced to hide cached task logs and only show what changed. ```shell -turbo run build --silence-cached-logs +turbo run build --progress=standard +turbo run build --progress=reduced ``` #### `--only` From c01ba9015ae43df7c94dd72a2f5a9ef44074f4f4 Mon Sep 17 00:00:00 2001 From: chelkyl <14041823+chelkyl@users.noreply.github.com> Date: Sat, 12 Mar 2022 23:05:49 -0600 Subject: [PATCH 3/9] refactor: rename log output option now more specific, also renamed and added more log control modes --- cli/internal/run/run.go | 98 ++++++++++++------- .../docs/reference/command-line-reference.mdx | 8 +- 2 files changed, 64 insertions(+), 42 deletions(-) diff --git a/cli/internal/run/run.go b/cli/internal/run/run.go index fbf64cd1c69e7..7f467e49d2352 100644 --- a/cli/internal/run/run.go +++ b/cli/internal/run/run.go @@ -92,7 +92,7 @@ Options: --scope Specify package(s) to act as entry points for task execution. Supports globs. --cache-dir Specify local filesystem cache directory. - (default "./node_modules/.cache/turbo") + (default "./node_modules/.cache/turbo") --concurrency Limit the concurrency of task execution. Use 1 for serial (i.e. one-at-a-time) execution. (default 10) --continue Continue execution even if a task exits with an error @@ -102,8 +102,8 @@ Options: (default false) --graph Generate a Dot graph of the task execution. --global-deps Specify glob of global filesystem dependencies to - be hashed. Useful for .env and files in the root - directory. Can be specified multiple times. + be hashed. Useful for .env and files in the root + directory. Can be specified multiple times. --since Limit/Set scope to changed packages since a mergebase. This uses the git diff ${target_branch}... mechanism to identify which packages have changed. @@ -121,9 +121,11 @@ Options: (default false) --no-cache Avoid saving task results to the cache. Useful for development/watch tasks. (default false) - --progress Set type of progress output (standard|reduced). - Use reduced to hide cached task logs. - (default standard) + --output-logs Set type of process output logging. Use full to show + all output. Use hash-only to show only turbo-computed + task hashes. Use new-only to show only new output with + only hashes for cached tasks. Use none to hide process + output. (default full) `) return strings.TrimSpace(helpText) } @@ -416,7 +418,7 @@ func (c *RunCommand) runOperation(g *completeGraph, rs *runSpec, backend *api.La } else if hit { if rs.Opts.stream && fs.FileExists(filepath.Join(rs.Opts.cwd, logFileName)) { logReplayWaitGroup.Add(1) - go replayLogs(targetLogger, targetBaseUI, rs.Opts, logFileName, hash, &logReplayWaitGroup, false, rs.Opts.progressType == "reduced") + go replayLogs(targetLogger, targetBaseUI, rs.Opts, logFileName, hash, &logReplayWaitGroup, false, rs.Opts.cachedOutputLogsMode) } targetLogger.Debug("done", "status", "complete", "duration", time.Since(cmdTime)) tracer(TargetCached, nil) @@ -473,7 +475,11 @@ func (c *RunCommand) runOperation(g *completeGraph, rs *runSpec, backend *api.La bufWriter := bufio.NewWriter(output) bufWriter.WriteString(fmt.Sprintf("%scache hit, replaying output %s\n", actualPrefix, ui.Dim(hash))) defer bufWriter.Flush() - writer = io.MultiWriter(os.Stdout, bufWriter) + if rs.Opts.notCachedOutputLogsMode == "none" || rs.Opts.notCachedOutputLogsMode == "hash" { + writer = bufWriter + } else { + writer = io.MultiWriter(os.Stdout, bufWriter) + } } logger := log.New(writer, "", 0) @@ -670,8 +676,12 @@ type RunOptions struct { passThroughArgs []string // Restrict execution to only the listed task names. Default false only bool - // Task logs output mode - progressType string + // Task logs output modes (cached and not cached tasks): + // full - show all, + // hash - only show task hash, + // none - show nothing + cachedOutputLogsMode string + notCachedOutputLogsMode string } func (ro *RunOptions) ScopeOpts() *scope.Opts { @@ -688,18 +698,19 @@ func (ro *RunOptions) ScopeOpts() *scope.Opts { func getDefaultRunOptions() *RunOptions { return &RunOptions{ - bail: true, - includeDependents: true, - parallel: false, - concurrency: 10, - dotGraph: "", - includeDependencies: false, - cache: true, - profile: "", // empty string does no tracing - forceExecution: false, - stream: true, - only: false, - progressType: "standard", + bail: true, + includeDependents: true, + parallel: false, + concurrency: 10, + dotGraph: "", + includeDependencies: false, + cache: true, + profile: "", // empty string does no tracing + forceExecution: false, + stream: true, + only: false, + cachedOutputLogsMode: "full", + notCachedOutputLogsMode: "full", } } @@ -802,14 +813,23 @@ func parseRunArgs(args []string, output cli.Ui) (*RunOptions, error) { includDepsSet = true case strings.HasPrefix(arg, "--only"): runOptions.only = true - case strings.HasPrefix(arg, "--progress"): - if len(arg[len("--progress="):]) > 0 { - progressType := arg[len("--progress="):] - if progressType != "standard" && progressType != "reduced" { - output.Warn(fmt.Sprintf("[WARNING] unknown value %v for --progress CLI flag. Falling back to standard", progressType)) - progressType = "standard" + case strings.HasPrefix(arg, "--output-logs"): + outputLogsMode := arg[len("--output-logs="):] + if len(outputLogsMode) > 0 { + switch outputLogsMode { + case "full", + "none": + runOptions.notCachedOutputLogsMode = outputLogsMode + runOptions.cachedOutputLogsMode = outputLogsMode + case "hash-only": + runOptions.notCachedOutputLogsMode = "hash" + runOptions.cachedOutputLogsMode = "hash" + case "new-only": + runOptions.notCachedOutputLogsMode = "full" + runOptions.cachedOutputLogsMode = "hash" + default: + output.Warn(fmt.Sprintf("[WARNING] unknown value %v for --output-logs CLI flag. Falling back to full", outputLogsMode)) } - runOptions.progressType = progressType } case strings.HasPrefix(arg, "--team"): case strings.HasPrefix(arg, "--token"): @@ -867,7 +887,7 @@ func hasGraphViz() bool { } // Replay logs will try to replay logs back to the stdout -func replayLogs(logger hclog.Logger, prefixUi cli.Ui, runOptions *RunOptions, logFileName, hash string, wg *sync.WaitGroup, silent bool, silentOutput bool) { +func replayLogs(logger hclog.Logger, prefixUi cli.Ui, runOptions *RunOptions, logFileName, hash string, wg *sync.WaitGroup, silent bool, outputLogsMode string) { defer wg.Done() logger.Debug("start replaying logs") f, err := os.Open(filepath.Join(runOptions.cwd, logFileName)) @@ -876,14 +896,16 @@ func replayLogs(logger hclog.Logger, prefixUi cli.Ui, runOptions *RunOptions, lo logger.Error(fmt.Sprintf("error reading logs: %v", err.Error())) } defer f.Close() - scan := bufio.NewScanner(f) - if silentOutput { - //Writing to Stdout only the "cache hit, replaying output" line - scan.Scan() - prefixUi.Output(ui.StripAnsi(string(scan.Bytes()))) - } else { - for scan.Scan() { - prefixUi.Output(ui.StripAnsi(string(scan.Bytes()))) //Writing to Stdout + if outputLogsMode != "none" { + scan := bufio.NewScanner(f) + if outputLogsMode == "hash" { + //Writing to Stdout only the "cache hit, replaying output" line + scan.Scan() + prefixUi.Output(ui.StripAnsi(string(scan.Bytes()))) + } else { + for scan.Scan() { + prefixUi.Output(ui.StripAnsi(string(scan.Bytes()))) //Writing to Stdout + } } } logger.Debug("finish replaying logs") diff --git a/docs/pages/docs/reference/command-line-reference.mdx b/docs/pages/docs/reference/command-line-reference.mdx index 5913174ec1d30..e3245bf11b99b 100644 --- a/docs/pages/docs/reference/command-line-reference.mdx +++ b/docs/pages/docs/reference/command-line-reference.mdx @@ -169,15 +169,15 @@ turbo run build --no-cache turbo run dev --parallel --no-cache ``` -#### `--progress` +#### `--output-logs` `type: string` -Default standard. Set type of progress output. Use standard for normal output. Use reduced to hide cached task logs and only show what changed. +Default `full`. Set type of process output logging. Use `full` to show all output. Use `hash-only` to show only turbo-computed task hashes. Use `new-only` to show only new output with only hashes for cached tasks. Use `none` to hide process output. ```shell -turbo run build --progress=standard -turbo run build --progress=reduced +turbo run build --output-logs=full +turbo run build --output-logs=new-only ``` #### `--only` From 09a3d6a99a454e17633d2085980555d313ed965f Mon Sep 17 00:00:00 2001 From: chelkyl <14041823+chelkyl@users.noreply.github.com> Date: Sun, 13 Mar 2022 00:34:22 -0600 Subject: [PATCH 4/9] fix: missed other process logs to silence --- cli/internal/run/run.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/internal/run/run.go b/cli/internal/run/run.go index da6def75eb7ea..f76dafec5d841 100644 --- a/cli/internal/run/run.go +++ b/cli/internal/run/run.go @@ -910,11 +910,11 @@ func (e *execContext) exec(pt *packageTask) error { return nil } - if e.rs.Opts.stream { + if e.rs.Opts.stream && e.rs.Opts.cachedOutputLogsMode != "none" { targetUi.Output(fmt.Sprintf("cache miss, executing %s", ui.Dim(hash))) } } else { - if e.rs.Opts.stream { + if e.rs.Opts.stream && e.rs.Opts.cachedOutputLogsMode != "none" { targetUi.Output(fmt.Sprintf("cache bypass, force executing %s", ui.Dim(hash))) } } From 5c3ac51664d8045482edbc06cd4ce2d23f7a72da Mon Sep 17 00:00:00 2001 From: chelkyl <14041823+chelkyl@users.noreply.github.com> Date: Tue, 15 Mar 2022 18:00:36 -0500 Subject: [PATCH 5/9] refactor: rename to be concise and consistent --- cli/internal/run/run.go | 54 ++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/cli/internal/run/run.go b/cli/internal/run/run.go index 7472d470dba44..c5ecc30b47080 100644 --- a/cli/internal/run/run.go +++ b/cli/internal/run/run.go @@ -437,10 +437,10 @@ type RunOptions struct { // full - show all, // hash - only show task hash, // none - show nothing - cachedOutputLogsMode string - notCachedOutputLogsMode string - dryRun bool - dryRunJson bool + cacheHitLogsMode string + cacheMissLogsMode string + dryRun bool + dryRunJson bool } func (ro *RunOptions) ScopeOpts() *scope.Opts { @@ -457,19 +457,19 @@ func (ro *RunOptions) ScopeOpts() *scope.Opts { func getDefaultRunOptions() *RunOptions { return &RunOptions{ - bail: true, - includeDependents: true, - parallel: false, - concurrency: 10, - dotGraph: "", - includeDependencies: false, - cache: true, - profile: "", // empty string does no tracing - forceExecution: false, - stream: true, - only: false, - cachedOutputLogsMode: "full", - notCachedOutputLogsMode: "full", + bail: true, + includeDependents: true, + parallel: false, + concurrency: 10, + dotGraph: "", + includeDependencies: false, + cache: true, + profile: "", // empty string does no tracing + forceExecution: false, + stream: true, + only: false, + cacheHitLogsMode: "full", + cacheMissLogsMode: "full", } } @@ -578,14 +578,14 @@ func parseRunArgs(args []string, output cli.Ui) (*RunOptions, error) { switch outputLogsMode { case "full", "none": - runOptions.notCachedOutputLogsMode = outputLogsMode - runOptions.cachedOutputLogsMode = outputLogsMode + runOptions.cacheMissLogsMode = outputLogsMode + runOptions.cacheHitLogsMode = outputLogsMode case "hash-only": - runOptions.notCachedOutputLogsMode = "hash" - runOptions.cachedOutputLogsMode = "hash" + runOptions.cacheMissLogsMode = "hash" + runOptions.cacheHitLogsMode = "hash" case "new-only": - runOptions.notCachedOutputLogsMode = "full" - runOptions.cachedOutputLogsMode = "hash" + runOptions.cacheMissLogsMode = "full" + runOptions.cacheHitLogsMode = "hash" default: output.Warn(fmt.Sprintf("[WARNING] unknown value %v for --output-logs CLI flag. Falling back to full", outputLogsMode)) } @@ -903,18 +903,18 @@ func (e *execContext) exec(pt *packageTask) error { } else if hit { if e.rs.Opts.stream && fs.FileExists(filepath.Join(e.rs.Opts.cwd, logFileName)) { e.logReplayWaitGroup.Add(1) - go replayLogs(targetLogger, e.ui, e.rs.Opts, logFileName, hash, &e.logReplayWaitGroup, false, e.rs.Opts.cachedOutputLogsMode) + go replayLogs(targetLogger, e.ui, e.rs.Opts, logFileName, hash, &e.logReplayWaitGroup, false, e.rs.Opts.cacheHitLogsMode) } targetLogger.Debug("done", "status", "complete", "duration", time.Since(cmdTime)) tracer(TargetCached, nil) return nil } - if e.rs.Opts.stream && e.rs.Opts.cachedOutputLogsMode != "none" { + if e.rs.Opts.stream && e.rs.Opts.cacheHitLogsMode != "none" { targetUi.Output(fmt.Sprintf("cache miss, executing %s", ui.Dim(hash))) } } else { - if e.rs.Opts.stream && e.rs.Opts.cachedOutputLogsMode != "none" { + if e.rs.Opts.stream && e.rs.Opts.cacheHitLogsMode != "none" { targetUi.Output(fmt.Sprintf("cache bypass, force executing %s", ui.Dim(hash))) } } @@ -960,7 +960,7 @@ func (e *execContext) exec(pt *packageTask) error { bufWriter := bufio.NewWriter(output) bufWriter.WriteString(fmt.Sprintf("%scache hit, replaying output %s\n", actualPrefix, ui.Dim(hash))) defer bufWriter.Flush() - if e.rs.Opts.notCachedOutputLogsMode == "none" || e.rs.Opts.notCachedOutputLogsMode == "hash" { + if e.rs.Opts.cacheMissLogsMode == "none" || e.rs.Opts.cacheMissLogsMode == "hash" { // only write to log file, not to stdout writer = bufWriter } else { From 3df907a3b6dc18d3001c889cd848d6554efb87e5 Mon Sep 17 00:00:00 2001 From: chelkyl <14041823+chelkyl@users.noreply.github.com> Date: Tue, 15 Mar 2022 18:01:34 -0500 Subject: [PATCH 6/9] feat: remove unnecessary len check --- cli/internal/run/run.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/cli/internal/run/run.go b/cli/internal/run/run.go index c5ecc30b47080..1ac7feedf5899 100644 --- a/cli/internal/run/run.go +++ b/cli/internal/run/run.go @@ -574,7 +574,6 @@ func parseRunArgs(args []string, output cli.Ui) (*RunOptions, error) { runOptions.only = true case strings.HasPrefix(arg, "--output-logs"): outputLogsMode := arg[len("--output-logs="):] - if len(outputLogsMode) > 0 { switch outputLogsMode { case "full", "none": @@ -588,7 +587,6 @@ func parseRunArgs(args []string, output cli.Ui) (*RunOptions, error) { runOptions.cacheHitLogsMode = "hash" default: output.Warn(fmt.Sprintf("[WARNING] unknown value %v for --output-logs CLI flag. Falling back to full", outputLogsMode)) - } } case strings.HasPrefix(arg, "--dry-run"): runOptions.dryRun = true From 2752dd89fcc727eb6fc756a57c611eed68e1ee96 Mon Sep 17 00:00:00 2001 From: chelkyl <14041823+chelkyl@users.noreply.github.com> Date: Tue, 15 Mar 2022 18:09:19 -0500 Subject: [PATCH 7/9] fix: must include = else slice bounds out of range --- cli/internal/run/run.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/cli/internal/run/run.go b/cli/internal/run/run.go index 1ac7feedf5899..35f6f479f8229 100644 --- a/cli/internal/run/run.go +++ b/cli/internal/run/run.go @@ -572,21 +572,21 @@ func parseRunArgs(args []string, output cli.Ui) (*RunOptions, error) { includDepsSet = true case strings.HasPrefix(arg, "--only"): runOptions.only = true - case strings.HasPrefix(arg, "--output-logs"): + case strings.HasPrefix(arg, "--output-logs="): outputLogsMode := arg[len("--output-logs="):] - switch outputLogsMode { - case "full", - "none": - runOptions.cacheMissLogsMode = outputLogsMode - runOptions.cacheHitLogsMode = outputLogsMode - case "hash-only": - runOptions.cacheMissLogsMode = "hash" - runOptions.cacheHitLogsMode = "hash" - case "new-only": - runOptions.cacheMissLogsMode = "full" - runOptions.cacheHitLogsMode = "hash" - default: - output.Warn(fmt.Sprintf("[WARNING] unknown value %v for --output-logs CLI flag. Falling back to full", outputLogsMode)) + switch outputLogsMode { + case "full", + "none": + runOptions.cacheMissLogsMode = outputLogsMode + runOptions.cacheHitLogsMode = outputLogsMode + case "hash-only": + runOptions.cacheMissLogsMode = "hash" + runOptions.cacheHitLogsMode = "hash" + case "new-only": + runOptions.cacheMissLogsMode = "full" + runOptions.cacheHitLogsMode = "hash" + default: + output.Warn(fmt.Sprintf("[WARNING] unknown value %v for --output-logs CLI flag. Falling back to full", outputLogsMode)) } case strings.HasPrefix(arg, "--dry-run"): runOptions.dryRun = true From 9308836ef9a5386aba8757a0bcac181df535335b Mon Sep 17 00:00:00 2001 From: chelkyl <14041823+chelkyl@users.noreply.github.com> Date: Tue, 15 Mar 2022 18:28:18 -0500 Subject: [PATCH 8/9] refactor: output logs mode strings should be enums --- cli/internal/run/run.go | 46 +++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/cli/internal/run/run.go b/cli/internal/run/run.go index 35f6f479f8229..9dc24c021f06d 100644 --- a/cli/internal/run/run.go +++ b/cli/internal/run/run.go @@ -71,6 +71,14 @@ type runSpec struct { Opts *RunOptions } +type LogsMode string + +const ( + FullLogs LogsMode = "full" + HashLogs LogsMode = "hash" + NoLogs LogsMode = "none" +) + func (rs *runSpec) ArgsForTask(task string) []string { passThroughArgs := make([]string, 0, len(rs.Opts.passThroughArgs)) for _, target := range rs.Targets { @@ -437,8 +445,8 @@ type RunOptions struct { // full - show all, // hash - only show task hash, // none - show nothing - cacheHitLogsMode string - cacheMissLogsMode string + cacheHitLogsMode LogsMode + cacheMissLogsMode LogsMode dryRun bool dryRunJson bool } @@ -468,8 +476,8 @@ func getDefaultRunOptions() *RunOptions { forceExecution: false, stream: true, only: false, - cacheHitLogsMode: "full", - cacheMissLogsMode: "full", + cacheHitLogsMode: FullLogs, + cacheMissLogsMode: FullLogs, } } @@ -575,16 +583,18 @@ func parseRunArgs(args []string, output cli.Ui) (*RunOptions, error) { case strings.HasPrefix(arg, "--output-logs="): outputLogsMode := arg[len("--output-logs="):] switch outputLogsMode { - case "full", - "none": - runOptions.cacheMissLogsMode = outputLogsMode - runOptions.cacheHitLogsMode = outputLogsMode + case "full": + runOptions.cacheMissLogsMode = FullLogs + runOptions.cacheHitLogsMode = FullLogs + case "none": + runOptions.cacheMissLogsMode = NoLogs + runOptions.cacheHitLogsMode = NoLogs case "hash-only": - runOptions.cacheMissLogsMode = "hash" - runOptions.cacheHitLogsMode = "hash" + runOptions.cacheMissLogsMode = HashLogs + runOptions.cacheHitLogsMode = HashLogs case "new-only": - runOptions.cacheMissLogsMode = "full" - runOptions.cacheHitLogsMode = "hash" + runOptions.cacheMissLogsMode = FullLogs + runOptions.cacheHitLogsMode = HashLogs default: output.Warn(fmt.Sprintf("[WARNING] unknown value %v for --output-logs CLI flag. Falling back to full", outputLogsMode)) } @@ -783,7 +793,7 @@ func (c *RunCommand) executeDryRun(engine *core.Scheduler, g *completeGraph, rs } // Replay logs will try to replay logs back to the stdout -func replayLogs(logger hclog.Logger, prefixUi cli.Ui, runOptions *RunOptions, logFileName, hash string, wg *sync.WaitGroup, silent bool, outputLogsMode string) { +func replayLogs(logger hclog.Logger, prefixUi cli.Ui, runOptions *RunOptions, logFileName, hash string, wg *sync.WaitGroup, silent bool, outputLogsMode LogsMode) { defer wg.Done() logger.Debug("start replaying logs") f, err := os.Open(filepath.Join(runOptions.cwd, logFileName)) @@ -792,9 +802,9 @@ func replayLogs(logger hclog.Logger, prefixUi cli.Ui, runOptions *RunOptions, lo logger.Error(fmt.Sprintf("error reading logs: %v", err.Error())) } defer f.Close() - if outputLogsMode != "none" { + if outputLogsMode != NoLogs { scan := bufio.NewScanner(f) - if outputLogsMode == "hash" { + if outputLogsMode == HashLogs { //Writing to Stdout only the "cache hit, replaying output" line scan.Scan() prefixUi.Output(ui.StripAnsi(string(scan.Bytes()))) @@ -908,11 +918,11 @@ func (e *execContext) exec(pt *packageTask) error { return nil } - if e.rs.Opts.stream && e.rs.Opts.cacheHitLogsMode != "none" { + if e.rs.Opts.stream && e.rs.Opts.cacheHitLogsMode != NoLogs { targetUi.Output(fmt.Sprintf("cache miss, executing %s", ui.Dim(hash))) } } else { - if e.rs.Opts.stream && e.rs.Opts.cacheHitLogsMode != "none" { + if e.rs.Opts.stream && e.rs.Opts.cacheHitLogsMode != NoLogs { targetUi.Output(fmt.Sprintf("cache bypass, force executing %s", ui.Dim(hash))) } } @@ -958,7 +968,7 @@ func (e *execContext) exec(pt *packageTask) error { bufWriter := bufio.NewWriter(output) bufWriter.WriteString(fmt.Sprintf("%scache hit, replaying output %s\n", actualPrefix, ui.Dim(hash))) defer bufWriter.Flush() - if e.rs.Opts.cacheMissLogsMode == "none" || e.rs.Opts.cacheMissLogsMode == "hash" { + if e.rs.Opts.cacheMissLogsMode == NoLogs || e.rs.Opts.cacheMissLogsMode == HashLogs { // only write to log file, not to stdout writer = bufWriter } else { From 1acb2130f33bf2ffbaea5f8100a80fc3353ae3bc Mon Sep 17 00:00:00 2001 From: chelkyl <14041823+chelkyl@users.noreply.github.com> Date: Wed, 16 Mar 2022 21:17:48 -0500 Subject: [PATCH 9/9] test: add new log mode opts to config tests --- cli/internal/run/run_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/cli/internal/run/run_test.go b/cli/internal/run/run_test.go index aaeb9ca43538b..d146df212c601 100644 --- a/cli/internal/run/run_test.go +++ b/cli/internal/run/run_test.go @@ -39,6 +39,8 @@ func TestParseConfig(t *testing.T) { profile: "", cwd: defaultCwd, cacheFolder: defaultCacheFolder, + cacheHitLogsMode: FullLogs, + cacheMissLogsMode: FullLogs, }, }, { @@ -56,6 +58,8 @@ func TestParseConfig(t *testing.T) { profile: "", cwd: "zop", cacheFolder: filepath.FromSlash("zop/node_modules/.cache/turbo"), + cacheHitLogsMode: FullLogs, + cacheMissLogsMode: FullLogs, }, }, { @@ -74,6 +78,8 @@ func TestParseConfig(t *testing.T) { scope: []string{"foo", "blah"}, cwd: defaultCwd, cacheFolder: defaultCacheFolder, + cacheHitLogsMode: FullLogs, + cacheMissLogsMode: FullLogs, }, }, { @@ -91,6 +97,8 @@ func TestParseConfig(t *testing.T) { profile: "", cwd: defaultCwd, cacheFolder: defaultCacheFolder, + cacheHitLogsMode: FullLogs, + cacheMissLogsMode: FullLogs, }, }, { @@ -108,6 +116,8 @@ func TestParseConfig(t *testing.T) { profile: "", cwd: defaultCwd, cacheFolder: defaultCacheFolder, + cacheHitLogsMode: FullLogs, + cacheMissLogsMode: FullLogs, }, }, { @@ -126,6 +136,8 @@ func TestParseConfig(t *testing.T) { cwd: defaultCwd, cacheFolder: defaultCacheFolder, passThroughArgs: []string{"--boop", "zoop"}, + cacheHitLogsMode: FullLogs, + cacheMissLogsMode: FullLogs, }, }, { @@ -144,6 +156,8 @@ func TestParseConfig(t *testing.T) { cwd: defaultCwd, cacheFolder: defaultCacheFolder, passThroughArgs: []string{}, + cacheHitLogsMode: FullLogs, + cacheMissLogsMode: FullLogs, }, }, { @@ -160,6 +174,8 @@ func TestParseConfig(t *testing.T) { cacheFolder: defaultCacheFolder, scope: []string{"bar"}, since: "some-ref", + cacheHitLogsMode: FullLogs, + cacheMissLogsMode: FullLogs, }, }, }