From 14ff2efa3d0caf7967d715b14bfde3d2e5104960 Mon Sep 17 00:00:00 2001 From: CyberL1 Date: Sun, 16 Feb 2025 09:44:31 +0100 Subject: [PATCH] feat: upgrade command --- cmd/version.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++ scripts/get.ps1 | 36 ++++++++++++++++++++++++++++ scripts/get.sh | 45 ++++++++++++++++++++++++++++++++++ utils/constants.go | 6 +++++ utils/types.go | 12 ++++++++++ utils/version.go | 24 +++++++++++++++++++ 6 files changed, 183 insertions(+) create mode 100644 cmd/version.go create mode 100644 scripts/get.ps1 create mode 100644 scripts/get.sh create mode 100644 utils/constants.go create mode 100644 utils/types.go create mode 100644 utils/version.go diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..0dbf713 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,60 @@ +package cmd + +import ( + "fmt" + "holesail-proxy/utils" + "os" + "os/exec" + "runtime" + + "github.com/spf13/cobra" +) + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Displays your CLI version", + Run: version, +} + +var upgradeCmd = &cobra.Command{ + Use: "upgrade", + Short: "Upgrades your CLI version", + Run: upgrade, +} + +func init() { + rootCmd.AddCommand(versionCmd) + versionCmd.AddCommand(upgradeCmd) +} + +func version(cmd *cobra.Command, args []string) { + latestRelease, _ := utils.GetLatestCliVersion() + + if utils.Version < latestRelease.TagName { + fmt.Println("A new update is avaliable") + fmt.Println("Run 'holesail-proxy version upgrade' to upgrade") + } + fmt.Println("Your CLI Version:", utils.Version) + fmt.Println("Latest CLI version:", latestRelease.TagName) +} + +func upgrade(cmd *cobra.Command, args []string) { + var command string + var cmdArgs []string + + switch runtime.GOOS { + case "linux", "darwin": + command = "sh" + cmdArgs = []string{"-c", "curl -fsSL https://raw.githubusercontent.com/CyberL1/holesail-proxy/main/scripts/get.sh | sh"} + + case "windows": + command = "powershell" + cmdArgs = []string{"irm https://raw.githubusercontent.com/CyberL1/holesail-proxy/main/scripts/get.ps1 | iex"} + } + + execCmd := exec.Command(command, cmdArgs...) + execCmd.Stderr = os.Stderr + execCmd.Stdin = os.Stdin + execCmd.Stdout = os.Stdout + execCmd.Run() +} diff --git a/scripts/get.ps1 b/scripts/get.ps1 new file mode 100644 index 0000000..537070a --- /dev/null +++ b/scripts/get.ps1 @@ -0,0 +1,36 @@ +#!/usr/bin/env pwsh + +$ErrorActionPreference = 'Stop' + +$BinPath = "${Home}\.holesail-proxy\bin" +$Zip = "$RuntimerPath\holesail-proxy.zip" +$Exe = "$RuntimerPath\holesail-proxy.exe" +$OldExe = "$env:Temp\holesail-proxy-old.exe" + +$Target = "windows-amd64" + +$DownloadUrl = "https://github.com/CyberL1/holesail-proxy/releases/latest/download/holesal-proxy-${Target}.zip" + +if (!(Test-Path $BinPath)) { + New-Item $BinPath -ItemType Directory | Out-Null +} + +curl.exe -Lo $Zip $DownloadUrl + +if (Test-Path $Exe) { + Move-Item -Path $Exe -Destination $OldExe -Force +} + +Expand-Archive -LiteralPath $Zip -DestinationPath $BinPath -Force +Remove-Item $Zip + +$User = [System.EnvironmentVariableTarget]::User +$Path = [System.Environment]::GetEnvironmentVariable('Path', $User) + +if (!(";${Path};".ToLower() -like "*;${BinPath};*".ToLower())) { + [System.Environment]::SetEnvironmentVariable('Path', "${Path};${BinPath}", $User) + $Env:Path += ";${BinPath}" +} + +Write-Output "Holesail proxy was installed to $Exe" +Write-Output "Run 'holesail-proxy up' to get started" diff --git a/scripts/get.sh b/scripts/get.sh new file mode 100644 index 0000000..fde42e4 --- /dev/null +++ b/scripts/get.sh @@ -0,0 +1,45 @@ +#!/bin/sh + +set -e + +if ! command -v unzip > /dev/null; then + echo "Error: Unzip is required to install Holesail proxy" + exit 1 +fi + +dir="$HOME/.holesail-proxy/bin" +zip="$dir/holesail-proxy.zip" +exe="$dir/holesail-proxy" + +if [ "$OS" = "Windows_NT" ]; then + target="windows" +else + case $(uname -sm) in + "Darwin x86_64") target="darwin-amd64" ;; + "Darwin arm64") target="dawin-arm64" ;; + "Linux aarch64") target="linux-arm64" ;; + *) target="linux-amd64" + esac +fi + +download_url="https://github.com/CyberL1/holesail-proxy/releases/latest/download/holesail-proxy-${target}.zip" + +if [ ! -d $dir ]; then + mkdir -p $dir +fi + +curl --fail --location --progress-bar --output $zip $download_url +unzip -d $dir -o $zip +chmod +x $exe +rm $zip + +echo "Holesail proxy was installed to $runtimer_exe" +if command -v holesail-proxy > /dev/null; then + echo "Run 'holesail-proxy --help' to get started" +else + case $SHELL in + /bin/zsh) shell_profile=".zshrc" ;; + *) shell_profile=".bashrc" ;; + esac + echo "export PATH=\"$dir:\$PATH\"" >> $HOME/$shell_profile +fi diff --git a/utils/constants.go b/utils/constants.go new file mode 100644 index 0000000..13962ba --- /dev/null +++ b/utils/constants.go @@ -0,0 +1,6 @@ +package utils + +var ( + Version string + GithubReleaseUrl = "https://api.github.com/repos/CyberL1/holesail-proxy/releases/latest" +) diff --git a/utils/types.go b/utils/types.go new file mode 100644 index 0000000..84e8f5d --- /dev/null +++ b/utils/types.go @@ -0,0 +1,12 @@ +package utils + +type GithubRelease struct { + TagName string `json:"tag_name"` + Prerelease bool +} + +type GithubFile struct { + Name string `json:"name"` + Path string `json:"path"` + DownloadUrl string `json:"download_url"` +} diff --git a/utils/version.go b/utils/version.go new file mode 100644 index 0000000..8005bdc --- /dev/null +++ b/utils/version.go @@ -0,0 +1,24 @@ +package utils + +import ( + "encoding/json" + "io" + "net/http" +) + +func GetLatestCliVersion() (*GithubRelease, error) { + resp, err := http.Get(GithubReleaseUrl) + if err != nil { + return nil, err + } + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + release := &GithubRelease{} + err = json.Unmarshal(body, release) + if err != nil { + return nil, err + } + return release, nil +}