-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall.ps1
More file actions
165 lines (141 loc) · 5.67 KB
/
install.ps1
File metadata and controls
165 lines (141 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Flashduty CLI installer for Windows
# Usage: irm https://raw.githubusercontent.com/flashcatcloud/flashduty-cli/main/install.ps1 | iex
#
# Environment variables:
# FLASHDUTY_VERSION - specific version to install (e.g. "v0.1.2")
# FLASHDUTY_INSTALL_DIR - install directory (default: $HOME\.flashduty\bin)
# MIRROR_URL - fetch release assets from this https mirror prefix
# instead of github.com. The mirror must replicate
# GitHub's release layout
# (<MIRROR_URL>/releases/download/<tag>/<asset>) and
# expose a plain-text <MIRROR_URL>/releases/latest file
# containing the latest tag.
$ErrorActionPreference = "Stop"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$Repo = "flashcatcloud/flashduty-cli"
$Binary = "flashduty-cli.exe"
$InstalledName = "flashduty.exe"
# When set, all release downloads are fetched from this prefix instead of github.com.
$MirrorUrl = $env:MIRROR_URL
if ($MirrorUrl) {
$MirrorUrl = $MirrorUrl.TrimEnd('/')
if ($MirrorUrl -notlike "https://*") {
Write-Error "[flashduty] MIRROR_URL must use https:// scheme, got: $MirrorUrl"
exit 1
}
}
function Write-Info($msg) {
Write-Host "[flashduty] $msg"
}
function Fail($msg) {
Write-Error "[flashduty] $msg"
exit 1
}
# --- detect architecture ---
function Get-Arch {
switch ($env:PROCESSOR_ARCHITECTURE) {
"AMD64" { return "x86_64" }
"ARM64" { return "arm64" }
default { Fail "unsupported architecture: $env:PROCESSOR_ARCHITECTURE" }
}
}
# --- resolve version ---
function Get-Version {
if ($env:FLASHDUTY_VERSION) {
return $env:FLASHDUTY_VERSION
}
if ($MirrorUrl) {
try {
$raw = Invoke-RestMethod -Uri "$MirrorUrl/releases/latest" -UseBasicParsing
$version = ([string]$raw).Trim()
} catch {
Fail "could not fetch $MirrorUrl/releases/latest. Set FLASHDUTY_VERSION to install a specific version."
}
} else {
try {
$release = Invoke-RestMethod -Uri "https://api.github.com/repos/$Repo/releases/latest" -UseBasicParsing
$version = $release.tag_name
} catch {
Fail "could not determine latest version. Set FLASHDUTY_VERSION to install a specific version."
}
}
# The resolved value comes from a network response and is interpolated into
# the download URL — reject anything that isn't a plain release tag.
if ($version -notmatch '^v[0-9][A-Za-z0-9.+-]*$') {
Fail "resolved version is not a valid release tag: '$version'"
}
return $version
}
# --- main ---
$Arch = Get-Arch
$Version = Get-Version
$InstallDir = if ($env:FLASHDUTY_INSTALL_DIR) {
$env:FLASHDUTY_INSTALL_DIR
} else {
Join-Path $HOME ".flashduty\bin"
}
$Archive = "flashduty-cli_Windows_${Arch}.zip"
$Base = if ($MirrorUrl) {
"$MirrorUrl/releases/download/$Version"
} else {
"https://github.com/$Repo/releases/download/$Version"
}
$Url = "$Base/$Archive"
Write-Info "Installing Flashduty CLI $Version (Windows/$Arch)"
Write-Info "Downloading $Url"
$TmpDir = Join-Path ([System.IO.Path]::GetTempPath()) "flashduty-install-$([System.Guid]::NewGuid().ToString('N'))"
New-Item -ItemType Directory -Path $TmpDir -Force | Out-Null
try {
$ArchivePath = Join-Path $TmpDir $Archive
Invoke-WebRequest -Uri $Url -OutFile $ArchivePath -UseBasicParsing
# Verify against the published checksums.txt when present. Releases cut
# before the mirror existed don't ship one, so a missing file only warns.
$ChecksumPath = Join-Path $TmpDir "checksums.txt"
try {
Invoke-WebRequest -Uri "$Base/checksums.txt" -OutFile $ChecksumPath -UseBasicParsing
} catch {
$ChecksumPath = $null
}
if ($ChecksumPath -and (Test-Path $ChecksumPath)) {
$expected = $null
foreach ($line in Get-Content $ChecksumPath) {
$parts = $line -split '\s+', 2
if ($parts.Count -eq 2 -and $parts[1].Trim() -eq $Archive) {
$expected = $parts[0].Trim().ToLower()
break
}
}
if (-not $expected) {
Fail "archive $Archive not listed in checksums.txt (wrong release or renamed asset)"
}
$actual = (Get-FileHash -Path $ArchivePath -Algorithm SHA256).Hash.ToLower()
if ($actual -ne $expected) {
Fail "checksum mismatch for ${Archive}: expected $expected, got $actual"
}
Write-Info "Checksum OK"
} else {
Write-Info "WARNING: checksums.txt not available -- skipping integrity check"
}
Expand-Archive -Path $ArchivePath -DestinationPath $TmpDir -Force
$BinaryPath = Join-Path $TmpDir $Binary
if (-not (Test-Path $BinaryPath)) {
Fail "binary '$Binary' not found in archive"
}
# Create install directory
if (-not (Test-Path $InstallDir)) {
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
}
$DestPath = Join-Path $InstallDir $InstalledName
Move-Item -Path $BinaryPath -Destination $DestPath -Force
Write-Info "Installed to $DestPath"
# Add to user PATH if not already there
$UserPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($UserPath -notlike "*$InstallDir*") {
[Environment]::SetEnvironmentVariable("Path", "$UserPath;$InstallDir", "User")
$env:Path = "$env:Path;$InstallDir"
Write-Info "Added $InstallDir to user PATH (restart your terminal for it to take effect)"
}
Write-Info "Run 'flashduty version' to verify"
} finally {
Remove-Item -Path $TmpDir -Recurse -Force -ErrorAction SilentlyContinue
}