Skip to content

Commit

Permalink
U: energy cli
Browse files Browse the repository at this point in the history
  • Loading branch information
sxmxta committed Dec 17, 2024
1 parent dd75bdf commit 56060f3
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 56 deletions.
2 changes: 1 addition & 1 deletion cmd/energy/energy.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func termRun() {
env.InitDevEnvConfig(wd) //初始化本地配置文件
signalHandler()
if err := cmd.Run(cc); err != nil {
term.Section.Println(err.Error())
term.Logger.Error(err.Error())
os.Exit(1)
}
}
Expand Down
17 changes: 11 additions & 6 deletions cmd/internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,28 +168,33 @@ func (m OS) Value() string {
}

func (m Arch) Is386() bool {
return m == "386" || strings.ToLower(m.Value()) == "i386" || m == "32"
v := m.Value()
return v == "386" || v == "i386" || v == "32"
}

func (m Arch) IsAMD64() bool {
return strings.ToLower(m.Value()) == "amd64" || m == "64" || strings.ToLower(m.Value()) == "x64"
v := strings.ToLower(m.Value())
return v == "amd64" || v == "64" || v == "x64"
}

func (m Arch) IsARM64() bool {
return strings.ToLower(m.Value()) == "arm64"
v := strings.ToLower(m.Value())
return v == "arm64"
}

func (m Arch) IsARM() bool {
return strings.ToLower(m.Value()) == "arm"
v := strings.ToLower(m.Value())
return v == "arm"
}

