Skip to content

Commit cbe960a

Browse files
committed
U: CLI install LibLCL Auto switch the download source
1 parent aea3f3c commit cbe960a

File tree

2 files changed

+60
-22
lines changed

2 files changed

+60
-22
lines changed

cmd/internal/install/install_cef_framework.go

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ func installCEFFramework(config *remotecfg.TConfig, cmdConfig *command.Config) (
8080
lclItem = lclCfg.Item(liblclModuleVersion)
8181
// 当前安装下载源
8282
cefDownloadItem = config.ModeBaseConfig.DownloadSourceItem.CEF.Item(cefItem.DownloadSource)
83-
lclDownloadItem = config.ModeBaseConfig.DownloadSourceItem.LCL.Item(lclItem.DownloadSource)
8483
)
8584
if cefModuleVersion == "" || liblclModuleVersion == "" {
8685
return "", nil, errors.New("CEF module " + cefModuleName + " is not configured in the current version")
@@ -105,26 +104,73 @@ func installCEFFramework(config *remotecfg.TConfig, cmdConfig *command.Config) (
105104
downloadCefURL = strings.ReplaceAll(downloadCefURL, "{OSARCH}", libCEFOS)
106105
downloads[consts.CefKey] = &downloadInfo{fileName: common.UrlName(downloadCefURL), downloadPath: filepath.Join(cmdConfig.Install.Path, consts.FrameworkCache, common.UrlName(downloadCefURL)), frameworkPath: installPathName, url: downloadCefURL, module: cefModuleName}
107106
}
107+
// LibLCL 动态链接库 下载地址, 在下面下载失败时自动切换下一个下载源使用
108+
var getLibLCLDownUrl func() string
108109
if !isLCL {
109110
// liblcl 当前模块版本支持系统
110111
libEnergyOS := common.LibLCLOS(cmdConfig)
111112
useLibLCLModuleNameGTK3 := common.LibLCLLinuxUseGTK3(cmdConfig.Install.OS, cmdConfig.Install.WS, liblclModuleName, moduleVersion)
112113
// https://www.xxx.xxx/xxx/releases/download/{version}/{module}.{OSARCH}.zip
113-
downloadEnergyURL := lclDownloadItem.Url
114-
downloadEnergyURL = strings.ReplaceAll(downloadEnergyURL, "{version}", "v"+liblclModuleVersion)
115-
downloadEnergyURL = strings.ReplaceAll(downloadEnergyURL, "{module}", useLibLCLModuleNameGTK3)
116-
downloadEnergyURL = strings.ReplaceAll(downloadEnergyURL, "{OSARCH}", libEnergyOS)
114+
downSrcList := lclItem.DownloadSourceList[:]
115+
// 默认使用下载源
116+
useDownSrc := lclItem.DownloadSource
117+
// 删除当前使用下载源,以在失败情况下可以获取下一个
118+
removeDownSrcForListAndSetNext := func() {
119+
for i, v := range downSrcList {
120+
if v == useDownSrc {
121+
// 删除这个
122+
downSrcList = append(downSrcList[:i], downSrcList[i+1:]...)
123+
break
124+
}
125+
}
126+
// 重新设置下载源,如果下载失败
127+
if len(downSrcList) > 0 {
128+
useDownSrc = downSrcList[0]
129+
}
130+
}
131+
// 获取 LibLCL 下载源,返回空表示已经被全部使用过了
132+
getLibLCLDownUrl = func() string {
133+
if len(downSrcList) == 0 {
134+
return ""
135+
}
136+
lclDownloadItem := config.ModeBaseConfig.DownloadSourceItem.LCL.Item(useDownSrc)
137+
downloadURL := lclDownloadItem.Url
138+
downloadURL = strings.ReplaceAll(downloadURL, "{version}", "v"+liblclModuleVersion)
139+
downloadURL = strings.ReplaceAll(downloadURL, "{module}", useLibLCLModuleNameGTK3)
140+
downloadURL = strings.ReplaceAll(downloadURL, "{OSARCH}", libEnergyOS)
141+
// 删除 downSrcList 当前下载源,表示已经使用过
142+
// 将当前使用的在 下载集合里 删除掉
143+
removeDownSrcForListAndSetNext()
144+
return downloadURL
145+
}
146+
// 获取下载地址
147+
downloadEnergyURL := getLibLCLDownUrl()
117148
downloads[consts.LiblclKey] = &downloadInfo{fileName: common.UrlName(downloadEnergyURL), downloadPath: filepath.Join(cmdConfig.Install.Path, consts.FrameworkCache, common.UrlName(downloadEnergyURL)), frameworkPath: installPathName, url: downloadEnergyURL, module: liblclModuleName}
118149
}
119150
// 在线下载 CEF 框架二进制包
120151
var sortsKeys = []string{consts.LiblclKey, consts.CefKey}
121-
for _, key := range sortsKeys {
152+
for i := 0; i < len(sortsKeys); i++ {
153+
key := sortsKeys[i]
122154
dl, ok := downloads[key]
123155
if ok {
124156
term.Section.Println("Download", key, ":", dl.url)
125157
err := tools.DownloadFile(dl.url, dl.downloadPath, env.GlobalDevEnvConfig.Proxy, nil)
126-
if err != nil {
127-
return "", nil, errors.New("Download [" + dl.fileName + "] " + err.Error())
158+
if key == consts.LiblclKey {
159+
if err != nil {
160+
// 失败尝试使用下个下载源
161+
if downURL := getLibLCLDownUrl(); downURL != "" {
162+
term.Logger.Error("Download", term.Logger.Args("ERROR", err.Error(), "Auto switch download source", downURL))
163+
dl.url = downURL
164+
i--
165+
continue
166+
} else {
167+
return "", nil, errors.New("Download [" + dl.fileName + "] " + err.Error())
168+
}
169+
}
170+
} else {
171+
if err != nil {
172+
return "", nil, errors.New("Download [" + dl.fileName + "] " + err.Error())
173+
}
128174
}
129175
dl.success = err == nil
130176
}

cmd/internal/remotecfg/model_base_config.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ type TExtracts map[string]TExtract
2323
type TDownloadItems map[string]TDownloadItem
2424

2525
type TModeBaseConfig struct {
26-
DownloadSourceItem TDownloadSourceItem `json:"downloadSourceItem"`
27-
Environment TEnvironment `json:"environment"`
28-
Extract TExtracts `json:"extract"`
26+
DownloadSourceItem TDownloadSourceItem `json:"downloadSourceItem"` // 下载源集合配置
27+
Extract TExtracts `json:"extract"` // 压缩包提取目录文件规则
2928
}
3029

3130
type TDownloadSourceItem struct {
@@ -38,17 +37,10 @@ type TDownloadSourceItem struct {
3837
}
3938

4039
type TDownloadItem struct {
41-
Label string `json:"label"`
42-
Url string `json:"url"`
43-
Md5 string `json:"md5"`
44-
Version string `json:"version"`
45-
}
46-
47-
type TEnvironment struct {
48-
EnergyHomeKey string
49-
GolanHomeKey string
50-
NSISHomeKey string
51-
Z7ZHomeKey string
40+
Label string `json:"label"` // 显示的下载源名
41+
Url string `json:"url"` // 下载地址
42+
Md5 string `json:"md5"` // 下载md5
43+
Version string `json:"version"` // 其它工具下载版本
5244
}
5345

5446
type TExtract struct {

0 commit comments

Comments
 (0)