sunsheng 1 éve
szülő
commit
b80c4efb5a
3 módosított fájl, 100 hozzáadás és 2 törlés
  1. 17 2
      src/main.go
  2. 3 0
      src/utils/global.go
  3. 80 0
      src/utils/utils.go

+ 17 - 2
src/main.go

@@ -8,6 +8,7 @@ import (
 	"path/filepath"
 	"time"
 	"updater/download"
+	"updater/utils"
 
 	"gioui.org/app"
 	"gioui.org/io/system"
@@ -30,6 +31,7 @@ import (
 
 // Define the progress variables, a channel and a variable
 
+var pkgUrl string = "http://spu3dv1.obs.cn-east-3.myhuaweicloud.com/lancher/windows-amd64/1.0.2.zip"
 var progress float32
 var progressIncrementer chan float32
 
@@ -38,6 +40,7 @@ var GAppOption = &AppOption{}
 type AppOption struct {
 	NatsPort int    `short:"p" long:"np" description:"nats port"`
 	Url      string `short:"u" long:"url" description:"url"`
+	Upload   string `short:"d" long:"ud" description:"upload"`
 }
 
 func (o *AppOption) Parse() {
@@ -78,7 +81,7 @@ func upgradeLancherExe(url string, cb ProcesCallback) {
 	time.Sleep(time.Second * 3)
 
 	//3 安装新的lancher.exe
-	currLancherExe := filepath.Join(appExeDir, "main")
+	currLancherExe := filepath.Join(appExeDir, "lancher.exe")
 	currLancherExeback := filepath.Join(appExeDir, "lancher.exe.back")
 	err = os.Rename(currLancherExe, currLancherExeback)
 	if err != nil {
@@ -98,7 +101,7 @@ func upgradeLancherExe(url string, cb ProcesCallback) {
 
 	//4 启动lancher.exe
 	// 重启
-	cmd := exec.Command(currLancherExe, "--np=14220")
+	cmd := exec.Command(currLancherExe)
 	cmd.Stdout = os.Stdout
 	cmd.Stderr = os.Stderr
 	err = cmd.Start()
@@ -111,6 +114,8 @@ func upgradeLancherExe(url string, cb ProcesCallback) {
 	cb(1, "")
 
 	time.Sleep(time.Second)
+	os.Remove(currLancherExeback)
+	os.Remove(downLancherExe)
 
 	os.Exit(0)
 }
@@ -121,6 +126,16 @@ var installSucc = false
 func main() {
 	GAppOption.Parse()
 	fmt.Println(GAppOption)
+	// 上传编译好的执行文件
+	if len(GAppOption.Upload) > 0 {
+		zipPath, err := utils.UploadExe()
+		if err != nil {
+			fmt.Println(err)
+		}
+		// 成功后删除压缩文件
+		os.Remove(zipPath)
+		return
+	}
 
 	// // Setup a separate channel to provide ticks to increment progress
 	progressIncrementer = make(chan float32)

+ 3 - 0
src/utils/global.go

@@ -0,0 +1,3 @@
+package utils
+
+var Version = "1.0.2"

+ 80 - 0
src/utils/utils.go

@@ -0,0 +1,80 @@
+package utils
+
+import (
+	"archive/zip"
+	"errors"
+	"fmt"
+	"io"
+	"os"
+	"path/filepath"
+	"runtime"
+	"strings"
+	"updater/huawei"
+)
+
+// 上传当前exe文件
+func UploadExe() (string, error) {
+	// 获取当前执行程序
+	// G:\wk\lancher-svc\src\lancher.exe
+	appExePath, _ := os.Executable()
+	// G:/wk/lancher-svc/src/lancher.exe
+	appExePath = strings.Replace(appExePath, "\\", "/", -1)
+	// G:/wk/lancher-svc/src/
+	appExeDir, _ := filepath.Split(appExePath)
+	fmt.Println(appExeDir)
+
+	// 压缩当前执行程序
+	// 压缩单个文件
+
+	zipFile := fmt.Sprintf("%s%s.%s-%s.zip", "updater", Version, runtime.GOOS, runtime.GOARCH)
+	zipPath := fmt.Sprintf("%s%s", appExeDir, zipFile)
+	err := ZipFile(zipPath, appExePath)
+	if err != nil {
+		return "", err
+	}
+	// 获取操作系统和版本号确定上传文件名
+	obsDir := "pkg"
+	//上传压缩后的文件到obs
+	// http://spu3dv1.obs.cn-east-3.myhuaweicloud.com/pkg/updater1.0.2.windows-amd64.zip
+	huawei.InitConfig()
+	obs, err := huawei.UploadFile(zipPath, obsDir, zipFile)
+	if err != nil {
+		return "", err
+	}
+	if len(obs.Url) < 1 {
+		fmt.Println("上传zipfile错误")
+		return "", errors.New("上传zipfile错误")
+	}
+	fmt.Println("succ uploaded=>", obs.Url)
+
+	return zipPath, nil
+
+}
+
+func ZipFile(zipPath string, exePath string) error {
+	os.RemoveAll(zipPath)
+	archive, err := os.Create(zipPath)
+	if err != nil {
+		return err
+	}
+	defer archive.Close()
+	zipWriter := zip.NewWriter(archive)
+
+	f, err := os.Open(exePath)
+	if err != nil {
+		return err
+	}
+	defer f.Close()
+	_, exeFile := filepath.Split(exePath)
+
+	w, err := zipWriter.Create(exeFile)
+	if err != nil {
+		return err
+	}
+	if _, err := io.Copy(w, f); err != nil {
+		return err
+	}
+
+	zipWriter.Close()
+	return nil
+}