func (m Arch) Value() string {
switch m {
v := string(m)
switch v {
case "386", "i386", "32":
return "32"
case "amd64", "64", "x64":
return "64"
default:
return string(m)
return v
}
}
22 changes: 16 additions & 6 deletions cmd/internal/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ func Install(cmdConfig *command.Config) error {
}
// 设置默认参数
defaultInstallConfig(cmdConfig)
// 检查环境
willInstall := checkInstallEnv(rmtConfig, cmdConfig)

// 检查本地环境
willInstall := checkLocalInstallEnv(rmtConfig, cmdConfig)
// 检查本地环境当前安装版本
_, _, _, err = rmtConfig.GetInstallVersion(cmdConfig)
if err != nil {
return err
}
var (
goRoot string
goSuccessCallback func()
Expand Down Expand Up @@ -133,15 +137,21 @@ func Install(cmdConfig *command.Config) error {
}

// 安装Go开发环境
goRoot, goSuccessCallback = installGolang(rmtConfig, cmdConfig)
goRoot, goSuccessCallback, err = installGolang(rmtConfig, cmdConfig)
if err != nil {
return err
}
// 设置 go 环境变量
if goRoot != "" {
env.GlobalDevEnvConfig.GoRoot = goRoot
env.SetGoEnv(goRoot)
}

// 安装CEF二进制框架
cefFrameworkName, cefFrameworkSuccessCallback = installCEFFramework(rmtConfig, cmdConfig)
cefFrameworkName, cefFrameworkSuccessCallback, err = installCEFFramework(rmtConfig, cmdConfig)
if err != nil {
return err
}
if cefFrameworkName != "" {
// 设置 CEF 框架, 此处是框架名不是目录
env.GlobalDevEnvConfig.Framework = cefFrameworkName
Expand Down Expand Up @@ -284,7 +294,7 @@ func z7zCanInstall() bool {
// nsis: windows
// cef: all os
// upx: windows amd64, 386, linux amd64, arm64
func checkInstallEnv(config *remotecfg.TConfig, cmdConfig *command.Config) (result []*softEnf) {
func checkLocalInstallEnv(config *remotecfg.TConfig, cmdConfig *command.Config) (result []*softEnf) {
result = make([]*softEnf, 0)
var check = func(chkInstall func() (string, bool), name string, yes func()) {
desc, ok := chkInstall()
Expand Down
46 changes: 20 additions & 26 deletions cmd/internal/install/install_cef_framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ import (
// Linux
// Gtk2: CEF 最新版本
// Gtk3: CEF 最新版本
func installCEFFramework(config *remotecfg.TConfig, cmdConfig *command.Config) (string, func()) {
func installCEFFramework(config *remotecfg.TConfig, cmdConfig *command.Config) (string, func(), error) {
if !cmdConfig.Install.ICEF {
return "", nil
return "", nil, nil
}
pterm.Println()
term.Section.Println("Install CEF")
Expand All @@ -47,18 +47,20 @@ func installCEFFramework(config *remotecfg.TConfig, cmdConfig *command.Config) (
installPathName := config.GetFrameworkInstallPath(cmdConfig)
if installPathName == "" {
term.Logger.Error("Failed to obtain the frame installation directory")
return "", nil
return "", nil, nil
}
term.Section.Println("Install Path", installPathName)

term.Section.Println("Start downloading CEF and Energy dependency")

// 获取到当前安装版本
installVersion, cefModuleName, liblclModuleName := config.GetInstallVersion(cmdConfig)
installVersion, cefModuleName, liblclModuleName, err := config.GetInstallVersion(cmdConfig)
if err != nil {
return "", nil, err
}
// 判断模块名是否为空
if installVersion == nil || liblclModuleName == "" || cefModuleName == "" {
term.Logger.Error("The supported version is not matched, Version: " + cmdConfig.Install.Version)
return "", nil
return "", nil, errors.New("The supported version is not matched, Version: " + cmdConfig.Install.Version)
}
term.Section.Println("Install CEF-VER: " + strings.ToUpper(cefModuleName))

Expand All @@ -80,8 +82,7 @@ func installCEFFramework(config *remotecfg.TConfig, cmdConfig *command.Config) (
lclDownloadItem = config.ModeBaseConfig.DownloadSourceItem.LCL.Item(lclItem.DownloadSource)
)
if cefModuleVersion == "" || liblclModuleVersion == "" {
term.Logger.Error("CEF module " + cefModuleName + " is not configured in the current version")
return "", nil
return "", nil, errors.New("CEF module " + cefModuleName + " is not configured in the current version")
}

// CEF 版本号 109, 101 ...
Expand Down Expand Up @@ -119,8 +120,7 @@ func installCEFFramework(config *remotecfg.TConfig, cmdConfig *command.Config) (
term.Section.Println("Download", key, ":", dl.url)
err := tools.DownloadFile(dl.url, dl.downloadPath, env.GlobalDevEnvConfig.Proxy, nil)
if err != nil {
term.Logger.Error("Download [" + dl.fileName + "] " + err.Error())
return "", nil
return "", nil, errors.New("Download [" + dl.fileName + "] " + err.Error())
}
dl.success = err == nil
}
Expand All @@ -134,35 +134,30 @@ func installCEFFramework(config *remotecfg.TConfig, cmdConfig *command.Config) (
if filepath.Ext(di.downloadPath) == ".bz2" {
processBar, err := pterm.DefaultProgressbar.WithShowCount(false).WithShowPercentage(false).WithMaxWidth(1).Start()
if err != nil {
term.Logger.Error(err.Error())
return "", nil
return "", nil, err
}
// 解压 tar bz2
tarSourcePath, err := tools.UnBz2ToTar(di.downloadPath, func(totalLength, processLength int64) {
processBar.UpdateTitle(fmt.Sprintf("Unpack file %s, process: %d", key, processLength)) // Update the title of the progressbar.
})
processBar.Stop()
if err != nil {
term.Logger.Error(err.Error())
return "", nil
return "", nil, err
}
// 释放文件
if err := extractFiles(key, tarSourcePath, di, extractOSConfig.CEF); err != nil {
term.Logger.Error(err.Error())
return "", nil
return "", nil, err
}
} else {
// 释放文件
if err := extractFiles(key, di.downloadPath, di, extractOSConfig.CEF); err != nil {
term.Logger.Error(err.Error())
return "", nil
return "", nil, err
}
}
} else if key == consts.LiblclKey {
// 释放文件
if err := extractFiles(key, di.downloadPath, di, extractOSConfig.LCL); err != nil {
term.Logger.Error(err.Error())
return "", nil
return "", nil, err
}
}
term.Section.Println("Unpack file", key, "success")
Expand All @@ -173,19 +168,18 @@ func installCEFFramework(config *remotecfg.TConfig, cmdConfig *command.Config) (
if liblclModuleName == "" {
term.Section.Println("hint: liblcl module", liblclModuleName, `is not configured in the current version`)
}
}
}, nil
}

// 返回 CEF 支持的系统, 格式 [os][arch] 示例 windows32, windows64, macosx64 ...
func cefOS(c *command.Config) string {
ins := c.Install
os := command.OS(runtime.GOOS)
arch := command.Arch(runtime.GOARCH)
if ins.OS != "" {
os = ins.OS
if c.Install.OS != "" {
os = c.Install.OS
}
if ins.Arch != "" {
arch = ins.Arch
if c.Install.Arch != "" {
arch = c.Install.Arch
}
if os.IsMacOS() {
os = "macos"
Expand Down
18 changes: 9 additions & 9 deletions cmd/internal/install/install_golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import (
)

// 下载go并配置安装
func installGolang(rtmConfig *remotecfg.TConfig, cmdConfig *command.Config) (string, func()) {
func installGolang(rtmConfig *remotecfg.TConfig, cmdConfig *command.Config) (string, func(), error) {
if !cmdConfig.Install.IGolang {
return "", nil
return "", nil, nil
}
pterm.Println()
term.Section.Println("Install Golang")
Expand All @@ -48,15 +48,15 @@ func installGolang(rtmConfig *remotecfg.TConfig, cmdConfig *command.Config) (str
term.Section.Println("Creating directory.", installPath)
if err := os.MkdirAll(installPath, fs.ModePerm); err != nil {
term.Section.Println("Failed to create goroot directory", err.Error())
return "", nil
return "", nil, err
}
} else {
// 检查 golang 的 go cmd 是否存在
gocmd := filepath.Join(installPath, "bin", "go")
if tools.IsExist(gocmd) {
return installPath, func() {
term.Logger.Info("Golang has been installed")
}
}, nil
}
}
// 下载文件名
Expand All @@ -72,6 +72,7 @@ func installGolang(rtmConfig *remotecfg.TConfig, cmdConfig *command.Config) (str
err = downloadGolang(downloadUrl, saveFileCachPath, fileName, 0)
if err != nil {
term.Logger.Error("Download [" + fileName + "] failed: " + err.Error())
return "", nil, err
} else {
term.Logger.Info("Download [" + fileName + "] success")
}
Expand All @@ -82,21 +83,20 @@ func installGolang(rtmConfig *remotecfg.TConfig, cmdConfig *command.Config) (str
if consts.IsWindows {
//zip
if err = tools.ExtractUnZip(saveFileCachPath, installPath, true); err != nil {
term.Logger.Error(err.Error())
return "", nil
return "", nil, err
}
} else {
//tar
if err = tools.ExtractUnTar(saveFileCachPath, installPath); err != nil {
term.Logger.Error(err.Error())
return "", nil
return "", nil, err
}
}
return installPath, func() {
term.Logger.Info("Golang Installed Successfully", term.Logger.Args("Version", version))
}
}, nil
}
return "", nil
return "", nil, err
}

func downloadGolang(downloadUrl, savePath, fileName string, count int) error {
Expand Down
16 changes: 8 additions & 8 deletions cmd/internal/remotecfg/model_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
package remotecfg

import (
"errors"
"fmt"
"github.com/energye/energy/v2/cmd/internal/command"
"github.com/energye/energy/v2/cmd/internal/consts"
"github.com/energye/energy/v2/cmd/internal/term"
"github.com/energye/energy/v2/cmd/internal/tools"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -65,10 +65,10 @@ func BaseConfig() (*TConfig, error) {
}

// 获取当前安装的版本
func (m TConfig) GetInstallVersion(c *command.Config) (installVersion *TVersionsUpgrade, cefModuleName string, liblclModuleName string) {
func (m TConfig) GetInstallVersion(c *command.Config) (installVersion *TVersionsUpgrade, cefModuleName string, liblclModuleName string, retErr error) {
releaseList, err := VersionUpgradeList()
if err != nil {
term.Logger.Error(err.Error())
retErr = err
return
}
if c.Install.Version == "latest" {
Expand All @@ -83,7 +83,7 @@ func (m TConfig) GetInstallVersion(c *command.Config) (installVersion *TVersions
}
}
if installVersion == nil {
term.Logger.Error("Invalid version number " + c.Install.Version)
retErr = errors.New("Invalid Version Number: " + c.Install.Version)
return
}
// 找到相同版配置
Expand All @@ -93,15 +93,15 @@ func (m TConfig) GetInstallVersion(c *command.Config) (installVersion *TVersions
installVersion = &v
break
} else {
term.Logger.Error("Incorrect version configuration. Identical: " + installVersion.Identical)
retErr = errors.New("Incorrect version configuration. Identical: " + installVersion.Identical)
return
}
} else {
break
}
}
if installVersion == nil {
term.Logger.Error("Identical Invalid version number " + c.Install.Version)
retErr = errors.New("Identical Invalid Version Number: " + c.Install.Version)
return
}
// 指定支持的固定 CEF 版本号
Expand Down Expand Up @@ -169,8 +169,8 @@ func (m TConfig) GetFrameworkInstallPath(c *command.Config) string {

// 获取 CEF 完整名 CEF_[VER]_[OS]_[ARCH]
func (m TConfig) GetFrameworkName(c *command.Config) string {
_, cefModuleName, _ := m.GetInstallVersion(c)
if cefModuleName != "" {
_, cefModuleName, _, err := m.GetInstallVersion(c)
if cefModuleName != "" && err == nil {
frameworkName := fmt.Sprintf("%s_%s_%s", cefModuleName, c.Install.OS.Value(), c.Install.Arch.Value())
return strings.ToUpper(frameworkName)
}
Expand Down

0 comments on commit 56060f3

Please sign in to comment.