Skip to content

Fix: Standardize Syntax and Error Handling#22

Merged
rmottanet merged 3 commits into
mainfrom
fix/sonar-alarms
Feb 1, 2026
Merged

Fix: Standardize Syntax and Error Handling#22
rmottanet merged 3 commits into
mainfrom
fix/sonar-alarms

Conversation

@rmottanet

Copy link
Copy Markdown
Owner

This pull request addresses SonarCloud code quality alarms by standardizing shell script syntax patterns and improving error handling consistency. The changes implement industry best practices for conditional testing and proper output stream separation across the codebase.


Changes Implemented:

  • Conditional Test Standardization: Updated conditional tests from [ to [[ operator in:

    • src/github/gh-repo-list.sh: GitHub repository listing operations
    • src/gitnap/gn-init-git.sh: Git initialization utilities
  • Error Stream Redirection: Enhanced error handling in:

    • src/utils/endpoints.sh: API endpoint configuration utilities
    • Updated all error/warning messages to use echo >&2 for proper stderr redirection

Technical Details:

1. Conditional Test Operator Standardization

Before (POSIX [ operator):

if [ -z "$variable" ]; then
    echo "Error"
fi

After (Bash [[ built-in):

if [[ -z "$variable" ]]; then
    echo "Error" >&2
fi

Benefits of [[ Operator:

  • Safer string handling: No word splitting or pathname expansion
  • Enhanced features: Pattern matching (=~), logical operators
  • Improved performance: Bash built-in vs external command
  • Better syntax: No need to quote variables in most cases

2. Error Stream Separation

Before (stdout pollution):

echo "Error: Invalid endpoint"

After (proper stderr):

echo "Error: Invalid endpoint" >&2

Benefits of stderr redirection:

  • Clean stdout: Preserved for actual command output and piping
  • Proper logging: Errors separated from normal output
  • Script robustness: Enables proper error detection in pipelines
  • User experience: Allows selective filtering of error messages

SonarCloud Compliance:

Addressed Rules:

  • S7688: "Use [[ ... ]] instead of [ ... ] for conditional tests"

    • Why: The [[ construct is a bash built-in that provides more features and safer string handling than the POSIX [ command. It prevents word splitting and pathname expansion issues.
  • S7677: "Error messages should be sent to stderr"

    • Why: Sending error messages to stderr (file descriptor 2) instead of stdout (file descriptor 1) ensures proper separation of error output from regular output, which is crucial for script pipelines and proper error handling.

Quality Impact:

  • Maintainability: Improved code clarity and modern bash practices
  • Reliability: Safer variable handling in conditional tests
  • Usability: Proper error stream separation for scripting contexts
  • Performance: Bash built-in operators are faster than external commands

Motivation:

These changes address fundamental shell scripting best practices that directly impact script reliability and maintainability:

  1. Modern Bash Practices: The [[ operator is recommended for all bash scripts as it provides safer and more feature-rich conditional testing
  2. Output Stream Discipline: Proper separation of stdout and stderr is essential for script composability and pipeline reliability
  3. Code Consistency: Establishes uniform patterns across the codebase
  4. Static Analysis Compliance: Meets SonarCloud's shell scripting standards

The updates align with industry standards from:

  • Google Shell Style Guide recommendations
  • ShellCheck diagnostic rules (SC2039, SC2069)
  • Bash scripting community best practices

Reference


Testing & Validation:

All modified scripts have been validated to ensure:

  • No functional changes to existing behavior
  • Conditional tests work identically with [[ operator
  • Error messages appear correctly on stderr without affecting stdout
  • Scripts remain compatible with bash 4.0+ environments
  • All existing functionality preserved

These improvements enhance the codebase's quality foundation while maintaining backward compatibility and operational reliability.

- Updated all error/warning messages from 'echo' to 'echo >&2'
- Ensures proper separation of output streams
- Maintains clean stdout for pipeline processing
- Follows shell scripting best practices for error handling
@rmottanet rmottanet self-assigned this Feb 1, 2026
@rmottanet rmottanet added this to @ws2git Feb 1, 2026
@sonarqubecloud

sonarqubecloud Bot commented Feb 1, 2026

Copy link
Copy Markdown

@rmottanet rmottanet merged commit c906827 into main Feb 1, 2026
5 checks passed
@rmottanet rmottanet deleted the fix/sonar-alarms branch February 1, 2026 22:13
@github-project-automation github-project-automation Bot moved this to Done in @ws2git Feb 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant