-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfetch-zac.ps1
113 lines (94 loc) · 3.74 KB
/
fetch-zac.ps1
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
# Parameters
param (
[string]$repo = "openziti/ziti-console", # Replace with the target GitHub repository
[string]$version # Version number in #.#.# format, or "latest" for the newest release
)
# Variables
$apiUrl = "https://api.github.com/repos/$repo/releases"
$zacDir = $zacDir = Join-Path -Path (Get-Location) -ChildPath "zac"
# Function to download artifact
function Download-Artifact {
param (
[string]$tag,
[string]$artifactName
)
$url = "https://github.com/$repo/releases/download/$tag/$artifactName"
$outputPath = Join-Path -Path $zacDir -ChildPath "$tag.zip"
if (-not (Test-Path $zacDir)) {
New-Item -ItemType Directory -Path $zacDir | Out-Null
}
Write-Host "Downloading artifact from: $url"
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $url -OutFile $outputPath
Write-Host "Artifact saved to: $outputPath"
return $outputPath
}
# Function to unzip artifact
function Unzip-Artifact {
param (
[string]$zipPath,
[string]$extractTo
)
if (-not (Test-Path $zipPath)) {
Write-Host "Zip file not found: $zipPath"
exit 1
}
if (-not (Test-Path $extractTo)) {
New-Item -ItemType Directory -Path $extractTo | Out-Null
}
Write-Host "Extracting $zipPath to $extractTo"
$global:ProgressPreference = "SilentlyContinue"
Expand-Archive -Path $zipPath -DestinationPath $extractTo -Force | Out-Null
Write-Host "Extraction complete."
}
# Fetch releases
try {
$releases = Invoke-RestMethod -Uri $apiUrl -Headers @{ "User-Agent" = "PowerShell" }
} catch {
Write-Host "Error fetching releases: $_"
exit 1
}
if (-not $version -or $version -eq 'latest') {
# Find the latest release that matches "app-ziti-console-"
$latestRelease = $releases | Where-Object { $_.tag_name -match "app-ziti-console-" } | Select-Object -First 1
if ($latestRelease) {
Write-Host "Latest release found:"
Write-Host "Name: $($latestRelease.name)"
Write-Host "Tag: $($latestRelease.tag_name)"
Write-Host "URL: $($latestRelease.html_url)"
$artifactPath = (Download-Artifact -tag $latestRelease.tag_name -artifactName "ziti-console.zip")
echo "Artfiact downloaded to : $artifactPath"
$where = (Join-Path -Path $zacDir -ChildPath $latestRelease.tag_name.Replace("app-", ""))
Unzip-Artifact -zipPath $artifactPath -extractTo $where
Write-Host " - binding: zac"
Write-Host " options:"
Write-Host " location: ""$where"""
Write-Host " indexFile: index.html"
} else {
Write-Host "No releases matching 'app-ziti-console-' found."
}
exit 0
}
# Validate version input
if (-not ($version -match '^(\\d+)\\.(\\d+)\\.(\\d+)$')) {
Write-Host "Invalid version format. Use #.#.# (e.g., 3.7.0)."
exit 1
}
# Find matching release
$pattern = "app-ziti-console-v$version"
$matchingRelease = $releases | Where-Object { $_.tag_name -match $pattern }
if ($matchingRelease) {
Write-Host "Matching release found:"
Write-Host "Name: $($matchingRelease.name)"
Write-Host "Tag: $($matchingRelease.tag_name)"
Write-Host "URL: $($matchingRelease.html_url)"
$artifactPath = Download-Artifact -tag $matchingRelease.tag_name -artifactName "ziti-console.zip"
$where = (Join-Path -Path $zacDir -ChildPath $matchingRelease.tag_name.Replace("app-", ""))
Unzip-Artifact -zipPath $artifactPath -extractTo $where
Write-Host " - binding: zac"
Write-Host " options:"
Write-Host " location: ""$where"""
Write-Host " indexFile: index.html"
} else {
Write-Host "No matching release found for pattern: $pattern"
}