chore: refactor install scripts to be actual scripts and simplify approach

This commit is contained in:
Slug-Boi
2024-12-01 22:18:43 +01:00
parent 73c9adf908
commit 495606d798
7 changed files with 107 additions and 142 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
-127
View File
@@ -1,127 +0,0 @@
package main
import (
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
)
func main() {
var cmd *exec.Cmd
// Check which os being run
if runtime.GOOS == "windows" {
cmd = exec.Command("where", "cocommit")
} else {
cmd = exec.Command("which", "cocommit")
}
_, err := cmd.Output()
if err != nil {
download()
} else {
download()
//update()
}
}
func cleanup() {
fmt.Println("Removing cocommit.tar.gz")
os.Remove("cocommit.tar.gz")
}
func download() {
var resp *http.Response
var err error
var cmd *exec.Cmd
// Download the latest release
filename := "cocommit.tar.gz"
switch runtime.GOOS {
case "darwin":
fmt.Println("Downloading mac version")
if runtime.GOARCH == "amd64" {
resp, err = http.Get("https://github.com/Slug-Boi/cocommit/releases/latest/download/cocommit-darwin-x86_64.tar.gz")
} else {
resp, err = http.Get("https://github.com/Slug-Boi/cocommit/releases/latest/download/cocommit-darwin-aarch64.tar.gz")
}
cmd = exec.Command("tar", "-xvf", filename)
case "windows":
fmt.Println("Downloading windows version")
resp, err = http.Get("https://github.com/Slug-Boi/cocommit/releases/latest/download/cocommit-win.tar.gz")
cmd = exec.Command("tar", "-xvf", filename)
default:
fmt.Println("Downloading linux version")
resp, err = http.Get("https://github.com/Slug-Boi/cocommit/releases/latest/download/cocommit-linux.tar.gz")
cmd = exec.Command("tar", "-xvf", filename)
}
if err != nil {
fmt.Println("Error downloading file")
}
// Create the file
file, err := os.Create("cocommit.tar.gz")
if err != nil {
fmt.Println("Error creating file")
}
defer cleanup()
defer file.Close()
defer resp.Body.Close()
_, err = io.Copy(file, resp.Body)
if err != nil {
fmt.Println("Error copying file")
}
// Extract the file
err = cmd.Run()
if err != nil {
panic("Error extracting file")
}
regExp := regexp.MustCompile("cocommit-.+")
// Find the correct binary
var new_binary string
err = filepath.Walk("./", func(path string, info os.FileInfo, err error) error {
if err == nil && regExp.MatchString(info.Name()) {
new_binary = info.Name()
return nil
}
return nil
})
if err != nil {
panic(err)
}
// Move the file to the correct path
var input string
fmt.Println("Cocommit default install location (/usr/local/bin/cocommit?):")
fmt.Scanln(&input)
if input == "" {
input = "/usr/local/bin/cocommit"
}
if new_binary != "" {
err = os.Rename(new_binary, input)
}
fmt.Println("Cocommit cli tool installed successfully")
// Cleanup
cleanup()
}
func update() {
cmd := exec.Command("cocommit", "update")
err := cmd.Run()
if err != nil {
fmt.Println("Error updating")
}
}
+61 -6
View File
@@ -1,8 +1,63 @@
$binaryUrl = "https://github.com/Slug-Boi/cocommit/blob/main/installer/bin/install-win" # Determine the OS and architecture
$outputPath = "install-win.exe"
# Download the binary # Set up a cleanup function to be triggered upon script exit
Invoke-WebRequest -Uri $binaryUrl -OutFile $outputPath function Cleanup {
Remove-Item -ErrorAction SilentlyContinue "cocommit.tar.gz"
Remove-Item -ErrorAction SilentlyContinue "author.txt"
if ($file) {
Remove-Item -ErrorAction SilentlyContinue $file
}
}
# Run the binary trap { Cleanup } EXIT
& .\$outputPath
$OS = (Get-CimInstance Win32_OperatingSystem).Caption
$ARCH = (Get-CimInstance Win32_Processor).Architecture
$file = ""
$url = "https://github.com/Slug-Boi/cocommit/releases/latest/download/"
# Set the download URL based on the OS and architecture
if ($OS -match "Windows") {
$URL = "${url}cocommit-win.tar.gz"
$file = "cocommit.exe"
} else {
Write-Host "Unsupported OS: $OS"
exit 1
}
# Download and run the script
Invoke-WebRequest -Uri $URL -OutFile "cocommit.tar.gz"
if ($?) {
tar -xvzf "cocommit.tar.gz"
Remove-Item "cocommit.tar.gz"
Remove-Item "author.txt"
if ($file) {
& .\$file -v
if ($LASTEXITCODE -ne 0) {
Write-Host "Failed to extract the binary"
exit 1
}
}
} else {
Write-Host "Failed to download the file"
exit 1
}
# Move the binary to the specified directory
$target_dir = Read-Host "Enter the directory to move the binary to (default: C:\Program Files\cocommit)"
$target_dir = if ($target_dir) { $target_dir } else { "C:\Program Files\cocommit" }
if (-Not (Test-Path (Split-Path $target_dir))) {
Write-Host "Directory does not exist: $(Split-Path $target_dir)"
exit 1
}
Move-Item -Path $file -Destination $target_dir
if ($?) {
Write-Host "Binary moved to $target_dir successfully"
} else {
Write-Host "Failed to move the binary to $target_dir"
exit 1
}
+46 -9
View File
@@ -1,23 +1,36 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Determine the OS and architecture # Determine the OS and architecture
# Set up a cleanup function to be triggered upon script exit
__cleanup ()
{
rm "cocommit.tar.gz" 2>/dev/null
rm author.txt 2>/dev/null
if [ -n "$file" ]; then
rm $file 2>/dev/null
fi
}
trap __cleanup EXIT
OS=$(uname -s) OS=$(uname -s)
ARCH=$(uname -m) ARCH=$(uname -m)
file="" file=""
url="https://github.com/Slug-Boi/cocommit/raw/refs/heads/chore_install_script/installer/bin/" url="https://github.com/Slug-Boi/cocommit/releases/latest/download/"
# Set the download URL based on the OS and architecture # Set the download URL based on the OS and architecture
if [ "$OS" == "Linux" ]; then if [ "$OS" == "Linux" ]; then
URL="${url}install-linux" URL="${url}cocommit-linux.tar.gz"
file="install-linux" file="cocommit-linux"
elif [ "$OS" == "Darwin" ]; then elif [ "$OS" == "Darwin" ]; then
if [ "$ARCH" == "x86_64" ]; then if [ "$ARCH" == "x86_64" ]; then
URL="${url}install-darwin-x86_64" URL="${url}cocommit-darwin-x86_64.tar.gz"
file="install-darwin-x86_64" file="cocommit-darwin"
else else
URL="${url}install-darwin-aarch64" URL="${url}cocommit-darwin-aarch64.tar.gz"
file="install-darwin-aarch64" file="cocommit-darwin-aarch64"
fi fi
else else
echo "Unsupported OS: $OS" echo "Unsupported OS: $OS"
@@ -26,5 +39,29 @@ fi
# Download and run the script # Download and run the script
echo $file curl -L -o cocommit.tar.gz $URL && \
curl -LJO $URL && chmod +x $file && ./$file tar -xvzf cocommit.tar.gz && \
rm cocommit.tar.gz && rm author.txt && \
chmod +x $file && ./$file -v
if [ $? -ne 0 ]; then
echo "Failed to extract the binary"
exit 1
fi
# Move the binary to the current directory
read -p "Enter the directory to move the binary to (default: /usr/local/bin/cocommit): " target_dir
target_dir=${target_dir:-/usr/local/bin/cocommit}
if [ ! -d "$(dirname "$target_dir")" ]; then
echo "Directory does not exist: $(dirname "$target_dir")"
exit 1
fi
mv $file "$target_dir"
if [ $? -ne 0 ]; then
echo "Failed to move the binary to $target_dir"
exit 1
fi
echo "Binary moved to $target_dir successfully"