Skip to content

Move RunAndLog to main.sh#2420

Merged
CLHatch merged 1 commit intomainfrom
RunAndLog
Jan 26, 2026
Merged

Move RunAndLog to main.sh#2420
CLHatch merged 1 commit intomainfrom
RunAndLog

Conversation

@CLHatch
Copy link
Contributor

@CLHatch CLHatch commented Jan 26, 2026

Pull request

Purpose
Describe the problem or feature in addition to a link to the issues.

Approach
How does this change address the problem?

Open Questions and Pre-Merge TODOs
Check all boxes as they are completed

  • Use github checklists. When solved, check the box and explain the answer.

Learning
Describe the research stage
Links to blog posts, patterns, libraries or addons used to solve this problem

Requirements
Check all boxes as they are completed

Summary by Sourcery

Add shared logging helpers to centralize command execution and output handling in main.sh.

Enhancements:

  • Introduce a PrefixFileLines helper to prefix each line of a file when logging command output.
  • Add a RunAndLog wrapper to standardize running shell commands with configurable logging, output redirection, and error reporting.

@CLHatch CLHatch requested a review from a team as a code owner January 26, 2026 05:17
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jan 26, 2026

Reviewer's Guide

Adds generalized RunAndLog and helper PrefixFileLines implementations directly into main.sh to standardize command execution, output prefixing, and logging behavior, including flexible notice types and output redirection modes.

Sequence diagram for RunAndLog command execution and logging

sequenceDiagram
    participant Caller
    participant RunAndLog
    participant SystemCommand
    participant TempFile as OutputFile
    participant Logger

    Caller->>RunAndLog: RunAndLog(RunningNoticeType, OutputNoticeType, ErrorNoticeType, ErrorMessage, Command)

    alt RunningNoticeType is set
        RunAndLog->>Logger: RunningNoticeType("Running: CommandText")
    end

    alt OutputNoticeType requires logging and not both null
        RunAndLog->>TempFile: Create temp file for command output
    end

    alt Both stdout and stderr to null
        RunAndLog->>SystemCommand: Execute Command &> /dev/null
        SystemCommand-->>RunAndLog: exit code
    else stderr to null, stdout to file
        RunAndLog->>SystemCommand: Execute Command > OutputFile 2> /dev/null
        SystemCommand-->>RunAndLog: exit code
    else stdout to null, stderr to file
        RunAndLog->>SystemCommand: Execute Command 2> OutputFile > /dev/null
        SystemCommand-->>RunAndLog: exit code
    else stdout and stderr to file
        RunAndLog->>SystemCommand: Execute Command &> OutputFile
        SystemCommand-->>RunAndLog: exit code
    else no redirection
        RunAndLog->>SystemCommand: Execute Command
        SystemCommand-->>RunAndLog: exit code
    end

    alt OutputFile exists and is nonempty
        RunAndLog->>TempFile: Read lines
        RunAndLog->>RunAndLog: PrefixFileLines(Prefix, OutputFile)
        RunAndLog->>Logger: OutputNoticeType(prefixed output)
        RunAndLog->>TempFile: Delete OutputFile
    end

    alt exit code is nonzero and ErrorNoticeType is set
        RunAndLog->>Logger: ErrorNoticeType(ErrorMessage, Failing command: CommandText)
    end

    RunAndLog-->>Caller: Return exit code
Loading

File-Level Changes

Change Details Files
Add PrefixFileLines helper to prefix each line of a file with a colored, formatted prefix for logging
  • Introduce PrefixFileLines function that accepts a prefix and filename, counts file lines, and uses paste with yes/head to prepend the prefix to each line
  • Use process substitution and wc -l to avoid loading file content into memory while computing line count
main.sh
Implement RunAndLog function to centralize running commands with optional notices, output routing, and error handling
  • Define RunAndLog with positional, mostly optional parameters controlling running notice, output notice (with possible prefix), error notice, error message, and the command to execute
  • Parse OutputNoticeType for an optional prefix component and build a colored prefix using the existing color map C and reset color NC
  • Build a shell-escaped textual representation of the command using printf %q and xargs for inclusion in log messages
  • Support routing stdout/stderr independently to /dev/null or a temporary output file based on flags encoded in OutputNoticeType (errtonull, outtonull, bothtonull) combined with notice-type regex validation
  • Use mktemp with APPLICATION_NAME, FUNCNAME, and a template suffix to create a per-call temporary output file, then execute the command with appropriate redirections and capture the exit status
  • If an output file is used and non-empty, log its contents via the output notice function after prefixing lines with PrefixFileLines, then remove the temporary file
  • On non-zero exit, conditionally log an error via the configured ErrorNoticeType with the provided error message and a highlighted failing command string before returning the command’s exit code
main.sh

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@CLHatch CLHatch merged commit 018eeef into main Jan 26, 2026
16 checks passed
@CLHatch CLHatch deleted the RunAndLog branch January 26, 2026 05:18
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • In the RunAndLog tail section, instead of if [[ -n ${OutputFile-} && -n $(cat "${OutputFile}") ]]; then, consider using [[ -n ${OutputFile-} && -s ${OutputFile} ]] to avoid a useless cat, eliminate subshell/word-splitting concerns, and rely on the built-in file size test.
  • The string flags ErrToNull and OutToNull in RunAndLog work, but using local ErrToNull=false/true and testing them as [ "$ErrToNull" = true ] (or just using separate condition branches without boolean variables) would simplify the control flow and make the redirection handling easier to read.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In the `RunAndLog` tail section, instead of `if [[ -n ${OutputFile-} && -n $(cat "${OutputFile}") ]]; then`, consider using `[[ -n ${OutputFile-} && -s ${OutputFile} ]]` to avoid a useless `cat`, eliminate subshell/word-splitting concerns, and rely on the built-in file size test.
- The string flags `ErrToNull` and `OutToNull` in `RunAndLog` work, but using `local ErrToNull=false`/`true` and testing them as `[ "$ErrToNull" = true ]` (or just using separate condition branches without boolean variables) would simplify the control flow and make the redirection handling easier to read.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments