Skip to content

Commit

Permalink
Merge branch 'main' into 1486-update-readme-with-new-scubagear-graphics
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuynhMITRE authored Jan 23, 2025
2 parents bf2d656 + a57f269 commit fb47483
Showing 1 changed file with 12 additions and 31 deletions.
43 changes: 12 additions & 31 deletions PowerShell/ScubaGear/CheckVersion.ps1
Original file line number Diff line number Diff line change
@@ -1,43 +1,24 @@
function Invoke-CheckScubaGearVersionPSGallery {

# Retrieve the installed version of ScubaGear from the system
$InstalledModule = Get-Module -Name ScubaGear -ListAvailable -ErrorAction 'Stop'
if ($InstalledModule) {
$CurrentVersion = [System.Version]$InstalledModule.Version
} else {
# If we are here, ScubaGear is not installed from PSGallery.
# Or it may have been installed a different way in a nonstandard folder,
# or is running in an extracted release folder. Check github instead.
return Invoke-CheckScubaGearVersionGithub -ErrorAction 'Stop'
}

# Retrieve the latest version from PowerShell Gallery
$ModuleInfo = Find-Module -Name ScubaGear -ErrorAction 'Stop'
$LatestVersion = [System.Version]$ModuleInfo.Version

if ($CurrentVersion -lt $LatestVersion) {
Write-Warning "A new version of ScubaGear ($LatestVersion) is available on PowerShell Gallery. This notification can be disabled by setting `$env:SCUBAGEAR_SKIP_VERSION_CHECK = `$true before running ScubaGear."

}

# Store the current time in the file to mark the last check time
(Get-Date -ErrorAction 'Stop').ToString() | Set-Content $VersionCheckFile -ErrorAction 'Stop'
}
function Invoke-CheckScubaGearVersion {
<#
.SYNOPSIS
Complain if a newer version of ScubaGear is available from the Github release page.

function Invoke-CheckScubaGearVersionGithub {
.DESCRIPTION
Checks latest version available on the Github release page and compares it to the current running version.
#>
$ScubaManifest = Import-PowerShellDataFile (Join-Path -Path $PSScriptRoot -ChildPath 'ScubaGear.psd1' -Resolve -ErrorAction 'Stop' ) -ErrorAction 'Stop'
$CurrentVersion = $ScubaManifest.ModuleVersion
$LatestVersion = $(Invoke-RestMethod -Uri "https://api.github.com/repos/cisagov/ScubaGear/releases/latest" -ErrorAction 'Stop').tag_name.TrimStart("v")
if ($CurrentVersion -ne $LatestVersion) {
Write-Warning "A new version of ScubaGear ($latestVersion) is available. Please consider updating at: https://github.com/cisagov/ScubaGear/releases. This notification can be disabled by setting `$env:SCUBAGEAR_SKIP_VERSION_CHECK = `$true before running ScubaGear."
$CurrentVersion = [System.Version]$ScubaManifest.ModuleVersion
$LatestVersion = [System.Version]$(Invoke-RestMethod -Uri "https://api.github.com/repos/cisagov/ScubaGear/releases/latest" -ErrorAction 'Stop').tag_name.TrimStart("v")
if ($CurrentVersion -lt $LatestVersion) {
Write-Warning "A newer version of ScubaGear ($latestVersion) is available. Please consider updating at: https://github.com/cisagov/ScubaGear/releases. This notification can be disabled by setting `$env:SCUBAGEAR_SKIP_VERSION_CHECK = `$true before running ScubaGear."
}
}

# Do the version check if the skip envvar is not defined.
if ([string]::IsNullOrWhiteSpace($env:SCUBAGEAR_SKIP_VERSION_CHECK)) {
try {
Invoke-CheckScubaGearVersionPSGallery -ErrorAction 'Stop'
Invoke-CheckScubaGearVersion -ErrorAction 'Stop'
}
catch {
Write-Warning "The ScubaGear version check failed to execute. This notification can be disabled by setting `$env:SCUBAGEAR_SKIP_VERSION_CHECK = `$true.`n$($_.Exception.Message)`n$($_.ScriptStackTrace)"
Expand Down

0 comments on commit fb47483

Please sign in to comment.