Skip to content

Commit

Permalink
updated update function
Browse files Browse the repository at this point in the history
  • Loading branch information
tacheraSasi committed Dec 11, 2024
1 parent e623cdb commit 73f2296
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 13 deletions.
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=0.1.3
VERSION=0.1.2

build:
make build_android
Expand All @@ -12,31 +12,31 @@ build_linux:
@echo 'shrinking binary...'
./upx --brute vint
@echo 'zipping build....'
tar -zcvf binaries/vintLang_linux_amd64_v${VERSION}.tar.gz vint
tar -zcvf binaries/vintLang_linux_amd64.tar.gz vint
@echo 'cleaning up...'
rm vint

build_windows:
@echo 'building windows executable...'
env GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -o vint_windows_amd64_v${VERSION}.exe
env GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -o vint_windows_amd64.exe
@echo 'shrinking build...'
./upx --brute binaries/vintLang_windows_amd64_v${VERSION}.exe
./upx --brute binaries/vintLang_windows_amd64.exe

build_mac:
@echo 'building mac binary...'
env GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -o vint
@echo 'shrinking binary...'
./upx --brute vint
@echo 'zipping build...'
tar -zcvf binaries/vintLang_mac_amd64_v${VERSION}.tar.gz vint
tar -zcvf binaries/vintLang_mac_amd64.tar.gz vint
@echo 'cleaning up...'
rm vint

build_android:
@echo 'building android binary'
env GOOS=android GOARCH=arm64 go build -ldflags="-s -w" -o vint
@echo 'zipping build...'
tar -zcvf binaries/vintLang_android_arm64_v${VERSION}.tar.gz vint
tar -zcvf binaries/vintLang_android_arm64.tar.gz vint
@echo 'cleaning up...'
rm vint

Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/ekilie/vint-lang/styles"
"github.com/ekilie/vint-lang/toolkit"
)

const VINT_VERSION = "0.1.2"
// Constants for styled output
var (
// Title banner for the CLI
Expand All @@ -24,7 +24,7 @@ var (
`)

// CLI metadata
Version = styles.VersionStyle.Render("v0.1.3")
Version = styles.VersionStyle.Render("v"+VINT_VERSION)
Author = styles.AuthorStyle.Render("by Tachera Sasi")

// Combined logo with title, author, and version
Expand Down
187 changes: 182 additions & 5 deletions toolkit/toolkit.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,190 @@
package toolkit

import "fmt"

import (
"archive/tar"
// "archive/zip"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"runtime"
// "strings"
)
var CLI_ARGS []string = []string{}

func GetCliArgs()[]string{// Returns the CLI_ARGS
return CLI_ARGS
}

func Update(){
fmt.Println("updating vint...")
}
type ReleaseAsset struct {
Name string `json:"name"`
BrowserDownloadURL string `json:"browser_download_url"`
}

type Release struct {
Assets []ReleaseAsset `json:"assets"`
}

func detectPlatform() string {
switch runtime.GOOS {
case "linux":
if _, err := os.Stat("/system/build.prop"); err == nil {
return "android"
}
return "linux"
case "darwin":
return "macos"
case "windows":
return "windows"
default:
return "unsupported"
}
}

func getBinaryName(platform string) string {
switch platform {
case "linux":
return "vintLang_linux_amd64.tar.gz"
case "macos":
return "vintLang_macos_amd64.tar.gz"
case "android":
return "vintLang_android_arm64.tar.gz"
case "windows":
return "vintLang_windows_amd64.zip"
default:
return ""
}
}

func fetchLatestReleaseURL(binaryName string) (string, error) {
resp, err := http.Get("https://api.github.com/repos/ekilie/vint-lang/releases/latest")
if err != nil {
return "", err
}
defer resp.Body.Close()

var release Release
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
return "", err
}

for _, asset := range release.Assets {
if asset.Name == binaryName {
return asset.BrowserDownloadURL, nil
}
}

return "", fmt.Errorf("no suitable binary found for platform")
}

func downloadFile(url, outputPath string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()

out, err := os.Create(outputPath)
if err != nil {
return err
}
defer out.Close()

_, err = io.Copy(out, resp.Body)
return err
}

func installBinary(binaryName, platform string) error {
if platform == "windows" {
cmd := exec.Command("unzip", "-o", binaryName, "-d", "C:/usr/local/bin")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

file, err := os.Open(binaryName)
if err != nil {
return err
}
defer file.Close()

gzipReader, err := gzip.NewReader(file)
if err != nil {
return err
}
defer gzipReader.Close()

tarReader := tar.NewReader(gzipReader)
for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}

if header.Typeflag == tar.TypeReg {
destFile := "/usr/local/bin/" + header.Name
outFile, err := os.Create(destFile)
if err != nil {
return err
}
defer outFile.Close()

if _, err := io.Copy(outFile, tarReader); err != nil {
return err
}

if err := os.Chmod(destFile, 0755); err != nil {
return err
}
}
}

return nil
}

func Update() {
platform := detectPlatform()
if platform == "unsupported" {
fmt.Println("Unsupported platform. Exiting.")
return
}

binaryName := getBinaryName(platform)
if binaryName == "" {
fmt.Println("No binary name mapping found for platform. Exiting.")
return
}

fmt.Println("Fetching the latest release information...")
assetURL, err := fetchLatestReleaseURL(binaryName)
if err != nil {
fmt.Printf("Error fetching release: %v\n", err)
return
}

fmt.Println("Downloading the latest release...")
if err := downloadFile(assetURL, binaryName); err != nil {
fmt.Printf("Error downloading binary: %v\n", err)
return
}

fmt.Println("Installing the new binary...")
if err := installBinary(binaryName, platform); err != nil {
fmt.Printf("Error installing binary: %v\n", err)
return
}

fmt.Println("Cleaning up...")
if err := os.Remove(binaryName); err != nil {
fmt.Printf("Error cleaning up: %v\n", err)
}

fmt.Println("Installation complete!")
}

0 comments on commit 73f2296

Please sign in to comment.