# Installs the saboura command line tool on Windows (x64 and arm64). # # irm https://saboura.site/install.ps1 | iex # # Settings, all optional: # $env:SABOURA_INSTALL_DIR where to put the tool (default: $env:LOCALAPPDATA\Programs\saboura) # $env:SABOURA_VERSION install this version, such as 1.2.3, instead of the latest # $env:SABOURA_BASE_URL where releases live (default: https://app.saboura.site) # # The tool is checked against its published SHA-256 before it is installed, and its folder is # added to your user PATH. No administrator rights are needed. The script only defines functions # until its last line runs Install-Saboura, so a cut off download runs nothing. function Get-SabouraArch { # The processor of Windows itself, even when this PowerShell runs emulated. $name = $null try { $name = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString() } catch { $name = $null } if (-not $name) { $name = if ($env:PROCESSOR_ARCHITEW6432) { $env:PROCESSOR_ARCHITEW6432 } else { $env:PROCESSOR_ARCHITECTURE } } switch -Regex ("$name") { '^(X64|AMD64)$' { return 'x64' } '^(Arm64|ARM64)$' { return 'arm64' } default { throw "saboura does not support the '$name' processor yet. It needs x64 or arm64." } } } function Test-SabouraVersion([string] $Version) { return $Version -match '^[0-9]+\.[0-9]+\.[0-9]+([-+][0-9A-Za-z.-]+)?$' } function Test-SabouraName([string] $Name) { return $Name -match '^[A-Za-z0-9][A-Za-z0-9._-]*$' } function Test-SabouraSha256([string] $Hash) { return $Hash -match '^[0-9A-Fa-f]{64}$' } # The file for this system from latest.json: @{ Version; Name; Sha256 }. function Find-SabouraLatest([string] $Json, [string] $Arch) { $latest = $Json | ConvertFrom-Json if (-not (Test-SabouraVersion "$($latest.version)")) { throw 'The saboura release list is not valid.' } $file = @($latest.files | Where-Object { $_.os -eq 'windows' -and $_.arch -eq $Arch })[0] if (-not $file) { throw "There is no saboura build for Windows $Arch yet." } return @{ Version = "$($latest.version)"; Name = "$($file.name)"; Sha256 = "$($file.sha256)" } } # The file for this system from a SHA256SUMS list, by the windows and arch words in its name. function Find-SabouraSum([string] $Sums, [string] $Version, [string] $Arch) { $pattern = "^([0-9A-Fa-f]{64})\s+\*?(([^\s]*[-_.])?windows[-_.]([^\s]*[-_.])?$Arch([-_.][^\s]*)?)$" foreach ($line in ($Sums -split "`r?`n")) { if ($line.Trim() -match $pattern) { return @{ Version = $Version; Name = $Matches[2]; Sha256 = $Matches[1] } } } throw "Version $Version has no build for Windows $Arch." } function Get-SabouraText([string] $Url) { $response = Invoke-WebRequest -Uri $Url -UseBasicParsing if ($response.Content -is [byte[]]) { return [System.Text.Encoding]::UTF8.GetString($response.Content) } return "$($response.Content)" } # Downloads, checks and installs the tool into $Dir. Returns the installed path. function Install-SabouraFile([string] $BaseUrl, [string] $Arch, [string] $Dir, [string] $Pinned) { if ($Pinned) { if (-not (Test-SabouraVersion $Pinned)) { throw "SABOURA_VERSION must look like 1.2.3, not '$Pinned'." } try { $sums = Get-SabouraText "$BaseUrl/cli/$Pinned/SHA256SUMS" } catch { throw "Version $Pinned was not found at $BaseUrl." } $release = Find-SabouraSum $sums $Pinned $Arch } else { try { $json = Get-SabouraText "$BaseUrl/cli/latest.json" } catch { throw "Could not read the latest version from $BaseUrl/cli/latest.json." } $release = Find-SabouraLatest $json $Arch } if (-not (Test-SabouraName $release.Name)) { throw "The release list names a file that is not valid: '$($release.Name)'." } if (-not (Test-SabouraSha256 $release.Sha256)) { throw "The release list has no valid SHA-256 for $($release.Name)." } Write-Host "Downloading saboura $($release.Version) for Windows $Arch" $temp = Join-Path ([System.IO.Path]::GetTempPath()) ("saboura-" + [guid]::NewGuid().ToString('N')) New-Item -ItemType Directory -Path $temp -Force | Out-Null try { $download = Join-Path $temp $release.Name try { Invoke-WebRequest -Uri "$BaseUrl/cli/$($release.Version)/$($release.Name)" -OutFile $download -UseBasicParsing } catch { throw "Could not download $($release.Name)." } $actual = (Get-FileHash -Algorithm SHA256 -LiteralPath $download).Hash.ToLowerInvariant() $expected = $release.Sha256.ToLowerInvariant() if ($actual -ne $expected) { throw "The download does not match its SHA-256, so it was not installed.`n expected $expected`n got $actual" } New-Item -ItemType Directory -Path $Dir -Force | Out-Null $target = Join-Path $Dir 'saboura.exe' # Copy next to the target first, then rename, so a working tool is never half replaced. $staged = Join-Path $Dir ".saboura.$PID.exe" Copy-Item -LiteralPath $download -Destination $staged -Force Move-Item -LiteralPath $staged -Destination $target -Force Write-Host "Installed saboura $($release.Version) to $target" return $target } finally { Remove-Item -LiteralPath $temp -Recurse -Force -ErrorAction SilentlyContinue } } # Adds $Dir to the user PATH (no administrator rights) and to this window's PATH. function Add-SabouraPath([string] $Dir) { $userPath = [Environment]::GetEnvironmentVariable('Path', 'User') $parts = @("$userPath" -split ';' | Where-Object { $_ }) if ($parts -notcontains $Dir) { [Environment]::SetEnvironmentVariable('Path', (($parts + $Dir) -join ';'), 'User') Write-Host "Added $Dir to your user PATH. Open a new terminal to use it everywhere." } if (@($env:Path -split ';') -notcontains $Dir) { $env:Path = "$env:Path;$Dir" } } function Install-Saboura { $ErrorActionPreference = 'Stop' $ProgressPreference = 'SilentlyContinue' try { if ($PSVersionTable.PSEdition -eq 'Core' -and -not $IsWindows) { throw 'This script is for Windows. On macOS and Linux run: curl -fsSL https://saboura.site/install.sh | sh' } # Older Windows PowerShell may still default to TLS 1.0. try { [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 } catch { Write-Verbose 'TLS 1.2 is already on.' } $baseUrl = if ($env:SABOURA_BASE_URL) { $env:SABOURA_BASE_URL.TrimEnd('/') } else { 'https://app.saboura.site' } $dir = if ($env:SABOURA_INSTALL_DIR) { $env:SABOURA_INSTALL_DIR } elseif ($env:LOCALAPPDATA) { Join-Path $env:LOCALAPPDATA 'Programs\saboura' } else { throw 'LOCALAPPDATA is not set. Set SABOURA_INSTALL_DIR to the folder to install into.' } $pinned = "$env:SABOURA_VERSION".TrimStart('v') $arch = Get-SabouraArch Install-SabouraFile $baseUrl $arch $dir $pinned | Out-Null Add-SabouraPath $dir Write-Host 'Next: saboura login' } catch { # Write-Host, not exit: exit would close the window that ran irm | iex. Write-Host "saboura install: $($_.Exception.Message)" -ForegroundColor Red } } Install-Saboura