Skip to content

Commit

Permalink
Added auto downloader for launcher
Browse files Browse the repository at this point in the history
  • Loading branch information
tobychui committed Jan 4, 2023
1 parent 524438b commit 4ebe52e
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 4 deletions.
89 changes: 89 additions & 0 deletions download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package main

import (
"fmt"
"io"
"net/http"
"os"
"runtime"
"strings"
)

/*
Download.go
This script help download the latest release from Github, if endpoint is not specified
*/

func downloadReleaseBinary() error {
//Check if the src.txt file exists and overwrite default release if found
src := "https://github.com/tobychui/arozos/releases/latest/download/"
srcFileContent, err := os.ReadFile("src.txt")
if err == nil {
src = strings.TrimSpace(string(srcFileContent))
}

if src[len(src)-1:] != "/" {
src = src + "/"
}

fmt.Println("[LAUNCHER] Downloading release from " + src)

//Download binary
binaryTag := "arozos_" + runtime.GOOS + "_" + runtime.GOARCH
if runtime.GOOS == "windows" {
binaryTag += ".exe"
}

binaryName := "arozos"
if runtime.GOOS == "windows" {
binaryName += ".exe"
}

fmt.Println("[LAUNCHER] Downloading arozos release binary from: " + src + binaryTag)
err = downloadFile(binaryName, src+binaryTag)
if err != nil {
fmt.Println("[LAUNCHER] Trying to download binary from release page but failed: ", err.Error())
return err
}

//Download web.tar.gz
fmt.Println("[LAUNCHER] Downloading web.tar.gz from: " + src + "web.tar.gz (this might take a while)...")
err = downloadFile("web.tar.gz", src+"web.tar.gz")
if err != nil {
fmt.Println("[LAUNCHER] Trying to download binary from release page but failed: ", err.Error())
return err
}

return nil
}

func downloadFile(filepath string, url string) (err error) {

// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()

// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()

// Check server response
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", resp.Status)
}

// Writer the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}

return nil
}
17 changes: 14 additions & 3 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,20 @@ func autoDetectExecutable() string {
if fileExists(binaryExecPath) {
return binaryExecPath
} else {
fmt.Println("[LAUNCHER] Unable to detect ArozOS start binary")
os.Exit(1)
return ""
//Binary not found. Try download it
err := downloadReleaseBinary()
if err != nil {
fmt.Println("[LAUNCHER] Unable to detect ArozOS start binary")
os.Exit(1)
return ""
}

if runtime.GOOS == "windows" {
return "arozos.exe"
} else {
return "arozos"
}

}
}

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
*/

const (
launcherVersion = "1.1"
launcherVersion = "1.2"
restoreRetryCount = 3 //The number of retry before restore old version, if not working after restoreRetryCount + 1 launcher will exit
)

Expand Down

0 comments on commit 4ebe52e

Please sign in to comment.