-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
231 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
function Handle-Error { | ||
param ( | ||
[string]$Message | ||
) | ||
Write-Host "Error: $Message" -ForegroundColor Red | ||
exit 1 | ||
} | ||
|
||
Write-Host "Go Package Manager Updater" | ||
Write-Host "" | ||
|
||
# Detect the operating system | ||
$OS = "windows" | ||
|
||
Write-Host "Your operating system: $OS" | ||
|
||
# Detect the system architecture | ||
$ARCH = $null | ||
switch ([System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture) { | ||
"X64" { $ARCH = "amd64" } | ||
"X86" { $ARCH = "386" } | ||
"Arm" { $ARCH = "arm" } | ||
"Arm64" { $ARCH = "arm64" } | ||
default { | ||
$ARCH = "unknown" | ||
Handle-Error "Unknown architecture detected!" | ||
} | ||
} | ||
|
||
$ARCH = "386" | ||
|
||
Write-Host "Your architecture: $ARCH" | ||
Write-Host "" | ||
|
||
Write-Host "Finding download URL and SHA256 URL ..." | ||
|
||
# Fetch the latest release info | ||
try { | ||
$LatestReleaseInfo = Invoke-RestMethod -Uri "https://api.github.com/repos/mkloubert/go-package-manager/releases/latest" | ||
} catch { | ||
Handle-Error "Could not fetch release infos" | ||
} | ||
|
||
$DownloadUrl = $LatestReleaseInfo.assets | Where-Object { | ||
$_.browser_download_url -match "gpm" -and | ||
$_.browser_download_url -match $OS -and | ||
$_.browser_download_url -match $ARCH -and | ||
$_.browser_download_url -notmatch "sha256" | ||
} | Select-Object -ExpandProperty browser_download_url | ||
|
||
$Sha256Url = $LatestReleaseInfo.assets | Where-Object { | ||
$_.browser_download_url -match "gpm" -and | ||
$_.browser_download_url -match $OS -and | ||
$_.browser_download_url -match $ARCH -and | ||
$_.browser_download_url -match "sha256" | ||
} | Select-Object -ExpandProperty browser_download_url | ||
|
||
if (-not $DownloadUrl) { | ||
Handle-Error "No valid download URL found" | ||
} | ||
if (-not $Sha256Url) { | ||
Handle-Error "No valid SHA256 URL found" | ||
} | ||
|
||
Write-Host "Downloading zip file from '$DownloadUrl'..." | ||
try { | ||
Invoke-WebRequest -Uri $DownloadUrl -OutFile "gpm.zip" | ||
} catch { | ||
Handle-Error "Failed to download zip file" | ||
} | ||
|
||
Write-Host "Downloading SHA256 file from '$Sha256Url'..." | ||
try { | ||
Invoke-WebRequest -Uri $Sha256Url -OutFile "gpm.zip.sha256" | ||
} catch { | ||
Handle-Error "Failed to download SHA256 file" | ||
} | ||
|
||
Write-Host "Verifying zip file ..." | ||
try { | ||
$Sha256Value = Get-Content "gpm.zip.sha256" | ||
$CalculatedHash = (Get-FileHash -Path "gpm.zip" -Algorithm SHA256).Hash | ||
if ($Sha256Value -ne $CalculatedHash) { | ||
Handle-Error "SHA256 verification failed" | ||
} | ||
} catch { | ||
Handle-Error "SHA256 verification failed" | ||
} | ||
|
||
Write-Host "Extracting binary ..." | ||
try { | ||
Expand-Archive -Path "gpm.zip" -DestinationPath "gpm_extracted" -Force | ||
Move-Item -Path "gpm_extracted\gpm.exe" -Destination "gpm.exe" | ||
} catch { | ||
Handle-Error "Could not extract 'gpm.exe' binary" | ||
} | ||
|
||
# Prompt for installation directory | ||
$DefaultDestination = "C:\\Program Files\\gpm\\gpm.exe" | ||
$Destination = Read-Host "Enter the installation directory (Press Enter to use the default: $DefaultDestination)" | ||
if ([string]::IsNullOrWhiteSpace($Destination)) { | ||
$Destination = $DefaultDestination | ||
} | ||
|
||
Write-Host "Installing 'gpm.exe' to $Destination ..." | ||
try { | ||
$DestinationFolder = Split-Path -Path $Destination -Parent | ||
if (-not (Test-Path $DestinationFolder)) { | ||
New-Item -ItemType Directory -Path $DestinationFolder | ||
} | ||
Move-Item -Path gpm.exe -Destination $Destination -Force | ||
} catch { | ||
Handle-Error "Could not move 'gpm.exe' to '$Destination'" | ||
} | ||
|
||
Write-Host "Cleaning up ..." | ||
try { | ||
Remove-Item -Path "gpm.zip", "gpm.zip.sha256", "gpm_extracted" -Recurse -Force | ||
} catch { | ||
Handle-Error "Cleanups failed" | ||
} | ||
|
||
Write-Host "'gpm.exe' successfully installed or updated 👍" -ForegroundColor Green |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#!/bin/bash | ||
|
||
handle_error() { | ||
echo "Error: $1" | ||
exit 1 | ||
} | ||
|
||
echo "Go Package Manager Updater" | ||
echo "" | ||
|
||
case "$(uname -s)" in | ||
Darwin) | ||
OS="darwin" | ||
;; | ||
Linux) | ||
OS="linux" | ||
;; | ||
FreeBSD) | ||
OS="freebsd" | ||
;; | ||
OpenBSD) | ||
OS="openbsd" | ||
;; | ||
CYGWIN*|MINGW*|MSYS*|Windows_NT) | ||
OS="windows" | ||
;; | ||
*) | ||
OS="unknown" | ||
echo "Error: Unknown operating system detected!" | ||
exit 1 | ||
;; | ||
esac | ||
|
||
echo "Your operating system: $OS" | ||
|
||
# Detect the system architecture | ||
case "$(uname -m)" in | ||
x86_64) | ||
ARCH="amd64" | ||
;; | ||
i*86) | ||
ARCH="386" | ||
;; | ||
armv7*|armv6*|armv5*) | ||
ARCH="arm" | ||
;; | ||
aarch64|arm64) | ||
ARCH="arm64" | ||
;; | ||
*) | ||
ARCH="unknown" | ||
echo "Error: Unknown architecture detected!" | ||
exit 1 | ||
;; | ||
esac | ||
|
||
echo "Your architecture: $ARCH" | ||
echo "" | ||
|
||
echo "Finding download URL and SHA256 URL ..." | ||
latest_release_info=$(wget -qO- https://api.github.com/repos/mkloubert/go-package-manager/releases/latest) || handle_error "Could not fetch release infos" | ||
|
||
download_url=$(echo "$latest_release_info" | jq -r \ | ||
".assets[].browser_download_url | select(contains(\"gpm\") and contains(\"$OS\") and contains(\"$ARCH\") and (. | tostring | contains(\"sha256\") | not))") \ | ||
|| handle_error "Could not parse download URL" | ||
sha256_url=$(echo "$latest_release_info" | jq -r \ | ||
".assets[].browser_download_url | select(contains(\"gpm\") and contains(\"$OS\") and contains(\"$ARCH\") and contains(\"sha256\"))") \ | ||
|| handle_error "Could not parse SHA256 URL" | ||
|
||
if [ -z "$download_url" ]; then | ||
handle_error "No valid download URL found" | ||
fi | ||
|
||
if [ -z "$sha256_url" ]; then | ||
handle_error "No valid SHA256 URL found" | ||
fi | ||
|
||
echo "Downloading tarball from '$download_url'..." | ||
wget -q "$download_url" -O gpm.tar.gz || handle_error "Failed to download tarball" | ||
|
||
echo "Downloading SHA256 file from '$sha256_url'..." | ||
wget -q "$sha256_url" -O gpm.tar.gz.sha256 || handle_error "Failed to download SHA256 file" | ||
|
||
echo "Verifying tarball ..." | ||
shasum -a 256 gpm.tar.gz.sha256 || handle_error "SHA256 verification failed" | ||
|
||
echo "Extracting binary ..." | ||
tar -xzOf gpm.tar.gz gpm > gpm || handle_error "Could not extract 'gpm' binary" | ||
|
||
echo "Installing 'gpm' to /usr/local/bin ..." | ||
sudo mv gpm /usr/local/bin/gpm || handle_error "Could not move 'gpm' to '/usr/local/bin'" | ||
sudo chmod +x /usr/local/bin/gpm || handle_error "Could not update permissions of 'gpm' binary" | ||
|
||
echo "Cleaning up ..." | ||
rm gpm.tar.gz gpm.tar.gz.sha256 || handle_error "Cleanups failed" | ||
|
||
echo "'gpm' successfully installed or updated 👍" |