-
Notifications
You must be signed in to change notification settings - Fork 49
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
[SM-1130] - add install scripts #645
Changes from 16 commits
b39cc93
9d72a15
25464c8
51f1fc8
c798283
276beef
1e4a441
3160a61
83f53d3
f16cf6d
34e840e
a3ff347
c236af2
4aa9bc2
c0e67ba
b6e0b3f
0dd2fda
0c3b568
24e0e98
2dba059
035ca93
3ff5cde
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
$ErrorActionPreference = "Stop" | ||
|
||
$bwsVersion = if ($env:bwsVersion) { $env:bwsVersion } else { "0.5.0" } | ||
$installDir = [Environment]::GetFolderPath([Environment+SpecialFolder]::LocalApplicationData) | Join-Path -ChildPath "Programs" | Join-Path -ChildPath "Bitwarden" | ||
|
||
# https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-processor#properties | ||
$processorArch = (Get-CimInstance -ClassName Win32_Processor).Architecture | ||
if ($processorArch -eq 9) { | ||
$arch = "x86_64" | ||
} elseif ($processorArch -eq 12) { | ||
$arch = "aarch64" | ||
} else { | ||
throw "Unsupported architecture: $processorArch" | ||
} | ||
|
||
function Test-BwsInstallation { | ||
$existingBws = Get-Command bws -ErrorAction SilentlyContinue | ||
if ($null -ne $existingBws) { | ||
$userInput = Read-Host "bws is already installed at $($existingBws.Source). Do you want to overwrite it? (Y/N)" | ||
if ($userInput -ne "Y") { | ||
Write-Host "Installation cancelled by user." | ||
exit | ||
} | ||
} | ||
} | ||
|
||
function Invoke-BwsDownload { | ||
Write-Host "Detected architecture: $arch" | ||
|
||
$bwsUrl = "https://github.com/bitwarden/sdk/releases/download/bws-v$bwsVersion/bws-$arch-pc-windows-msvc-$bwsVersion.zip" | ||
Write-Host "Downloading bws from: $bwsUrl" | ||
$outputPath = Join-Path $env:TEMP "bws.zip" | ||
Invoke-WebRequest -Uri $bwsUrl -OutFile $outputPath | ||
return $outputPath | ||
} | ||
|
||
function Test-Checksum { | ||
param($zipPath) | ||
Write-Host "Validating checksum..." | ||
|
||
$checksumUrl = "https://github.com/bitwarden/sdk/releases/download/bws-v$bwsVersion/bws-sha256-checksums-$bwsVersion.txt" | ||
$checksumFile = Join-Path $env:TEMP "bws-checksums.txt" | ||
Invoke-WebRequest -Uri $checksumUrl -OutFile $checksumFile | ||
|
||
$expectedChecksum = (Get-Content $checksumFile | Where-Object { $_ -match "bws-$arch-pc-windows-msvc-$bwsVersion.zip" }).Split(" ")[0] | ||
$actualChecksum = (Get-FileHash -Algorithm SHA256 -Path $zipPath).Hash | ||
|
||
if ($actualChecksum -ne $expectedChecksum) { | ||
throw "Checksum validation failed. Expected: $expectedChecksum, Actual: $actualChecksum" | ||
} else { | ||
Write-Host "Checksum validation successful." | ||
} | ||
} | ||
|
||
function Install-Bws { | ||
param($zipPath) | ||
Write-Host "Installing bws..." | ||
New-Item -ItemType Directory -Force -Path $installDir | Out-Null | ||
Expand-Archive -Force $zipPath $installDir | ||
Write-Host "bws installed to $installDir" | ||
setx PATH "$env:PATH;$installDir" | ||
Write-Host "$installDir has been added to your PATH" | ||
Write-Host "Please restart your shell to use bws" | ||
} | ||
|
||
function Test-Bws { | ||
Write-Host "Checking bws..." | ||
$bwsPath = Join-Path $installDir "bws.exe" | ||
if (Test-Path $bwsPath) { | ||
Write-Host "bws is installed at $bwsPath" | ||
} else { | ||
throw "bws is not installed" | ||
} | ||
} | ||
|
||
Test-BwsInstallation | ||
$zipPath = Invoke-BwsDownload | ||
Test-Checksum -zipPath $zipPath | ||
Install-Bws -zipPath $zipPath | ||
Test-Bws |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
#!/bin/sh | ||
set -u | ||
|
||
################################################## | ||
# An installer for the bws command line utility. # | ||
################################################## | ||
|
||
BWS_VERSION="${BWS_VERSION:-0.5.0}" | ||
|
||
main() { | ||
check_required | ||
platform_detect | ||
arch_detect | ||
download_bws | ||
validate_checksum | ||
install_bws | ||
} | ||
|
||
error() { | ||
echo "$1" >&2 | ||
echo "Exiting..." >&2 | ||
exit 1 | ||
} | ||
|
||
check_required() { | ||
coltonhurst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ! command -v curl >/dev/null && ! command -v wget >/dev/null; then | ||
error "curl or wget is required to download bws." | ||
fi | ||
|
||
if ! command -v unzip >/dev/null; then | ||
error "unzip is required to install bws." | ||
fi | ||
} | ||
|
||
can_sudo() { | ||
if command -v sudo >/dev/null; then | ||
echo "Attempting to install bws with sudo. Please enter your password if prompted." | ||
if sudo -v 2>/dev/null; then | ||
echo "sudo is available and we have the necessary permissions." | ||
echo "Installing bws to /usr/local/bin..." | ||
return 0 | ||
else | ||
echo "sudo is available, but we failed to authenticate. Trying to install to your \$HOME directory..." | ||
return 1 | ||
fi | ||
else | ||
echo "sudo is not available. Trying to install to your \$HOME directory..." | ||
return 1 | ||
fi | ||
} | ||
|
||
platform_detect() { | ||
if [ "$(uname -s)" = "Linux" ]; then | ||
PLATFORM="unknown-linux-gnu" | ||
elif [ "$(uname -s)" = "Darwin" ]; then | ||
PLATFORM="apple-darwin" | ||
else | ||
error "Unsupported platform: $(uname -s)" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could potentially point people to the GitHub SDK repository if they want to create an issue for us to look into supporting their platform. |
||
fi | ||
} | ||
|
||
arch_detect() { | ||
if [ "$(uname -m)" = "x86_64" ]; then | ||
ARCH="x86_64" | ||
elif [ "$(uname -m)" = "aarch64" ]; then # Linux uname output | ||
ARCH="aarch64" | ||
elif [ "$(uname -m)" = "arm64" ]; then # Darwin uname output | ||
ARCH="aarch64" | ||
else | ||
error "Unsupported architecture: $(uname -m)" | ||
fi | ||
} | ||
|
||
downloader() { | ||
if command -v curl >/dev/null; then | ||
curl -L -o "$2" "$1" | ||
else | ||
wget -O "$2" "$1" | ||
fi | ||
} | ||
|
||
extract() { | ||
unzip -o "$1" -d "$2" | ||
} | ||
|
||
download_bws() { | ||
bws_url="https://github.com/bitwarden/sdk/releases/download/bws-v${BWS_VERSION}/bws-${ARCH}-${PLATFORM}-${BWS_VERSION}.zip" | ||
echo "Downloading bws from: $bws_url" | ||
tmp_dir="$(mktemp -d)" | ||
downloader "$bws_url" "$tmp_dir/bws.zip" | ||
} | ||
|
||
validate_checksum() { | ||
checksum_url="https://github.com/bitwarden/sdk/releases/download/bws-v${BWS_VERSION}/bws-sha256-checksums-${BWS_VERSION}.txt" | ||
echo "Downloading checksum file from: $checksum_url" | ||
checksum_file="$tmp_dir/bws-checksums.txt" | ||
downloader "$checksum_url" "$checksum_file" | ||
|
||
expected_checksum="$(grep "bws-${ARCH}-${PLATFORM}-${BWS_VERSION}.zip" "$checksum_file" | awk '{print $1}')" | ||
actual_checksum="$(sha256sum "$tmp_dir/bws.zip" | awk '{print $1}')" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this get the expected and actual checksum from the same downloaded zip? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This greps for the string Like this: bash-5.2$ grep bws-aarch64-apple-darwin-0.5.0.zip ~/Downloads/bws-sha256-checksums-0.5.0.txt
aeac01edbd7cdfb8e75e9143d13d69221e6e0bff0fd9f8de69b85c3108a4e986 bws-aarch64-apple-darwin-0.5.0.zip
bash-5.2$ grep bws-aarch64-apple-darwin-0.5.0.zip ~/Downloads/bws-sha256-checksums-0.5.0.txt | awk '{print $1}'
aeac01edbd7cdfb8e75e9143d13d69221e6e0bff0fd9f8de69b85c3108a4e986 |
||
|
||
if [ "$actual_checksum" != "$expected_checksum" ]; then | ||
error "Checksum validation failed. Expected: $expected_checksum, Actual: $actual_checksum" | ||
else | ||
echo "Checksum validation successful." | ||
fi | ||
} | ||
|
||
install_bws() { | ||
echo "Installing bws..." | ||
extract "$tmp_dir/bws.zip" "$tmp_dir" | ||
chmod +x "$tmp_dir/bws" | ||
|
||
if can_sudo; then | ||
sudo install -m 755 "$tmp_dir/bws" /usr/local/bin/bws | ||
|
||
if ! command -v bws >/dev/null; then | ||
error "Installation failed. bws was not found in /usr/local/bin" | ||
fi | ||
|
||
echo "bws installed to /usr/local/bin/bws" | ||
else | ||
user_bin_dir="${HOME}/.local/bin" | ||
mkdir -p "${user_bin_dir}" | ||
install -m 755 "$tmp_dir/bws" "${user_bin_dir}/bws" | ||
|
||
if ! command -v "${user_bin_dir}/bws" >/dev/null; then | ||
error "Installation failed. bws was not found in ${user_bin_dir}" | ||
fi | ||
|
||
echo "bws installed at ${user_bin_dir}/bws" | ||
echo "Please add ${user_bin_dir} to your PATH by adding the following line to your ~/.profile or shell rc file:" | ||
echo "export PATH=\"\$PATH:${user_bin_dir}\"" | ||
fi | ||
|
||
rm -rf "$tmp_dir" | ||
} | ||
|
||
main |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These sed expressions worked when I tested them locally, but there doesn't appear to be a way to test this in the workflow, so I would appreciate some scrutiny here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated with simpler regex matches in 2dba059.