Skip to content

Commit 49df2bc

Browse files
committed
Fix console color fallback handling for invalid host values
1 parent 5d545c0 commit 49df2bc

1 file changed

Lines changed: 24 additions & 24 deletions

File tree

powershell/VstsTaskSdk/LoggingCommandFunctions.ps1

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ $IssueSources = @{
1919
TaskInternal = "TaskInternal"
2020
}
2121

22+
function Get-ConsoleColorOrDefault {
23+
[CmdletBinding()]
24+
param(
25+
[AllowNull()]
26+
$Value,
27+
[Parameter(Mandatory = $true)]
28+
[System.ConsoleColor]$DefaultColor
29+
)
30+
31+
if ($Value -is [System.ConsoleColor] -and [System.Enum]::IsDefined([System.ConsoleColor], $Value)) {
32+
return $Value
33+
}
34+
35+
return $DefaultColor
36+
}
37+
2238
<#
2339
.SYNOPSIS
2440
See https://github.com/Microsoft/vsts-tasks/blob/master/docs/authoring/commands.md
@@ -584,19 +600,11 @@ function Write-LogIssue {
584600
}
585601

586602
if ($Type -eq 'error') {
587-
$foregroundColor = $host.PrivateData.ErrorForegroundColor
588-
$backgroundColor = $host.PrivateData.ErrorBackgroundColor
589-
if ($foregroundColor -isnot [System.ConsoleColor] -or $backgroundColor -isnot [System.ConsoleColor]) {
590-
$foregroundColor = [System.ConsoleColor]::Red
591-
$backgroundColor = [System.ConsoleColor]::Black
592-
}
603+
$foregroundColor = Get-ConsoleColorOrDefault -Value $host.PrivateData.ErrorForegroundColor -DefaultColor ([System.ConsoleColor]::Red)
604+
$backgroundColor = Get-ConsoleColorOrDefault -Value $host.PrivateData.ErrorBackgroundColor -DefaultColor ([System.ConsoleColor]::Black)
593605
} else {
594-
$foregroundColor = $host.PrivateData.WarningForegroundColor
595-
$backgroundColor = $host.PrivateData.WarningBackgroundColor
596-
if ($foregroundColor -isnot [System.ConsoleColor] -or $backgroundColor -isnot [System.ConsoleColor]) {
597-
$foregroundColor = [System.ConsoleColor]::Yellow
598-
$backgroundColor = [System.ConsoleColor]::Black
599-
}
606+
$foregroundColor = Get-ConsoleColorOrDefault -Value $host.PrivateData.WarningForegroundColor -DefaultColor ([System.ConsoleColor]::Yellow)
607+
$backgroundColor = Get-ConsoleColorOrDefault -Value $host.PrivateData.WarningBackgroundColor -DefaultColor ([System.ConsoleColor]::Black)
600608
}
601609

602610
Write-Host $command -ForegroundColor $foregroundColor -BackgroundColor $backgroundColor
@@ -615,19 +623,11 @@ function Write-TaskDebug_Internal {
615623
}
616624

617625
if ($AsVerbose) {
618-
$foregroundColor = $host.PrivateData.VerboseForegroundColor
619-
$backgroundColor = $host.PrivateData.VerboseBackgroundColor
620-
if ($foregroundColor -isnot [System.ConsoleColor] -or $backgroundColor -isnot [System.ConsoleColor]) {
621-
$foregroundColor = [System.ConsoleColor]::Cyan
622-
$backgroundColor = [System.ConsoleColor]::Black
623-
}
626+
$foregroundColor = Get-ConsoleColorOrDefault -Value $host.PrivateData.VerboseForegroundColor -DefaultColor ([System.ConsoleColor]::Cyan)
627+
$backgroundColor = Get-ConsoleColorOrDefault -Value $host.PrivateData.VerboseBackgroundColor -DefaultColor ([System.ConsoleColor]::Black)
624628
} else {
625-
$foregroundColor = $host.PrivateData.DebugForegroundColor
626-
$backgroundColor = $host.PrivateData.DebugBackgroundColor
627-
if ($foregroundColor -isnot [System.ConsoleColor] -or $backgroundColor -isnot [System.ConsoleColor]) {
628-
$foregroundColor = [System.ConsoleColor]::DarkGray
629-
$backgroundColor = [System.ConsoleColor]::Black
630-
}
629+
$foregroundColor = Get-ConsoleColorOrDefault -Value $host.PrivateData.DebugForegroundColor -DefaultColor ([System.ConsoleColor]::DarkGray)
630+
$backgroundColor = Get-ConsoleColorOrDefault -Value $host.PrivateData.DebugBackgroundColor -DefaultColor ([System.ConsoleColor]::Black)
631631
}
632632

633633
Write-Host -Object $command -ForegroundColor $foregroundColor -BackgroundColor $backgroundColor

0 commit comments

Comments
 (0)