Kaynağa Gözat

init commit

Toby Chui 3 yıl önce
ebeveyn
işleme
9f41675c03
7 değiştirilmiş dosya ile 180 ekleme ve 0 silme
  1. 0 0
      .gitignore
  2. 6 0
      autopush.bat
  3. 98 0
      fs.go
  4. 5 0
      go.mod
  5. 6 0
      go.sum
  6. BIN
      launcher.exe
  7. 65 0
      main.go

+ 0 - 0
.gitignore


+ 6 - 0
autopush.bat

@@ -0,0 +1,6 @@
+@echo off
+set /p id="Enter commit notes: "
+git pull
+git add -A
+git commit -m "%id%"
+git push

+ 98 - 0
fs.go

@@ -0,0 +1,98 @@
+package main
+
+import (
+	"errors"
+	"fmt"
+	"io"
+	"os"
+	"path/filepath"
+	"runtime"
+)
+
+//Auto detect and execute the correct binary
+func autoDetectExecutable() string {
+	if runtime.GOOS == "windows" {
+		if fileExists("arozos.exe") {
+			return "arozos.exe"
+		}
+	} else {
+		if fileExists("arozos") {
+			return "arozos"
+		}
+	}
+
+	//Not build from source. Look for release binary names
+	binaryExecPath := "arozos_" + runtime.GOOS + "_" + runtime.GOARCH
+	if runtime.GOOS == "windows" {
+		binaryExecPath += ".exe"
+	}
+
+	if fileExists(binaryExecPath) {
+		return binaryExecPath
+	} else {
+		fmt.Println("[LAUNCHER] Unable to detect ArozOS start binary")
+		os.Exit(1)
+		return ""
+	}
+}
+
+func getUpdateBinaryFilename() (string, error) {
+	updateFiles, err := filepath.Glob("./updates/*")
+	if err != nil {
+		return "", err
+	}
+
+	for _, thisFile := range updateFiles {
+		if !isDir(thisFile) && filepath.Ext(thisFile) != ".gz" {
+			//This might be the file
+			return thisFile, nil
+		}
+	}
+
+	return "", errors.New("file not found")
+}
+
+func copy(src, dst string) (int64, error) {
+	sourceFileStat, err := os.Stat(src)
+	if err != nil {
+		return 0, err
+	}
+
+	if !sourceFileStat.Mode().IsRegular() {
+		return 0, errors.New("invalid file")
+	}
+
+	source, err := os.Open(src)
+	if err != nil {
+		return 0, err
+	}
+	defer source.Close()
+
+	destination, err := os.Create(dst)
+	if err != nil {
+		return 0, err
+	}
+	defer destination.Close()
+	nBytes, err := io.Copy(destination, source)
+	return nBytes, err
+}
+
+func isDir(path string) bool {
+	fileInfo, err := os.Stat(path)
+	if err != nil {
+		return false
+	}
+
+	return fileInfo.IsDir()
+}
+
+func fileExists(name string) bool {
+	_, err := os.Stat(name)
+	if err == nil {
+		return true
+	}
+	if errors.Is(err, os.ErrNotExist) {
+		return false
+	}
+	return false
+}

+ 5 - 0
go.mod

@@ -0,0 +1,5 @@
+module imuslab.com/arozos/launcher
+
+go 1.17
+
+require github.com/otiai10/copy v1.7.0 // indirect

+ 6 - 0
go.sum

@@ -0,0 +1,6 @@
+github.com/otiai10/copy v1.7.0 h1:hVoPiN+t+7d2nzzwMiDHPSOogsWAStewq3TwU05+clE=
+github.com/otiai10/copy v1.7.0/go.mod h1:rmRl6QPdJj6EiUqXQ/4Nn2lLXoNQjFCQbbNrxgc/t3U=
+github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
+github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
+github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
+github.com/otiai10/mint v1.3.3/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=

BIN
launcher.exe


+ 65 - 0
main.go

@@ -0,0 +1,65 @@
+package main
+
+import (
+	"fmt"
+	"os"
+	"os/exec"
+	"path/filepath"
+
+	cp "github.com/otiai10/copy"
+)
+
+/*
+	ArozOS Launcher
+	For auto update and future extension purpose
+
+	Author: tobychui
+
+*/
+
+const (
+	launcherVersion = "1.0"
+)
+
+func main() {
+	//Print basic information
+	fmt.Println("[LAUNCHER] ArozOS Launcher ver " + launcherVersion)
+	binaryName := autoDetectExecutable()
+	fmt.Println("[LAUNCHER] Choosing binary executable: " + binaryName)
+
+	//Check if updates exists. If yes, overwrite it
+	if fileExists("./updates") && fileExists("./updates/web/") && fileExists("./updates/system") {
+		//All component exists. Update it
+		newArozBinary, err := getUpdateBinaryFilename()
+		if err != nil {
+			fmt.Println("[LAUNCHER] Unable to access update files: ", err.Error())
+		} else {
+			//Binary file got. Update it
+			//Backup the current executables and system files
+			fmt.Println("[LAUNCHER] Starting system backup process (to ./arozos.old)")
+			os.MkdirAll("./arozos.old", 0775)
+			copy(binaryName, filepath.Join("./arozos.old", filepath.Base(binaryName)))
+			cp.Copy("./system", "./arozos.old/system/")
+			cp.Copy("./web", "./arozos.old/web/")
+
+			//Success. Continue binary replacement
+			fmt.Println("[LAUNCHER] Copying updates to runtime environment")
+			copy(newArozBinary, binaryName)
+			cp.Copy("./updates/system", "./system/")
+			cp.Copy("./updates/web", "./web/")
+
+			fmt.Println("[LAUNCHER] Update Completed. Removing the update files")
+			os.RemoveAll("./updates/")
+		}
+
+	} else if fileExists("./updates") && (!fileExists("./updates/web/") || !fileExists("./updates/system")) {
+		//Update folder exists but some components is broken
+		fmt.Println("[LAUNCHER] Detected damaged / incomplete update package. Skipping update process")
+	}
+
+	cmd := exec.Command(binaryName, os.Args[1:]...)
+	cmd.Stdout = os.Stdout
+	cmd.Stderr = os.Stderr
+	cmd.Run()
+
+}