animeic-cd 1 年之前
父节点
当前提交
325a70d904
共有 6 个文件被更改,包括 189 次插入104 次删除
  1. 5 0
      config.json
  2. 25 96
      main.go
  3. 80 0
      processtmp.go
  4. 0 7
      utils/readFile.go
  5. 11 0
      utils/utils.go
  6. 68 1
      utils/zip.go

+ 5 - 0
config.json

@@ -0,0 +1,5 @@
+{
+    "version": "1.0.0",
+    "url": "http://127.0.0.1:8085/cr/version/latest",
+    "exePath": "data/CR.exe"
+}

+ 25 - 96
main.go

@@ -3,120 +3,49 @@ package main
 import (
 	"cr-launcher/utils"
 	"fmt"
-	"math/rand"
 	"os/exec"
-	"time"
-
-	"github.com/fatih/color"
-	"github.com/vbauerster/mpb/v8"
-	"github.com/vbauerster/mpb/v8/decor"
+	"path"
 )
 
 func main() {
 
-	// fmt.Println("xxxxxxxxxxxxx")
-	// http 获取版本信息
-	// 比较远程版本与本地版本
-	// http下载文件,更新版本信息
-
-	// 解压
-	// err := utils.WriteConfig(&utils.Config{
-	// 	Version: "2.0.0",
-	// 	Url:     "http://www.baidu.com",
-	// })
-	// fmt.Println(err)
-	// fmt.Println(config)
-	// fmt.Println(path.Base("http://www.dddfd.com/windows.zip"))
-	version, err := utils.GetLatest("http://127.0.0.1:8085/cr/version/latest")
-	config, _ := utils.ReadConfig()
-	if config == nil {
-		// fmt.Println(err)
+	config, err := utils.ReadConfig()
+	if err != nil {
 		fmt.Println("配置文件错误")
 		return
 	}
+	url := ""
+	if config != nil {
+		url = config.Url
+	}
+
+	version, err := utils.GetLatest(url)
+	if err != nil {
+		fmt.Println("获取版本信息错误")
+		return
+	}
+
 	// 服务器最新版本有变动 更新
 	if version.Version != config.Version {
-		// todo
+		// 下载
+		utils.DownloadZip(version.Url)
+		// 解压
 		fmt.Println("更新")
+		fileName := path.Base(version.Url)
+		err := utils.Unzip(fileName)
+		if err != nil {
+			fmt.Println(err)
+			return
+		}
 	}
 	// 运行应用程序
-	cmd := exec.Command("data/cr.exe")
+	cmd := exec.Command(config.ExePath)
 	data, err := cmd.Output()
 	if err != nil {
 		fmt.Println(err)
 		fmt.Println("启动失败")
 		return
 	}
-	fmt.Println(data)
+	fmt.Println(string(data))
 	fmt.Println("启动成功")
-
-	fmt.Println(err)
-	fmt.Println(config)
-
-	// to support color in Windows following both options are required
-	p := mpb.New(
-		mpb.WithOutput(color.Output),
-		mpb.WithAutoRefresh(),
-	)
-
-	red, green := color.New(color.FgRed), color.New(color.FgGreen)
-
-	fmt.Println("应用更新中...")
-	task := "windows.zip"
-	queue := make([]*mpb.Bar, 2)
-	queue[0] = p.AddBar(rand.Int63n(201)+100,
-		mpb.PrependDecorators(
-			decor.Name(task, decor.WC{C: decor.DindentRight | decor.DextraSpace}),
-			decor.Name("下载中:", decor.WCSyncSpaceR),
-			decor.CountersNoUnit("%d / %d", decor.WCSyncWidth),
-		),
-		mpb.AppendDecorators(
-			decor.OnComplete(decor.Percentage(decor.WC{W: 5}), "done"),
-		),
-	)
-	queue[1] = p.AddBar(rand.Int63n(101)+100,
-		mpb.BarQueueAfter(queue[0]), // this bar is queued
-		mpb.BarFillerClearOnComplete(),
-		mpb.PrependDecorators(
-			decor.Name(task, decor.WC{C: decor.DindentRight | decor.DextraSpace}),
-			decor.OnCompleteMeta(
-				decor.OnComplete(
-					decor.Meta(decor.Name("安装中:", decor.WCSyncSpaceR), toMetaFunc(red)),
-					"更新完成!",
-				),
-				toMetaFunc(green),
-			),
-			decor.OnComplete(decor.EwmaETA(decor.ET_STYLE_MMSS, 0, decor.WCSyncWidth), ""),
-		),
-		mpb.AppendDecorators(
-			decor.OnComplete(decor.Percentage(decor.WC{W: 5}), ""),
-		),
-	)
-
-	go func() {
-		for _, b := range queue {
-			complete(b)
-		}
-	}()
-
-	p.Wait()
-
-}
-
-func complete(bar *mpb.Bar) {
-	max := 100 * time.Millisecond
-	for !bar.Completed() {
-		// start variable is solely for EWMA calculation
-		// EWMA's unit of measure is an iteration's duration
-		start := time.Now()
-		time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
-		// we need to call EwmaIncrement to fulfill ewma decorator's contract
-		bar.EwmaIncrInt64(rand.Int63n(5)+1, time.Since(start))
-	}
-}
-
-func toMetaFunc(c *color.Color) func(string) string {
-	return func(s string) string {
-		return c.Sprint(s)
-	}
 }

+ 80 - 0
processtmp.go

@@ -0,0 +1,80 @@
+package main
+
+import (
+	"fmt"
+	"math/rand"
+	"time"
+
+	"github.com/fatih/color"
+	"github.com/vbauerster/mpb/v8"
+	"github.com/vbauerster/mpb/v8/decor"
+)
+
+func process() {
+
+	// to support color in Windows following both options are required
+	p := mpb.New(
+		mpb.WithOutput(color.Output),
+		mpb.WithAutoRefresh(),
+	)
+
+	red, green := color.New(color.FgRed), color.New(color.FgGreen)
+
+	fmt.Println("应用更新中...")
+	task := "windows.zip"
+	queue := make([]*mpb.Bar, 2)
+	queue[0] = p.AddBar(rand.Int63n(201)+100,
+		mpb.PrependDecorators(
+			decor.Name(task, decor.WC{C: decor.DindentRight | decor.DextraSpace}),
+			decor.Name("下载中:", decor.WCSyncSpaceR),
+			decor.CountersNoUnit("%d / %d", decor.WCSyncWidth),
+		),
+		mpb.AppendDecorators(
+			decor.OnComplete(decor.Percentage(decor.WC{W: 5}), "done"),
+		),
+	)
+	queue[1] = p.AddBar(rand.Int63n(101)+100,
+		mpb.BarQueueAfter(queue[0]), // this bar is queued
+		mpb.BarFillerClearOnComplete(),
+		mpb.PrependDecorators(
+			decor.Name(task, decor.WC{C: decor.DindentRight | decor.DextraSpace}),
+			decor.OnCompleteMeta(
+				decor.OnComplete(
+					decor.Meta(decor.Name("安装中:", decor.WCSyncSpaceR), toMetaFunc(red)),
+					"更新完成!",
+				),
+				toMetaFunc(green),
+			),
+			decor.OnComplete(decor.EwmaETA(decor.ET_STYLE_MMSS, 0, decor.WCSyncWidth), ""),
+		),
+		mpb.AppendDecorators(
+			decor.OnComplete(decor.Percentage(decor.WC{W: 5}), ""),
+		),
+	)
+
+	go func() {
+		for _, b := range queue {
+			complete(b)
+		}
+	}()
+
+	p.Wait()
+}
+
+func complete(bar *mpb.Bar) {
+	max := 100 * time.Millisecond
+	for !bar.Completed() {
+		// start variable is solely for EWMA calculation
+		// EWMA's unit of measure is an iteration's duration
+		start := time.Now()
+		time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
+		// we need to call EwmaIncrement to fulfill ewma decorator's contract
+		bar.EwmaIncrInt64(rand.Int63n(5)+1, time.Since(start))
+	}
+}
+
+func toMetaFunc(c *color.Color) func(string) string {
+	return func(s string) string {
+		return c.Sprint(s)
+	}
+}

+ 0 - 7
utils/readFile.go

@@ -8,13 +8,6 @@ import (
 	"os"
 )
 
-var ConfigPath = "config.json"
-
-type Config struct {
-	Version string
-	Url     string
-}
-
 func ReadConfig() (*Config, error) {
 	conf, err := os.Open(ConfigPath)
 	if err != nil {

+ 11 - 0
utils/utils.go

@@ -0,0 +1,11 @@
+package utils
+
+var ConfigPath = "config.json"
+var appPath = "data"
+var unzipTmp = "data-tmp"
+
+type Config struct {
+	Version string
+	Url     string
+	ExePath string
+}

+ 68 - 1
utils/zip.go

@@ -1,7 +1,74 @@
 package utils
 
+import (
+	"archive/zip"
+	"errors"
+	"fmt"
+	"io"
+	"os"
+	"path/filepath"
+)
+
 // 解压文件
+func Unzip(src string) error {
+	err := unzipFile(src, unzipTmp)
+	if err != nil {
+		fmt.Println("解压失败:", err)
+		// 删除临时文件
+		os.RemoveAll(unzipTmp)
+		os.Remove(src)
+		return errors.New("解压失败")
+	}
+	// 替换新旧文件
+	os.RemoveAll(appPath)
+	os.Rename(unzipTmp, appPath)
+	// 删除下载的压缩文件
+	os.Remove(src)
+
+	fmt.Println("解压成功!")
+	return nil
+}
+
+func unzipFile(zipFile, destDir string) error {
+	r, err := zip.OpenReader(zipFile)
+	if err != nil {
+		return err
+	}
+	defer r.Close()
+
+	for _, f := range r.File {
+		// 构建解压后的文件路径
+		filePath := filepath.Join(destDir, f.Name)
+
+		if f.FileInfo().IsDir() {
+			// 创建目录
+			os.MkdirAll(filePath, os.ModePerm)
+			continue
+		}
+
+		// 创建父目录
+		os.MkdirAll(filepath.Dir(filePath), os.ModePerm)
+
+		// 打开zip文件中的文件
+		rc, err := f.Open()
+		if err != nil {
+			return err
+		}
+		defer rc.Close()
+
+		// 创建解压后的文件
+		outFile, err := os.Create(filePath)
+		if err != nil {
+			return err
+		}
+		defer outFile.Close()
+
+		// 将zip文件中的内容复制到解压后的文件中
+		_, err = io.Copy(outFile, rc)
+		if err != nil {
+			return err
+		}
+	}
 
-func Unzip(fileName string) error {
 	return nil
 }