-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
129 lines (104 loc) · 3.56 KB
/
Copy pathinstall.ps1
File metadata and controls
129 lines (104 loc) · 3.56 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
<#
.SYNOPSIS
Simple vvctl installer for Windows
.DESCRIPTION
Downloads and installs the latest vvctl release (or a specific version, or latest prerelease).
#>
[CmdletBinding()]
param(
[string] $Version,
[switch] $Preview
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
function Get-WritablePathDir {
# Return the first existing, writable folder from $Env:PATH
foreach ($path in $Env:PATH -split ';') {
if ([string]::IsNullOrWhiteSpace($path)) {
continue
}
if (-not (Test-Path $path)) {
continue
}
try {
$testFile = Join-Path $path '__permtest'
New-Item -Path $testFile -ItemType File -Force -ErrorAction Stop | Out-Null
Remove-Item -Path $testFile -Force | Out-Null
return $path
} catch {
# skip paths that are not writable
continue
}
}
return $null
}
function Get-LatestReleaseTag {
param($repo, $prerelease)
$uri = if ($prerelease) {
"https://api.github.com/repos/$repo/releases"
} else {
"https://api.github.com/repos/$repo/releases/latest"
}
if (-not $prerelease) {
(Invoke-RestMethod -UseBasicParsing -Uri $uri).tag_name
} else {
(Invoke-RestMethod -UseBasicParsing -Uri $uri) |
Where-Object { $_.prerelease } |
Select-Object -First 1 |
Select-Object -ExpandProperty tag_name
}
}
function Install-vvctl {
param($Tag, $DestDir)
Write-Host "Installing vvctl $Tag to $DestDir..."
$platform = "x86_64-pc-windows-msvc"
$zipName = "vvctl-$Tag-$platform.zip"
$url = "https://github.com/ververica/vvctl/releases/download/$Tag/$zipName"
$tmpDir = Join-Path ([IO.Path]::GetTempPath()) ([Guid]::NewGuid().ToString())
New-Item -ItemType Directory -Path $tmpDir | Out-Null
$zipPath = Join-Path $tmpDir $zipName
Write-Host "Downloading $url..."
Invoke-RestMethod -UseBasicParsing -Uri $url -OutFile $zipPath
if ((Get-Item $zipPath).Length -eq 0) {
throw "Downloaded file is missing or empty"
}
Write-Host "Extracting..."
Expand-Archive -Path $zipPath -DestinationPath $tmpDir -Force
$exe = Get-ChildItem -Path $tmpDir -Filter 'vvctl.exe' -Recurse | Select-Object -First 1
if (-not $exe) {
throw "Could not locate vvctl.exe in extracted archive"
}
if (-not (Test-Path $DestDir)) {
New-Item -ItemType Directory -Path $DestDir -Force | Out-Null
}
$destExe = Join-Path $DestDir 'vvctl.exe'
Write-Host "Copying to $destExe..."
Copy-Item -Path $exe.FullName -Destination $destExe -Force
Write-Host "Cleaning up..."
Remove-Item -LiteralPath $tmpDir -Recurse -Force
Write-Host "vvctl installation complete!`nRun ` vvctl --help` to get started."
}
# --- Main ---
$repo = 'ververica/vvctl'
# Determine install directory
$InstallDir = Get-WritablePathDir
if (-not $InstallDir) {
$InstallDir = Join-Path $HOME 'bin'
Write-Warning "No writable PATH directory found. Falling back to '$InstallDir'"
}
# Determine version/tag
if ($Version) {
Write-Host "Using specified version: $Version"
$tag = $Version
}
elseif ($Preview) {
Write-Host "Fetching latest prerelease…"
$tag = Get-LatestReleaseTag -repo $repo -prerelease
Write-Host "Using latest prerelease version: $tag"
}
else {
Write-Host "Fetching latest stable release…"
$tag = Get-LatestReleaseTag -repo $repo -prerelease:$false
Write-Host "Using latest version: $tag"
}
Install-vvctl -Tag $tag -DestDir $InstallDir