提交 bafaf363 编写于 作者: yanghye's avatar yanghye

up: auto update

上级 381b5de5
### energy lib framework auto update
#### Divided into three modules
* liblcl
* enregy
* cef
#### Control whether to enable the module update based on enable
#### Field description
> first level node is the module name
>
> download: module download source
>
> url: module download source url template
>
> source: module download source, addrs
>
> sourceSelect: Download the address source selection, select the source based on the index, and replace it with the Url template
>
> enable: module update based on enable
>
> latest: module latest version
>
> versions: version list
>
>> forced: forced update, a significant update that applies to the current version
>>
>> content: update logs
---
#### update json template
```json
{
"liblcl": {
"download": {
"url": "https://{url}/energye/energy/releases/download/{version}/{OSARCH}.zip",
"source": [
"gitee.com",
"github.com"
],
"sourceSelect": 0
},
"enable": true,
"latest": "version.1",
"versions": {
"version.1": {
"content": [
"update log 1",
"update log 2"
],
"forced": false
},
"version.0": {
"content": [
""
],
"forcede": false
}
}
},
"energy": {
"download": {
"url": "",
"source": [],
"sourceSelect": 0
},
"enable": false,
"latest": "",
"versions": {
"": {
"content": [
""
],
"forcede": false
}
}
},
"cef": {
"download": {
"url": "",
"source": [
""
],
"sourceSelect": 0
},
"enable": false,
"latest": "",
"versions": {
"": {
"content": [
""
],
"forcede": false
}
}
}
}
```
---
\ No newline at end of file
......@@ -11,11 +11,49 @@
// Package autoupdate Energy check auto update
package autoupdate
import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/energye/energy/v2/cef/autoupdate/internal"
"io/ioutil"
"net/http"
"time"
)
var (
// 检查更新开关, 默认关闭
isCheckUpdate = false
)
// 更新模块
type update struct {
Liblcl model `json:"liblcl"`
Energy model `json:"energy"`
CEF model `json:"cef"`
}
// 模块
type model struct {
Latest string `json:"latest"` // 最新版本
Download download `json:"download"` // 下载源
Enable bool `json:"enable"` // 是否开启该模块更新
Versions map[string]versionInfo `json:"versions"` // 当前模块所有版本集合 key=版本, value=版本信息
}
// 更新下载源
type download struct {
Url string `json:"url"` // 下载地址模板 https://{url}/energye/energy/releases/download/{version}/{OSARCH}.zip
Source []string `json:"source"` // 下载地址源 ["gitee.com", "github.com"]
SourceSelect uint8 `json:"sourceSelect"` // 下载地址源选择, 根据下标(index)选择源(Source), 替换到(Url)模板
}
// 版本信息
type versionInfo struct {
Content []string `json:"content"` // 更新内容
Forced bool `json:"forced"` // 强制更新, 适用当前版本的启作用
}
// CheckUpdate
// 检查更新, isCheckUpdate 为true时
func CheckUpdate() {
......@@ -35,7 +73,31 @@ func UpdateLog() []string {
}
func check() {
client := &http.Client{Timeout: 5 * time.Second, Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}}
request, err := http.NewRequest("GET", internal.CheckURL, nil)
if err != nil {
println("energy check update http.NewRequest error:", err.Error())
return
}
response, err := client.Do(request)
if err != nil {
println("energy check update client.Do error:", err.Error())
return
}
defer response.Body.Close()
if data, err := ioutil.ReadAll(response.Body); err == nil {
//var v map[string]any
var v = update{}
if err = json.Unmarshal(data, &v); err == nil {
fmt.Printf("data: %+v\n", v)
} else {
println("energy check update json.Unmarshal error", err.Error())
}
} else {
println("energy check update ioutil.ReadAl(response.Body) error", err.Error())
}
}
func updatePrompt() {
......
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
package internal
const (
CheckURL = "https://energy.yanghy.cn/autoconfig/update.json"
)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册