fix: improve user feedback and error handling in installation scripts and language support

- Removed unnecessary print statement in main.go to streamline output.
- Updated language strings in lang.go to include newline characters for better formatting.
- Enhanced PowerShell installation script (install.ps1) with improved error handling and user prompts for admin rights.
- Modified shell installation script (install.sh) to provide clearer error messages and find matching assets for different architectures.
This commit is contained in:
Vaggelis kavouras 2024-12-29 00:04:28 +02:00
parent 947d11fbc6
commit 96af6471e4
4 changed files with 96 additions and 29 deletions

View File

@ -272,7 +272,6 @@ func showCompletionMessages(display *ui.Display) {
message = "操作完成!" message = "操作完成!"
} }
display.ShowInfo(message) display.ShowInfo(message)
fmt.Println()
} }
func waitExit() { func waitExit() {

View File

@ -13,7 +13,7 @@ type Language string
const ( const (
// CN represents Chinese language // CN represents Chinese language
CN Language = "cn" CN Language = "cn"
// EN represents English language // EN represents English language
EN Language = "en" EN Language = "en"
) )
@ -151,7 +151,7 @@ var texts = map[Language]TextResource{
RunAsAdmin: "请右键点击程序,选择「以管理员身份运行」", RunAsAdmin: "请右键点击程序,选择「以管理员身份运行」",
RunWithSudo: "请使用 sudo 命令运行此程序", RunWithSudo: "请使用 sudo 命令运行此程序",
SudoExample: "示例: sudo %s", SudoExample: "示例: sudo %s",
PressEnterToExit: "按回车键退出程序...", PressEnterToExit: "\n按回车键退出程序...",
SetReadOnlyMessage: "设置 storage.json 为只读模式, 这将导致 workspace 记录信息丢失等问题", SetReadOnlyMessage: "设置 storage.json 为只读模式, 这将导致 workspace 记录信息丢失等问题",
// Info messages // Info messages
@ -178,7 +178,7 @@ var texts = map[Language]TextResource{
RunAsAdmin: "Please right-click and select 'Run as Administrator'", RunAsAdmin: "Please right-click and select 'Run as Administrator'",
RunWithSudo: "Please run this program with sudo", RunWithSudo: "Please run this program with sudo",
SudoExample: "Example: sudo %s", SudoExample: "Example: sudo %s",
PressEnterToExit: "Press Enter to exit...", PressEnterToExit: "\nPress Enter to exit...",
SetReadOnlyMessage: "Set storage.json to read-only mode, which will cause issues such as lost workspace records", SetReadOnlyMessage: "Set storage.json to read-only mode, which will cause issues such as lost workspace records",
// Info messages // Info messages

View File

@ -1,9 +1,18 @@
# Auto-elevate to admin rights if not already running as admin # Auto-elevate to admin rights if not already running as admin
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { $isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
Write-Host "Requesting administrator privileges..." if (-NOT $isAdmin) {
$arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" -ExecutionFromElevated" try {
Start-Process powershell.exe -ArgumentList $arguments -Verb RunAs Write-Host "Requesting administrator privileges..." -ForegroundColor Cyan
Exit $argList = "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`""
Start-Process powershell.exe -Verb RunAs -ArgumentList $argList -Wait
exit
}
catch {
Write-Host "Failed to get admin rights. Please run as Administrator." -ForegroundColor Red
Write-Host "Press any key to exit..."
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
exit 1
}
} }
# Set TLS to 1.2 # Set TLS to 1.2
@ -24,6 +33,8 @@ function Cleanup {
trap { trap {
Write-Host "Error: $_" -ForegroundColor Red Write-Host "Error: $_" -ForegroundColor Red
Cleanup Cleanup
Write-Host "Press any key to exit..."
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
exit 1 exit 1
} }
@ -75,18 +86,31 @@ function Install-CursorModifier {
$latestRelease = Invoke-RestMethod -Uri "https://api.github.com/repos/dacrab/go-cursor-help/releases/latest" $latestRelease = Invoke-RestMethod -Uri "https://api.github.com/repos/dacrab/go-cursor-help/releases/latest"
Write-Host "Found latest release: $($latestRelease.tag_name)" -ForegroundColor Cyan Write-Host "Found latest release: $($latestRelease.tag_name)" -ForegroundColor Cyan
# Updated binary name format to match actual assets # Look for Windows binary with our architecture
$binaryName = "cursor-id-modifier_windows_$arch.exe" $possibleNames = @(
Write-Host "Looking for asset: $binaryName" -ForegroundColor Cyan "cursor-id-modifier_windows_$($arch).exe",
"cursor-id-modifier_windows_$($arch).exe.exe",
"cursor-id-modifier_Windows_$($arch).exe",
"cursor-id-modifier_Windows_$($arch).exe.exe"
)
$asset = $latestRelease.assets | Where-Object { $_.name -eq $binaryName } $asset = $null
$downloadUrl = $asset.browser_download_url foreach ($name in $possibleNames) {
Write-Host "Checking for asset: $name" -ForegroundColor Cyan
if (!$downloadUrl) { $asset = $latestRelease.assets | Where-Object { $_.name -eq $name }
Write-Host "Available assets:" -ForegroundColor Yellow if ($asset) {
$latestRelease.assets | ForEach-Object { Write-Host $_.name } Write-Host "Found matching asset: $($asset.name)" -ForegroundColor Green
throw "Could not find download URL for $binaryName" break
}
} }
if (!$asset) {
Write-Host "`nAvailable assets:" -ForegroundColor Yellow
$latestRelease.assets | ForEach-Object { Write-Host "- $($_.name)" }
throw "Could not find appropriate Windows binary for $arch architecture"
}
$downloadUrl = $asset.browser_download_url
} }
catch { catch {
Write-Host "Failed to get latest release: $_" -ForegroundColor Red Write-Host "Failed to get latest release: $_" -ForegroundColor Red
@ -94,7 +118,7 @@ function Install-CursorModifier {
} }
# Download binary # Download binary
Write-Host "Downloading latest release from $downloadUrl..." -ForegroundColor Cyan Write-Host "`nDownloading latest release..." -ForegroundColor Cyan
$binaryPath = Join-Path $TmpDir "cursor-id-modifier.exe" $binaryPath = Join-Path $TmpDir "cursor-id-modifier.exe"
if (!(Get-FileWithProgress -Url $downloadUrl -OutputFile $binaryPath)) { if (!(Get-FileWithProgress -Url $downloadUrl -OutputFile $binaryPath)) {
@ -138,8 +162,15 @@ function Install-CursorModifier {
try { try {
Install-CursorModifier Install-CursorModifier
} }
catch {
Write-Host "Installation failed: $_" -ForegroundColor Red
Cleanup
Write-Host "Press any key to exit..."
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
exit 1
}
finally { finally {
Cleanup Cleanup
Write-Host "Press any key to continue..." Write-Host "Press any key to exit..." -ForegroundColor Green
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown') $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
} }

View File

@ -20,7 +20,7 @@ detect_system() {
case "$(uname -s)" in case "$(uname -s)" in
Linux*) os="linux";; Linux*) os="linux";;
Darwin*) os="darwin";; Darwin*) os="darwin";;
*) echo "Unsupported OS"; exit 1;; *) echo -e "${RED}Unsupported OS${NC}"; exit 1;;
esac esac
case "$(uname -m)" in case "$(uname -m)" in
@ -32,7 +32,11 @@ detect_system() {
arch="arm64" arch="arm64"
[ "$os" = "darwin" ] && suffix="_apple_silicon" [ "$os" = "darwin" ] && suffix="_apple_silicon"
;; ;;
*) echo "Unsupported architecture"; exit 1;; i386|i686)
arch="x86"
[ "$os" = "darwin" ] && { echo -e "${RED}32-bit not supported on macOS${NC}"; exit 1; }
;;
*) echo -e "${RED}Unsupported architecture${NC}"; exit 1;;
esac esac
echo "$os $arch $suffix" echo "$os $arch $suffix"
@ -48,7 +52,7 @@ download() {
elif command -v wget >/dev/null 2>&1; then elif command -v wget >/dev/null 2>&1; then
wget --show-progress -q "$url" -O "$output" wget --show-progress -q "$url" -O "$output"
else else
echo "Error: curl or wget is required" echo -e "${RED}Error: curl or wget is required${NC}"
exit 1 exit 1
fi fi
} }
@ -59,12 +63,42 @@ setup_install_dir() {
if [ ! -d "$install_dir" ]; then if [ ! -d "$install_dir" ]; then
mkdir -p "$install_dir" || { mkdir -p "$install_dir" || {
echo "Failed to create installation directory" echo -e "${RED}Failed to create installation directory${NC}"
exit 1 exit 1
} }
fi fi
} }
# Find matching asset from release
find_asset() {
local json="$1"
local os="$2"
local arch="$3"
local suffix="$4"
# Try possible binary names
local binary_names=(
"cursor-id-modifier_${os}_${arch}${suffix}" # lowercase os
"cursor-id-modifier_$(tr '[:lower:]' '[:upper:]' <<< ${os:0:1})${os:1}_${arch}${suffix}" # capitalized os
)
local url=""
for name in "${binary_names[@]}"; do
echo -e "${BLUE}Looking for asset: $name${NC}"
url=$(echo "$json" | grep -o "\"browser_download_url\": \"[^\"]*${name}\"" | cut -d'"' -f4)
if [ -n "$url" ]; then
echo -e "${GREEN}Found matching asset: $name${NC}"
echo "$url"
return 0
fi
done
# If no match found, show available assets
echo -e "${YELLOW}Available assets:${NC}"
echo "$json" | grep "\"name\":" | cut -d'"' -f4
return 1
}
# Main installation function # Main installation function
main() { main() {
echo -e "${BLUE}Starting installation...${NC}" echo -e "${BLUE}Starting installation...${NC}"
@ -80,13 +114,16 @@ main() {
# Setup installation directory # Setup installation directory
setup_install_dir "$INSTALL_DIR" setup_install_dir "$INSTALL_DIR"
# Download latest release # Get latest release info
echo -e "${BLUE}Fetching latest release information...${NC}"
LATEST_URL="https://api.github.com/repos/dacrab/go-cursor-help/releases/latest" LATEST_URL="https://api.github.com/repos/dacrab/go-cursor-help/releases/latest"
BINARY_NAME="cursor-id-modifier_${OS}_${ARCH}${SUFFIX}" RELEASE_JSON=$(curl -s "$LATEST_URL")
DOWNLOAD_URL=$(curl -s "$LATEST_URL" | grep "browser_download_url.*${BINARY_NAME}" | cut -d '"' -f 4)
# Find matching asset
DOWNLOAD_URL=$(find_asset "$RELEASE_JSON" "$OS" "$ARCH" "$SUFFIX")
if [ -z "$DOWNLOAD_URL" ]; then if [ -z "$DOWNLOAD_URL" ]; then
echo -e "${RED}Error: Could not find download URL for $OS $ARCH${NC}" echo -e "${RED}Error: Could not find appropriate binary for $OS $ARCH${NC}"
exit 1 exit 1
fi fi