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

energy command-line tools: install

上级 fbcdaaf4
......@@ -17,6 +17,7 @@ import (
"bytes"
"fmt"
"github.com/energye/energy/v2/cmd/internal/consts"
"github.com/energye/energy/v2/cmd/internal/term"
"github.com/energye/energy/v2/cmd/internal/tools"
"github.com/energye/golcl/energy/homedir"
toolsCommand "github.com/energye/golcl/tools/command"
......@@ -30,19 +31,19 @@ import (
func SetUPXEnv(upxRoot string) {
upx := filepath.Join(upxRoot, "upx")
if !tools.IsExist(upx) {
println("\nError: Failed to set the UPX environment variable, not a correct UPX installation directory. ", upxRoot)
term.Logger.Error("Failed to set the UPX environment variable, not a correct UPX installation directory. " + upxRoot)
return
}
println("\nSetting UPX environment Variables to:", upxRoot)
term.Logger.Info("Setting UPX environment Variables: ", term.Logger.Args("UPX_HOME", upxRoot))
cmd := toolsCommand.NewCMD()
cmd.IsPrint = false
cmd.MessageCallback = func(s []byte, e error) {
msg := strings.TrimSpace(string(s))
if msg != "" {
fmt.Println("CMD:", msg)
term.Logger.Info("CMD " + msg)
}
if e != nil {
fmt.Println("CMD Error:", e)
term.Logger.Error("CMD " + e.Error())
}
}
defer cmd.Close()
......@@ -50,7 +51,7 @@ func SetUPXEnv(upxRoot string) {
var exPath = "export PATH=$PATH:$UPX_HOME"
var exs = []string{exUpxRoot, exPath}
setPosixEnv(exs, exPath, "$UPX_HOME")
println("Hint: Reopen the cmd window for the upx command to take effect.")
term.BoxPrintln("Hint: Reopen the cmd window for the upx command to take effect.")
}
func SetNSISEnv(nsisRoot string) {
......@@ -60,19 +61,19 @@ func SetNSISEnv(nsisRoot string) {
func SetGoEnv(goRoot string) {
goBin := filepath.Join(goRoot, "bin", "go")
if !tools.IsExist(goBin) {
println("\nError: Failed to set the Golang environment variable, not a correct Golang installation directory. ", goRoot)
term.Logger.Error("Error: Failed to set the Golang environment variable, not a correct Golang installation directory. " + goRoot)
return
}
println("\nSetting Golang environment Variables to:", goRoot)
term.Logger.Info("Setting Golang environment Variables: ", term.Logger.Args("GOROOT", goRoot, "GOCACHE", "%GOROOT%\\go-build", "GOBIN", "%GOROOT%\\bin"))
cmd := toolsCommand.NewCMD()
cmd.IsPrint = false
cmd.MessageCallback = func(s []byte, e error) {
msg := strings.TrimSpace(string(s))
if msg != "" {
fmt.Println("CMD:", msg)
term.Logger.Info("CMD " + msg)
}
if e != nil {
fmt.Println("CMD Error:", e)
term.Logger.Error("CMD " + e.Error())
}
}
defer cmd.Close()
......@@ -86,7 +87,7 @@ func SetGoEnv(goRoot string) {
var exPath = "export PATH=$PATH:$GOBIN"
var exs = []string{exGoRoot, exGoCache, exGoBin}
setPosixEnv(exs, exPath, "$GOBIN")
println("\nHint: Reopen the cmd window for the Go command to take effect.")
term.BoxPrintln("Hint: Reopen the cmd window for the Go command to take effect.")
}
func SetEnergyHomeEnv(homePath string) {
......@@ -98,26 +99,26 @@ func SetEnergyHomeEnv(homePath string) {
}
cefPath := filepath.Join(homePath, cef)
if !tools.IsExist(cefPath) {
println("\nError: Setting the ENERGY_HOME environment variable failed and is not a correct CEF Framework installation directory. ", homePath)
term.Logger.Error("Error: Setting ENERGY_HOME environment variable failed and is not a correct CEF Framework installation directory. " + homePath)
return
}
println("\nSetting environment Variables [ENERGY_HOME] to", homePath)
term.Logger.Info("Setting ENERGY environment Variables [ENERGY_HOME] to " + homePath)
cmd := toolsCommand.NewCMD()
cmd.IsPrint = false
cmd.MessageCallback = func(s []byte, e error) {
msg := strings.TrimSpace(string(s))
if msg != "" {
fmt.Println("CMD:", msg)
term.Logger.Info("CMD " + msg)
}
if e != nil {
fmt.Println("CMD Error:", e)
term.Logger.Error("CMD " + e.Error())
}
}
defer cmd.Close()
var energyHome = fmt.Sprintf("export %s=%s", consts.EnergyHomeKey, homePath)
exs := []string{energyHome}
setPosixEnv(exs, "", "")
println("\nHint: Reopen the cmd window to make the environment variables take effect.")
term.BoxPrintln("Hint: Reopen the cmd window to make the environment variables take effect.")
}
func setPosixEnv(exs []string, binPath, bin string) {
......
......@@ -29,7 +29,7 @@ func SetUPXEnv(upxRoot string) {
term.Logger.Error("Failed to set the UPX environment variable, not a correct UPX installation directory. " + upxRoot)
return
}
term.Logger.Info("Setting UPX environment Variables to: " + upxRoot)
term.Logger.Info("Setting UPX environment Variables: ", term.Logger.Args("UPX_HOME", upxRoot))
setWindowsEnv(consts.UPXHomeKey, upxRoot)
appendWindowsEnv("Path", "%UPX_HOME%")
term.BoxPrintln("Hint: Reopen the cmd window for the upx command to take effect.")
......@@ -41,7 +41,7 @@ func SetNSISEnv(nsisRoot string) {
term.Logger.Error("Error: Failed to set the NSIS environment variable, not a correct NSIS installation directory. " + nsisRoot)
return
}
term.Logger.Info("Setting NSIS environment Variables to: " + nsisRoot)
term.Logger.Info("Setting NSIS environment Variables: ", term.Logger.Args("NSIS_HOME", nsisRoot))
//regCurUser := tools.NewRegistryCurrentUser()
//defer regCurUser.Close()
//regCurUser.Set(consts.NSISHomeKey, nsisRoot)
......@@ -58,7 +58,7 @@ func SetGoEnv(goRoot string) {
term.Logger.Error("Error: Failed to set the Golang environment variable, not a correct Golang installation directory. " + goRoot)
return
}
term.Logger.Info("Setting Golang environment Variables to: " + goRoot)
term.Logger.Info("Setting Golang environment Variables: ", term.Logger.Args("GOROOT", goRoot, "GOCACHE", "%GOROOT%\\go-build", "GOBIN", "%GOROOT%\\bin"))
//regCurUser := tools.NewRegistryCurrentUser()
//defer regCurUser.Close()
//regCurUser.Set("GOROOT", goRoot)
......
......@@ -431,9 +431,13 @@ func ExtractUnTar(filePath, targetPath string, files ...any) error {
if err != nil {
return err
}
var c int
var (
total = 100
c int
cn int
)
_, fileName := filepath.Split(targetFile)
wfpb, err := writeFileProcessBar.WithCurrent(0).WithTotal(100).Start("Write File " + fileName)
wfpb, err := writeFileProcessBar.WithCurrent(0).WithTotal(total).Start("Write File " + fileName)
if err != nil {
return err
}
......@@ -442,9 +446,12 @@ func ExtractUnTar(filePath, targetPath string, files ...any) error {
if process > c {
c = process
wfpb.Add(process)
cn++
}
})
wfpb.WithCurrent(100).Add(0)
if cn < total {
wfpb.Add(total - cn)
}
wfpb.Stop()
file.Sync()
file.Close()
......@@ -475,8 +482,12 @@ func ExtractUnZip(filePath, targetPath string, rmRootDir bool, files ...any) err
}
if targetFile, err := os.Create(targetFileName); err == nil {
defer targetFile.Close()
var c int
wfpb, err := writeFileProcessBar.WithCurrent(0).WithTotal(100).Start("Write File " + name)
var (
total = 100
c int
cn int
)
wfpb, err := writeFileProcessBar.WithCurrent(0).WithTotal(total).Start("Write File " + name)
if err != nil {
return err
}
......@@ -485,8 +496,12 @@ func ExtractUnZip(filePath, targetPath string, rmRootDir bool, files ...any) err
if process > c {
c = process
wfpb.Add(process)
cn++
}
})
if cn < total {
wfpb.Add(total - cn)
}
wfpb.Stop()
return nil
} else {
......@@ -634,16 +649,19 @@ func DownloadFile(url string, localPath string, callback func(totalLength, proce
return errors.New("body is null")
}
defer resp.Body.Close()
total := 100
_, fileName := filepath.Split(localPath)
p, err := pterm.DefaultProgressbar.WithTotal(100).WithTitle("Download " + fileName).Start()
p, err := pterm.DefaultProgressbar.WithTotal(total).WithTitle("Download " + fileName).Start()
if err != nil {
return err
}
defer func() {
p.WithCurrent(100).Add(0)
p.Stop()
}()
var count int
var (
count int
cn int
)
var nw int
for {
nr, er := resp.Body.Read(buf)
......@@ -659,6 +677,7 @@ func DownloadFile(url string, localPath string, callback func(totalLength, proce
if process > count {
count = process
p.Add(1)
cn++
}
}
if err != nil {
......@@ -676,6 +695,10 @@ func DownloadFile(url string, localPath string, callback func(totalLength, proce
break
}
}
if cn < total {
p.Add(total - cn)
}
p.Stop()
if err == nil {
file.Sync()
file.Close()
......
......@@ -40,13 +40,6 @@ func installUPX(c *command.Config) (string, func()) {
defer targetFile.Close()
term.Section.Println("extract file: ", upxName)
p, err := pterm.DefaultProgressbar.WithTotal(100).WithTitle("Write File " + upxName).Start()
if err != nil {
return "", nil
}
defer p.Stop()
var count int
fs, err := assets.UpxBytes()
if err != nil {
term.Logger.Error("UPX Installed Error: " + err.Error())
......@@ -57,13 +50,28 @@ func installUPX(c *command.Config) (string, func()) {
term.Logger.Error("UPX Installed Error: " + err.Error())
return "", nil
}
var (
total = 100
count int
cn int
)
p, err := pterm.DefaultProgressbar.WithTotal(total).WithTitle("Write File " + upxName).Start()
if err != nil {
return "", nil
}
writeFile(fs, targetFile, stat.Size(), func(totalLength, processLength int64) {
process := int((float64(processLength) / float64(totalLength)) * 100)
if process > count {
count = process
p.Add(1)
cn++
}
})
if cn < total {
p.Add(total - cn)
}
p.Stop()
return s, func() {
term.Logger.Info("UPX Installed Successfully ", term.Logger.Args("Version", assets.UpxVersion))
}
......
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
package term
import (
......
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
package term
import (
"github.com/energye/energy/v2/cmd/internal/consts"
"github.com/energye/golcl/lcl/rtl/version"
"github.com/pterm/pterm"
"os"
)
......@@ -16,15 +24,6 @@ var TermOut = new(Termout)
var Section *pterm.SectionPrinter
func init() {
if consts.IsWindows {
// < windows 10 禁用颜色
version.VersionInit()
ov := version.OSVersion
if ov.Major < 10 {
pterm.DisableColor()
}
}
// logger
Logger = pterm.DefaultLogger.WithLevel(pterm.LogLevelTrace)
TermOut = new(Termout)
......
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
//go:build windows
// +build windows
package term
import (
"github.com/energye/golcl/lcl/rtl/version"
"github.com/pterm/pterm"
)
func init() {
// < windows 10 禁用颜色
version.VersionInit()
ov := version.OSVersion
if ov.Major < 10 {
pterm.DisableColor()
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册