Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement: Powershell Error Table Format Improvements #3620

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Note: Can be used with `oxsecurity/megalinter@beta` in your GitHub Action mega-l

- Fixes
- [syft](https://github.com/anchore/syft) use `scan` instead of deprecated `packages` arg
- [Powershell](https://github.com/PowerShell/PSScriptAnalyzer#readme) Error table truncation improvements

- Doc

Expand Down
33 changes: 22 additions & 11 deletions megalinter/linters/PowershellLinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys
from shutil import get_terminal_size

from megalinter import Linter, config
from megalinter import Linter, config, utils


class PowershellLinter(Linter):
Expand Down Expand Up @@ -56,17 +56,10 @@ def build_lint_command(self, file=None):
and self.cli_lint_fix_arg_name is not None
):
pwsh_script[0] += f" {self.cli_lint_fix_arg_name}"

if self.linter_name == "powershell":
# Enforce table output for easier parsing of number of errors
# (severity first)
pwsh_script[
0
] += " | Format-Table -AutoSize -Wrap -Property Severity, RuleName, ScriptName, Line, Message"
# Format output to fit in terminal, respecting the terminal width.
# Use good defaults, shutil respects COLUMNS env var. For more info:
# https://stackoverflow.com/a/76889369
size = get_terminal_size()
pwsh_script[0] += f" | Out-String -Width {size.columns}"
pwsh_script[0] = self.format_powershell_output(pwsh_script[0])

cmd = [
*self.cli_executable,
"-NoProfile",
Expand All @@ -76,6 +69,24 @@ def build_lint_command(self, file=None):
]
return cmd

def format_powershell_output(self, pwsh_script):
if utils.is_ci():
width = 150 # Use a default width in CI environments to prevent output cutoff
else:
width = get_terminal_size().columns # Use the terminal width when not in CI

# Format the output to a table with specific columns
pwsh_script += " | Format-Table -AutoSize -Wrap -Property " \
"@{Name='Severity'; Expression={$_.Severity}; Alignment='left'}," \
" @{Name='RuleName'; Expression={$_.RuleName}; Alignment='left'}," \
" @{Name='ScriptName'; Expression={$_.ScriptName}; Alignment='left'}," \
" @{Name='Line'; Expression={$_.Line}; Alignment='right'}," \
" @{Name='Message'; Expression={$_.Message}; Alignment='left'}"

# Ensure the output string fits within the specified width
pwsh_script += f" | Out-String -Width {width}"
return pwsh_script

# Build the CLI command to get linter version
def build_version_command(self):
cmd = [
Expand Down