Skip to content

Commit

Permalink
Assertion - Jq (#319)
Browse files Browse the repository at this point in the history
* Update Initialize-AzOpsEnvironment.ps1
Fix jq casing

* Update Strings.psd1
Refactor logging messages

* Update Invoke-AzOpsNativeCommand.ps1
Add -NoNewLine switch

* Update Assert-AzOpsJqDependency.ps1
Add logic to validate installed version

* Rename Assert-AzOpsJqDependency.ps1
  • Loading branch information
ljtill authored Apr 27, 2021
1 parent b06c2b3 commit 42cd63f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/functions/Initialize-AzOpsEnvironment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

begin {
Assert-AzOpsWindowsLongPath -Cmdlet $PSCmdlet
Assert-AzOpsJQDependency -Cmdlet $PSCmdlet
Assert-AzOpsJqDependency -Cmdlet $PSCmdlet

$allAzContext = Get-AzContext -ListAvailable
if (-not $allAzContext) {
Expand Down
37 changes: 0 additions & 37 deletions src/internal/functions/Assert-AzOpsJQDependency.ps1

This file was deleted.

46 changes: 46 additions & 0 deletions src/internal/functions/Assert-AzOpsJqDependency.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function Assert-AzOpsJqDependency {

<#
.SYNOPSIS
Asserts that - if jq is installed and in current path
.DESCRIPTION
Asserts that - if jq is installed and in current path
.PARAMETER Cmdlet
The $PSCmdlet variable of the calling command.
.EXAMPLE
> Assert-AzOpsJqDependency -Cmdlet $PSCmdlet
#>

[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
$Cmdlet
)

process {
Write-PSFMessage -Level InternalComment -String 'Assert-AzOpsJqDependency.Validating'

$result = (Invoke-AzOpsNativeCommand -ScriptBlock { jq --version } -IgnoreExitcode)
$installed = $result -as [bool]

if ($installed) {
[double]$version = ($result).Split("-")[1]
if ($version -ge 1.6) {
Write-PSFMessage -Level InternalComment -String 'Assert-AzOpsJqDependency.Success'
return
}
else {
$exception = [System.InvalidOperationException]::new('Unsupported version of jq installed. Please update to a minimum jq version of 1.6')
$errorRecord = [System.Management.Automation.ErrorRecord]::new($exception, "ConfigurationError", 'InvalidOperation', $null)
Write-PSFMessage -Level Warning -String 'Assert-AzOpsJqDependency.Failed' -Tag error
$Cmdlet.ThrowTerminatingError($errorRecord)
}
}

$exception = [System.InvalidOperationException]::new('Unable to locate jq installation')
$errorRecord = [System.Management.Automation.ErrorRecord]::new($exception, "ConfigurationError", 'InvalidOperation', $null)
Write-PSFMessage -Level Warning -String 'Assert-AzOpsJqDependency.Failed' -Tag error
$Cmdlet.ThrowTerminatingError($errorRecord)
}

}
2 changes: 1 addition & 1 deletion src/internal/functions/Invoke-AzOpsNativeCommand.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
else { $output = & $ScriptBlock }

if (-not $Quiet -and $output) {
$output | Out-String | ForEach-Object {
$output | Out-String -NoNewLine | ForEach-Object {
Write-PSFMessage -Level Debug -Message $_
}
$output
Expand Down
4 changes: 2 additions & 2 deletions src/localized/en-us/Strings.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
'Assert-AzOpsWindowsLongPath.No.Registry' = 'Windows has not yet been configured for long path support' #
'Assert-AzOpsWindowsLongPath.Validating' = 'Validating Windows environment for LongPath support' #

'Assert-AzOpsJQDependency.Validating' = 'Validating if jq is in current path' #
'Assert-AzOpsJQDependency.Success' = 'jq is found in current path' #
'Assert-AzOpsJqDependency.Validating' = 'Validating if jq is in current path' #
'Assert-AzOpsJqDependency.Success' = 'Supported version of jq found in current path' #

'AzOpsScope.GetAzOpsManagementGroupPath.NotFound' = 'Management Group not found: {0}' # $managementgroupName
'AzOpsScope.GetAzOpsResourcePath.NotFound' = 'Unable to determine Resource Scope for: {0}' # $this.Scope
Expand Down

0 comments on commit 42cd63f

Please sign in to comment.