2023-09-04 20:00:53 +08:00
|
|
|
//----------------------------------------
|
|
|
|
//
|
|
|
|
// Copyright © yanghy. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under Apache License Version 2.0, January 2004
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
|
2023-09-04 11:36:36 +08:00
|
|
|
package install
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"archive/zip"
|
|
|
|
"compress/bzip2"
|
2023-09-04 20:00:53 +08:00
|
|
|
"compress/gzip"
|
2023-09-06 17:41:06 +08:00
|
|
|
"errors"
|
2023-09-04 11:36:36 +08:00
|
|
|
"fmt"
|
|
|
|
"github.com/energye/energy/v2/cmd/internal/command"
|
2023-09-05 19:45:30 +08:00
|
|
|
"github.com/energye/energy/v2/cmd/internal/consts"
|
2023-09-04 11:36:36 +08:00
|
|
|
"github.com/energye/energy/v2/cmd/internal/env"
|
2023-09-10 21:57:00 +08:00
|
|
|
"github.com/energye/energy/v2/cmd/internal/term"
|
2023-09-04 11:36:36 +08:00
|
|
|
"github.com/energye/energy/v2/cmd/internal/tools"
|
2023-09-10 21:57:00 +08:00
|
|
|
"github.com/pterm/pterm"
|
2023-09-04 11:36:36 +08:00
|
|
|
"io"
|
|
|
|
"io/fs"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-09-05 22:30:04 +08:00
|
|
|
"runtime"
|
2023-09-04 11:36:36 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type downloadInfo struct {
|
|
|
|
fileName string
|
|
|
|
frameworkPath string
|
|
|
|
downloadPath string
|
|
|
|
url string
|
|
|
|
success bool
|
|
|
|
isSupport bool
|
|
|
|
module string
|
|
|
|
}
|
2023-09-10 21:57:00 +08:00
|
|
|
type softEnf struct {
|
|
|
|
name string
|
|
|
|
desc string
|
|
|
|
installed bool
|
|
|
|
yes func()
|
|
|
|
}
|
2023-09-04 11:36:36 +08:00
|
|
|
|
2023-09-10 21:57:00 +08:00
|
|
|
func Install(c *command.Config) error {
|
2023-09-05 17:01:33 +08:00
|
|
|
// 初始配置和安装目录
|
2023-09-04 20:00:53 +08:00
|
|
|
initInstall(c)
|
2023-09-05 22:31:14 +08:00
|
|
|
// 检查环境
|
2023-09-05 22:30:04 +08:00
|
|
|
willInstall := checkInstallEnv(c)
|
2023-09-05 19:09:15 +08:00
|
|
|
var (
|
2023-09-05 20:03:09 +08:00
|
|
|
goRoot string
|
|
|
|
goSuccessCallback func()
|
|
|
|
cefFrameworkRoot string
|
|
|
|
cefFrameworkSuccessCallback func()
|
|
|
|
nsisRoot string
|
|
|
|
nsisSuccessCallback func()
|
2023-09-08 22:54:06 +08:00
|
|
|
upxRoot string
|
|
|
|
upxSuccessCallback func()
|
2023-09-12 11:01:19 +08:00
|
|
|
z7zRoot string
|
|
|
|
z7zSuccessCallback func()
|
2023-09-05 19:09:15 +08:00
|
|
|
)
|
2023-09-05 22:30:04 +08:00
|
|
|
if len(willInstall) > 0 {
|
2023-09-10 21:57:00 +08:00
|
|
|
// energy 依赖的软件框架
|
|
|
|
tableData := pterm.TableData{
|
|
|
|
{"Software", "Installed", "Support Platform Description"},
|
|
|
|
}
|
|
|
|
var options []string
|
|
|
|
for _, se := range willInstall {
|
|
|
|
var installed string
|
|
|
|
if se.installed {
|
|
|
|
installed = "Yes"
|
|
|
|
} else {
|
|
|
|
installed = "No"
|
|
|
|
options = append(options, se.name)
|
|
|
|
//se.yes()
|
|
|
|
}
|
|
|
|
var data = []string{se.name, installed, se.desc}
|
|
|
|
tableData = append(tableData, data)
|
|
|
|
}
|
|
|
|
term.Section.Println("Energy Development Environment Framework Dependencies")
|
|
|
|
err := pterm.DefaultTable.WithHasHeader().WithHeaderRowSeparator("-").WithBoxed().WithData(tableData).Render()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// 选择
|
|
|
|
if len(options) > 0 {
|
|
|
|
printer := term.DefaultInteractiveMultiselect.WithOnInterruptFunc(func() {
|
|
|
|
os.Exit(1)
|
|
|
|
}).WithOptions(options)
|
2023-09-11 17:32:42 +08:00
|
|
|
printer.CheckmarkANSI()
|
2023-09-10 21:57:00 +08:00
|
|
|
printer.DefaultText = "Optional Installation"
|
|
|
|
printer.Filter = false
|
|
|
|
selectedOptions, err := printer.Show()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-12 17:05:44 +08:00
|
|
|
term.Section.Printfln("Selected : %s", pterm.Green(selectedOptions))
|
2023-09-11 15:28:02 +08:00
|
|
|
for _, option := range selectedOptions {
|
|
|
|
for _, wi := range willInstall {
|
|
|
|
if option == wi.name {
|
|
|
|
wi.yes()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-05 22:30:04 +08:00
|
|
|
}
|
|
|
|
}
|
2023-09-05 17:01:33 +08:00
|
|
|
// 安装Go开发环境
|
2023-09-05 20:03:09 +08:00
|
|
|
goRoot, goSuccessCallback = installGolang(c)
|
2023-09-06 17:41:06 +08:00
|
|
|
// 设置 go 环境变量
|
|
|
|
if goRoot != "" {
|
|
|
|
env.SetGoEnv(goRoot)
|
|
|
|
}
|
2023-09-12 11:01:19 +08:00
|
|
|
|
|
|
|
// 安装CEF二进制框架
|
|
|
|
cefFrameworkRoot, cefFrameworkSuccessCallback = installCEFFramework(c)
|
|
|
|
// 设置 energy cef 环境变量
|
|
|
|
if cefFrameworkRoot != "" && c.Install.IsSame {
|
|
|
|
env.SetEnergyHomeEnv(cefFrameworkRoot)
|
|
|
|
}
|
|
|
|
|
2023-09-05 17:01:33 +08:00
|
|
|
// 安装nsis安装包制作工具, 仅windows - amd64
|
2023-09-05 20:03:09 +08:00
|
|
|
nsisRoot, nsisSuccessCallback = installNSIS(c)
|
2023-09-05 17:01:33 +08:00
|
|
|
// 设置nsis环境变量
|
|
|
|
if nsisRoot != "" {
|
|
|
|
env.SetNSISEnv(nsisRoot)
|
|
|
|
}
|
2023-09-12 11:01:19 +08:00
|
|
|
|
2023-09-08 22:54:06 +08:00
|
|
|
// 安装upx, 内置, 仅windows, linux
|
|
|
|
upxRoot, upxSuccessCallback = installUPX(c)
|
|
|
|
// 设置upx环境变量
|
|
|
|
if upxRoot != "" {
|
|
|
|
env.SetUPXEnv(upxRoot)
|
|
|
|
}
|
2023-09-12 11:01:19 +08:00
|
|
|
|
|
|
|
// 安装7za
|
|
|
|
z7zRoot, z7zSuccessCallback = install7z(c)
|
|
|
|
// 设置7za环境变量
|
|
|
|
if z7zRoot != "" {
|
|
|
|
env.Set7zaEnv(z7zRoot)
|
2023-09-05 17:01:33 +08:00
|
|
|
}
|
2023-09-12 11:01:19 +08:00
|
|
|
|
2023-09-05 20:03:09 +08:00
|
|
|
// success 输出
|
2023-09-12 11:01:19 +08:00
|
|
|
if nsisSuccessCallback != nil || goSuccessCallback != nil || upxSuccessCallback != nil || cefFrameworkSuccessCallback != nil || z7zSuccessCallback != nil {
|
2023-09-10 21:57:00 +08:00
|
|
|
term.BoxPrintln("Hint: Reopen the cmd window for command to take effect.")
|
2023-09-06 15:03:26 +08:00
|
|
|
}
|
2023-09-12 11:01:19 +08:00
|
|
|
|
2023-09-05 20:03:09 +08:00
|
|
|
if nsisSuccessCallback != nil {
|
|
|
|
nsisSuccessCallback()
|
|
|
|
}
|
2023-09-12 11:01:19 +08:00
|
|
|
if cefFrameworkSuccessCallback != nil {
|
|
|
|
cefFrameworkSuccessCallback()
|
|
|
|
}
|
2023-09-05 20:03:09 +08:00
|
|
|
if goSuccessCallback != nil {
|
|
|
|
goSuccessCallback()
|
|
|
|
}
|
2023-09-08 22:54:06 +08:00
|
|
|
if upxSuccessCallback != nil {
|
|
|
|
upxSuccessCallback()
|
|
|
|
}
|
2023-09-12 11:01:19 +08:00
|
|
|
if z7zSuccessCallback != nil {
|
|
|
|
z7zSuccessCallback()
|
2023-09-05 20:03:09 +08:00
|
|
|
}
|
2023-09-12 17:05:44 +08:00
|
|
|
copyEnergyCMD(goRoot)
|
2023-09-10 21:57:00 +08:00
|
|
|
return nil
|
2023-09-04 20:00:53 +08:00
|
|
|
}
|
|
|
|
|
2023-09-12 17:05:44 +08:00
|
|
|
func copyEnergyCMD(goRoot string) {
|
|
|
|
if goRoot == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
exe, err := os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
term.Logger.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
energyName := "energy"
|
|
|
|
if consts.IsWindows {
|
|
|
|
energyName += ".exe"
|
|
|
|
}
|
|
|
|
goBin := filepath.Join(goRoot, "bin", energyName)
|
|
|
|
if tools.IsExist(goBin) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
src, err := os.Open(exe)
|
|
|
|
if err != nil {
|
|
|
|
term.Logger.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer src.Close()
|
|
|
|
dst, err := os.OpenFile(goBin, os.O_CREATE|os.O_WRONLY, fs.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
term.Logger.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer dst.Close()
|
|
|
|
_, err = io.Copy(dst, src)
|
|
|
|
if err != nil {
|
|
|
|
term.Logger.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-04 20:00:53 +08:00
|
|
|
func cefInstallPathName(c *command.Config) string {
|
2023-09-09 18:50:50 +08:00
|
|
|
if c.Install.IsSame {
|
|
|
|
return filepath.Join(c.Install.Path, consts.ENERGY, c.Install.Name)
|
|
|
|
} else {
|
|
|
|
return filepath.Join(c.Install.Path, consts.ENERGY, fmt.Sprintf("%s_%s%s", c.Install.Name, c.Install.OS, c.Install.Arch))
|
|
|
|
}
|
2023-09-04 20:00:53 +08:00
|
|
|
}
|
|
|
|
|
2023-09-05 19:09:15 +08:00
|
|
|
func goInstallPathName(c *command.Config) string {
|
2023-09-06 17:41:06 +08:00
|
|
|
return filepath.Join(c.Install.Path, consts.ENERGY, "go")
|
2023-09-05 19:09:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func nsisInstallPathName(c *command.Config) string {
|
2023-09-06 17:41:06 +08:00
|
|
|
return filepath.Join(c.Install.Path, consts.ENERGY, "nsis")
|
|
|
|
}
|
|
|
|
|
|
|
|
func upxInstallPathName(c *command.Config) string {
|
|
|
|
return filepath.Join(c.Install.Path, consts.ENERGY, "upx")
|
|
|
|
}
|
|
|
|
|
2023-09-12 11:01:19 +08:00
|
|
|
func z7zInstallPathName(c *command.Config) string {
|
|
|
|
return filepath.Join(c.Install.Path, consts.ENERGY, "7za")
|
|
|
|
}
|
|
|
|
|
|
|
|
func nsisCanInstall() bool {
|
2023-09-09 22:03:19 +08:00
|
|
|
return consts.IsWindows && !consts.IsARM64
|
2023-09-06 17:41:06 +08:00
|
|
|
}
|
|
|
|
|
2023-09-12 11:01:19 +08:00
|
|
|
func upxCanInstall() bool {
|
2023-09-09 22:03:19 +08:00
|
|
|
return (consts.IsWindows && !consts.IsARM64) || (consts.IsLinux)
|
2023-09-05 19:09:15 +08:00
|
|
|
}
|
|
|
|
|
2023-09-12 11:01:19 +08:00
|
|
|
func z7zCanInstall() bool {
|
|
|
|
return consts.IsWindows && !consts.IsARM64
|
|
|
|
}
|
|
|
|
|
2023-09-08 22:54:06 +08:00
|
|
|
// 检查当前环境
|
|
|
|
// golang, nsis, cef, upx
|
|
|
|
// golang: all os
|
|
|
|
// nsis: windows
|
|
|
|
// cef: all os
|
|
|
|
// upx: windows amd64, 386, linux amd64, arm64
|
2023-09-10 21:57:00 +08:00
|
|
|
func checkInstallEnv(c *command.Config) (result []*softEnf) {
|
|
|
|
result = make([]*softEnf, 0)
|
|
|
|
var check = func(chkInstall func() (string, bool), name string, yes func()) {
|
|
|
|
desc, ok := chkInstall()
|
|
|
|
result = append(result, &softEnf{name: name, desc: desc, installed: ok, yes: yes})
|
2023-09-05 22:30:04 +08:00
|
|
|
}
|
2023-09-09 20:16:40 +08:00
|
|
|
|
2023-09-05 22:30:04 +08:00
|
|
|
// go
|
2023-09-10 21:57:00 +08:00
|
|
|
check(func() (string, bool) {
|
|
|
|
return "All", tools.CommandExists("go")
|
2023-09-05 22:30:04 +08:00
|
|
|
}, "Golang", func() {
|
2023-09-12 11:01:19 +08:00
|
|
|
c.Install.IGolang = true //yes callback
|
2023-09-05 22:30:04 +08:00
|
|
|
})
|
2023-09-09 20:16:40 +08:00
|
|
|
|
2023-09-05 22:30:04 +08:00
|
|
|
// cef
|
2023-09-10 21:57:00 +08:00
|
|
|
var cefName = "CEF Framework"
|
|
|
|
if !c.Install.IsSame {
|
|
|
|
cefName = fmt.Sprintf("CEF Framework %s%s", c.Install.OS, c.Install.Arch)
|
|
|
|
}
|
|
|
|
check(func() (string, bool) {
|
2023-09-09 20:16:40 +08:00
|
|
|
if c.Install.IsSame {
|
|
|
|
// 检查环境变量是否配置
|
2023-09-10 21:57:00 +08:00
|
|
|
return "All", tools.CheckCEFDir()
|
2023-09-09 20:16:40 +08:00
|
|
|
}
|
|
|
|
// 非当月系统架构时检查一下目标安装路径是否已经存在
|
|
|
|
var lib = func() string {
|
|
|
|
if c.Install.OS.IsWindows() {
|
|
|
|
return "libcef.dll"
|
|
|
|
} else if c.Install.OS.IsLinux() {
|
|
|
|
return "libcef.so"
|
|
|
|
} else if c.Install.OS.IsDarwin() {
|
|
|
|
return "cef_sandbox.a"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}()
|
|
|
|
if lib != "" {
|
|
|
|
s := filepath.Join(cefInstallPathName(c), lib)
|
2023-09-10 21:57:00 +08:00
|
|
|
return "All", tools.IsExist(s)
|
2023-09-09 20:16:40 +08:00
|
|
|
} else {
|
2023-09-10 21:57:00 +08:00
|
|
|
return "Unsupported Platform", true
|
2023-09-09 20:16:40 +08:00
|
|
|
}
|
|
|
|
}, cefName, func() {
|
2023-09-12 11:01:19 +08:00
|
|
|
c.Install.ICEF = true //yes callback
|
2023-09-05 22:30:04 +08:00
|
|
|
})
|
2023-09-12 11:01:19 +08:00
|
|
|
if nsisCanInstall() {
|
2023-09-10 21:57:00 +08:00
|
|
|
// nsis
|
|
|
|
check(func() (string, bool) {
|
2023-09-12 11:01:19 +08:00
|
|
|
if nsisCanInstall() {
|
2023-09-10 21:57:00 +08:00
|
|
|
return "Windows AMD", tools.CommandExists("makensis")
|
|
|
|
} else {
|
|
|
|
return "Non Windows AMD skipping NSIS.", true
|
|
|
|
}
|
|
|
|
}, "NSIS", func() {
|
2023-09-12 11:01:19 +08:00
|
|
|
c.Install.INSIS = true //yes callback
|
2023-09-10 21:57:00 +08:00
|
|
|
})
|
|
|
|
}
|
2023-09-12 11:01:19 +08:00
|
|
|
if upxCanInstall() {
|
2023-09-10 21:57:00 +08:00
|
|
|
// upx
|
|
|
|
check(func() (string, bool) {
|
2023-09-12 11:01:19 +08:00
|
|
|
if upxCanInstall() {
|
2023-09-10 21:57:00 +08:00
|
|
|
return "Windows AMD, Linux", tools.CommandExists("upx")
|
|
|
|
} else {
|
|
|
|
return "Non Windows and Linux skipping UPX.", true
|
|
|
|
}
|
|
|
|
}, "UPX", func() {
|
2023-09-12 11:01:19 +08:00
|
|
|
c.Install.IUPX = true //yes callback
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if z7zCanInstall() {
|
|
|
|
// 7za
|
|
|
|
check(func() (string, bool) {
|
|
|
|
if z7zCanInstall() {
|
|
|
|
return "Windows AMD", tools.CommandExists("7za")
|
|
|
|
} else {
|
|
|
|
return "Non Windows skipping UPX.", true
|
|
|
|
}
|
|
|
|
}, "7za", func() {
|
|
|
|
c.Install.I7za = true //yes callback
|
2023-09-10 21:57:00 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if consts.IsLinux {
|
|
|
|
// gtk2
|
|
|
|
// gtk3
|
|
|
|
// dpkg
|
|
|
|
}
|
|
|
|
if consts.IsDarwin {
|
|
|
|
//
|
|
|
|
}
|
2023-09-05 22:30:04 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-04 20:00:53 +08:00
|
|
|
func initInstall(c *command.Config) {
|
|
|
|
if c.Install.Path == "" {
|
|
|
|
// current dir
|
|
|
|
c.Install.Path = c.Wd
|
|
|
|
}
|
|
|
|
if c.Install.Version == "" {
|
|
|
|
// latest
|
|
|
|
c.Install.Version = "latest"
|
|
|
|
}
|
2023-09-09 18:50:50 +08:00
|
|
|
if c.Install.OS == "" {
|
|
|
|
c.Install.OS = command.OS(runtime.GOOS)
|
|
|
|
}
|
|
|
|
if c.Install.Arch == "" {
|
|
|
|
c.Install.Arch = command.Arch(runtime.GOARCH)
|
|
|
|
}
|
|
|
|
if string(c.Install.OS) == runtime.GOOS && string(c.Install.Arch) == runtime.GOARCH {
|
|
|
|
c.Install.IsSame = true
|
|
|
|
}
|
2023-09-04 20:00:53 +08:00
|
|
|
// 创建安装目录
|
2023-09-12 11:01:19 +08:00
|
|
|
os.MkdirAll(c.Install.Path, fs.ModePerm) // framework root
|
|
|
|
os.MkdirAll(cefInstallPathName(c), fs.ModePerm) //cef
|
|
|
|
os.MkdirAll(goInstallPathName(c), fs.ModePerm) // go
|
|
|
|
if nsisCanInstall() {
|
|
|
|
os.MkdirAll(nsisInstallPathName(c), fs.ModePerm) // nsis
|
|
|
|
}
|
|
|
|
if upxCanInstall() {
|
|
|
|
os.MkdirAll(upxInstallPathName(c), fs.ModePerm) //upx
|
2023-09-06 17:41:06 +08:00
|
|
|
}
|
2023-09-12 11:01:19 +08:00
|
|
|
if z7zCanInstall() {
|
|
|
|
os.MkdirAll(z7zInstallPathName(c), fs.ModePerm) //upx
|
2023-09-06 17:41:06 +08:00
|
|
|
}
|
2023-09-12 11:01:19 +08:00
|
|
|
// framework download cace
|
2023-09-05 19:45:30 +08:00
|
|
|
os.MkdirAll(filepath.Join(c.Install.Path, consts.FrameworkCache), fs.ModePerm)
|
2023-09-04 20:00:53 +08:00
|
|
|
}
|
|
|
|
|
2023-09-04 11:36:36 +08:00
|
|
|
func filePathInclude(compressPath string, files ...any) (string, bool) {
|
2023-09-04 20:00:53 +08:00
|
|
|
if len(files) == 0 {
|
|
|
|
return compressPath, true
|
|
|
|
} else {
|
|
|
|
for _, file := range files {
|
|
|
|
f := file.(string)
|
|
|
|
tIdx := strings.LastIndex(f, "/*")
|
|
|
|
if tIdx != -1 {
|
|
|
|
f = f[:tIdx]
|
|
|
|
if f[0] == '/' {
|
|
|
|
if strings.Index(compressPath, f[1:]) == 0 {
|
|
|
|
return compressPath, true
|
|
|
|
}
|
2023-09-04 11:36:36 +08:00
|
|
|
}
|
2023-09-04 20:00:53 +08:00
|
|
|
if strings.Index(compressPath, f) == 0 {
|
|
|
|
return strings.Replace(compressPath, f, "", 1), true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if f[0] == '/' {
|
|
|
|
if compressPath == f[1:] {
|
|
|
|
return f, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if compressPath == f {
|
|
|
|
f = f[strings.Index(f, "/")+1:]
|
2023-09-04 11:36:36 +08:00
|
|
|
return f, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
func dir(path string) string {
|
|
|
|
path = strings.ReplaceAll(path, "\\", string(filepath.Separator))
|
|
|
|
lastSep := strings.LastIndex(path, string(filepath.Separator))
|
|
|
|
return path[:lastSep]
|
|
|
|
}
|
|
|
|
|
2023-09-06 17:41:06 +08:00
|
|
|
func tarFileReader(filePath string) (*tar.Reader, func(), error) {
|
2023-09-04 11:36:36 +08:00
|
|
|
reader, err := os.Open(filePath)
|
|
|
|
if err != nil {
|
2023-09-04 20:00:53 +08:00
|
|
|
fmt.Printf("error: cannot open file, error=[%v]\n", err)
|
2023-09-06 17:41:06 +08:00
|
|
|
return nil, nil, err
|
2023-09-04 11:36:36 +08:00
|
|
|
}
|
2023-09-04 20:00:53 +08:00
|
|
|
if filepath.Ext(filePath) == ".gz" {
|
|
|
|
gr, err := gzip.NewReader(reader)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error: cannot open gzip file, error=[%v]\n", err)
|
2023-09-06 17:41:06 +08:00
|
|
|
return nil, nil, err
|
2023-09-05 17:01:33 +08:00
|
|
|
}
|
|
|
|
return tar.NewReader(gr), func() {
|
|
|
|
gr.Close()
|
|
|
|
reader.Close()
|
2023-09-06 17:41:06 +08:00
|
|
|
}, nil
|
2023-09-04 20:00:53 +08:00
|
|
|
} else {
|
2023-09-05 17:01:33 +08:00
|
|
|
return tar.NewReader(reader), func() {
|
|
|
|
reader.Close()
|
2023-09-06 17:41:06 +08:00
|
|
|
}, nil
|
2023-09-04 20:00:53 +08:00
|
|
|
}
|
2023-09-05 17:01:33 +08:00
|
|
|
}
|
2023-09-04 20:00:53 +08:00
|
|
|
|
2023-09-11 15:28:02 +08:00
|
|
|
func tarFileCount(filePath string) (int, error) {
|
|
|
|
tarReader, clos, err := tarFileReader(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer clos()
|
|
|
|
var count int
|
|
|
|
for {
|
|
|
|
_, err := tarReader.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
|
2023-09-06 17:41:06 +08:00
|
|
|
func ExtractUnTar(filePath, targetPath string, files ...any) error {
|
2023-09-11 15:28:02 +08:00
|
|
|
term.Logger.Info("Read Files Number")
|
|
|
|
fileCount, err := tarFileCount(filePath)
|
|
|
|
println(fileCount)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tarReader, clos, err := tarFileReader(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer clos()
|
|
|
|
multi := pterm.DefaultMultiPrinter
|
|
|
|
defer multi.Stop()
|
|
|
|
fileTotalProcessBar := pterm.DefaultProgressbar.WithWriter(multi.NewWriter())
|
|
|
|
writeFileProcessBar := pterm.DefaultProgressbar.WithWriter(multi.NewWriter())
|
|
|
|
extractFilesProcessBar, err := fileTotalProcessBar.WithTotal(fileCount).Start("Extract File")
|
2023-09-06 17:41:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-09-05 17:01:33 +08:00
|
|
|
}
|
2023-09-11 15:28:02 +08:00
|
|
|
multi.Start()
|
2023-09-04 11:36:36 +08:00
|
|
|
for {
|
|
|
|
header, err := tarReader.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
2023-09-06 17:41:06 +08:00
|
|
|
return err
|
2023-09-04 11:36:36 +08:00
|
|
|
}
|
2023-09-11 15:28:02 +08:00
|
|
|
extractFilesProcessBar.Increment()
|
2023-09-04 20:00:53 +08:00
|
|
|
// 去除压缩包内的一级目录
|
|
|
|
compressPath := filepath.Clean(header.Name[strings.Index(header.Name, "/")+1:])
|
2023-09-04 11:36:36 +08:00
|
|
|
includePath, isInclude := filePathInclude(compressPath, files...)
|
|
|
|
if !isInclude {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
info := header.FileInfo()
|
|
|
|
targetFile := filepath.Join(targetPath, includePath)
|
|
|
|
if info.IsDir() {
|
|
|
|
if err = os.MkdirAll(targetFile, info.Mode()); err != nil {
|
2023-09-06 17:41:06 +08:00
|
|
|
return err
|
2023-09-04 11:36:36 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fDir := dir(targetFile)
|
|
|
|
_, err = os.Stat(fDir)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(fDir, info.Mode()); err != nil {
|
2023-09-06 17:41:06 +08:00
|
|
|
return err
|
2023-09-04 11:36:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
file, err := os.OpenFile(targetFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, info.Mode())
|
|
|
|
if err != nil {
|
2023-09-06 17:41:06 +08:00
|
|
|
return err
|
2023-09-04 11:36:36 +08:00
|
|
|
}
|
2023-09-11 15:52:47 +08:00
|
|
|
var (
|
|
|
|
total = 100
|
|
|
|
c int
|
|
|
|
cn int
|
|
|
|
)
|
2023-09-11 15:28:02 +08:00
|
|
|
_, fileName := filepath.Split(targetFile)
|
2023-09-11 15:52:47 +08:00
|
|
|
wfpb, err := writeFileProcessBar.WithCurrent(0).WithTotal(total).Start("Write File " + fileName)
|
2023-09-11 15:28:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-04 11:36:36 +08:00
|
|
|
writeFile(tarReader, file, header.Size, func(totalLength, processLength int64) {
|
2023-09-11 15:28:02 +08:00
|
|
|
process := int((float64(processLength) / float64(totalLength)) * 100)
|
|
|
|
if process > c {
|
|
|
|
c = process
|
|
|
|
wfpb.Add(process)
|
2023-09-11 15:52:47 +08:00
|
|
|
cn++
|
2023-09-11 15:28:02 +08:00
|
|
|
}
|
2023-09-04 11:36:36 +08:00
|
|
|
})
|
2023-09-11 15:52:47 +08:00
|
|
|
if cn < total {
|
|
|
|
wfpb.Add(total - cn)
|
|
|
|
}
|
2023-09-11 15:28:02 +08:00
|
|
|
wfpb.Stop()
|
2023-09-04 11:36:36 +08:00
|
|
|
file.Sync()
|
2023-09-04 20:00:53 +08:00
|
|
|
file.Close()
|
2023-09-04 11:36:36 +08:00
|
|
|
if err != nil {
|
2023-09-06 17:41:06 +08:00
|
|
|
return err
|
2023-09-04 11:36:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-06 17:41:06 +08:00
|
|
|
return nil
|
2023-09-04 11:36:36 +08:00
|
|
|
}
|
|
|
|
|
2023-09-06 17:41:06 +08:00
|
|
|
func ExtractUnZip(filePath, targetPath string, rmRootDir bool, files ...any) error {
|
2023-09-04 11:36:36 +08:00
|
|
|
if rc, err := zip.OpenReader(filePath); err == nil {
|
|
|
|
defer rc.Close()
|
2023-09-11 15:28:02 +08:00
|
|
|
multi := pterm.DefaultMultiPrinter
|
|
|
|
defer multi.Stop()
|
|
|
|
fileTotalProcessBar := pterm.DefaultProgressbar.WithWriter(multi.NewWriter())
|
|
|
|
writeFileProcessBar := pterm.DefaultProgressbar.WithWriter(multi.NewWriter())
|
2023-09-06 17:41:06 +08:00
|
|
|
var createWriteFile = func(info fs.FileInfo, path string, file io.Reader) error {
|
2023-09-04 20:00:53 +08:00
|
|
|
targetFileName := filepath.Join(targetPath, path)
|
|
|
|
if info.IsDir() {
|
|
|
|
os.MkdirAll(targetFileName, info.Mode())
|
2023-09-06 17:41:06 +08:00
|
|
|
return nil
|
2023-09-04 11:36:36 +08:00
|
|
|
}
|
2023-09-11 15:28:02 +08:00
|
|
|
fDir, name := filepath.Split(targetFileName)
|
2023-09-05 19:09:15 +08:00
|
|
|
if !tools.IsExist(fDir) {
|
|
|
|
os.MkdirAll(fDir, 0755)
|
|
|
|
}
|
2023-09-04 20:00:53 +08:00
|
|
|
if targetFile, err := os.Create(targetFileName); err == nil {
|
2023-09-08 22:54:06 +08:00
|
|
|
defer targetFile.Close()
|
2023-09-11 15:52:47 +08:00
|
|
|
var (
|
|
|
|
total = 100
|
|
|
|
c int
|
|
|
|
cn int
|
|
|
|
)
|
|
|
|
wfpb, err := writeFileProcessBar.WithCurrent(0).WithTotal(total).Start("Write File " + name)
|
2023-09-11 15:28:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-04 20:00:53 +08:00
|
|
|
writeFile(file, targetFile, info.Size(), func(totalLength, processLength int64) {
|
2023-09-11 15:28:02 +08:00
|
|
|
process := int((float64(processLength) / float64(totalLength)) * 100)
|
|
|
|
if process > c {
|
|
|
|
c = process
|
|
|
|
wfpb.Add(process)
|
2023-09-11 15:52:47 +08:00
|
|
|
cn++
|
2023-09-11 15:28:02 +08:00
|
|
|
}
|
2023-09-04 20:00:53 +08:00
|
|
|
})
|
2023-09-11 15:52:47 +08:00
|
|
|
if cn < total {
|
|
|
|
wfpb.Add(total - cn)
|
|
|
|
}
|
2023-09-11 15:28:02 +08:00
|
|
|
wfpb.Stop()
|
2023-09-06 17:41:06 +08:00
|
|
|
return nil
|
2023-09-05 19:09:15 +08:00
|
|
|
} else {
|
2023-09-06 17:41:06 +08:00
|
|
|
return err
|
2023-09-04 20:00:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// 所有文件
|
|
|
|
if len(files) == 0 {
|
|
|
|
zipFiles := rc.File
|
2023-09-11 15:28:02 +08:00
|
|
|
extractFilesProcessBar, err := fileTotalProcessBar.WithTotal(len(zipFiles)).Start("Extract File")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
multi.Start()
|
2023-09-04 20:00:53 +08:00
|
|
|
for _, f := range zipFiles {
|
2023-09-11 15:28:02 +08:00
|
|
|
extractFilesProcessBar.Increment() // +1
|
2023-09-04 20:00:53 +08:00
|
|
|
r, _ := f.Open()
|
2023-09-05 19:09:15 +08:00
|
|
|
var name string
|
|
|
|
if rmRootDir {
|
|
|
|
// 移除压缩包内的根文件夹名
|
|
|
|
name = filepath.Clean(f.Name[strings.Index(f.Name, "/")+1:])
|
|
|
|
} else {
|
|
|
|
name = filepath.Clean(f.Name)
|
|
|
|
}
|
2023-09-06 17:41:06 +08:00
|
|
|
if err := createWriteFile(f.FileInfo(), name, r.(io.Reader)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-04 20:00:53 +08:00
|
|
|
_ = r.Close()
|
|
|
|
}
|
2023-09-06 17:41:06 +08:00
|
|
|
return nil
|
2023-09-04 20:00:53 +08:00
|
|
|
} else {
|
2023-09-11 15:28:02 +08:00
|
|
|
extractFilesProcessBar, err := fileTotalProcessBar.WithTotal(len(files)).Start("Extract File")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
multi.Start()
|
2023-09-04 20:00:53 +08:00
|
|
|
// 指定名字的文件
|
|
|
|
for i := 0; i < len(files); i++ {
|
2023-09-11 15:28:02 +08:00
|
|
|
extractFilesProcessBar.Increment() // +1
|
2023-09-04 20:00:53 +08:00
|
|
|
if f, err := rc.Open(files[i].(string)); err == nil {
|
|
|
|
info, _ := f.Stat()
|
2023-09-06 17:41:06 +08:00
|
|
|
if err := createWriteFile(info, files[i].(string), f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-04 20:00:53 +08:00
|
|
|
_ = f.Close()
|
|
|
|
} else {
|
2023-09-06 17:41:06 +08:00
|
|
|
return err
|
2023-09-04 20:00:53 +08:00
|
|
|
}
|
|
|
|
}
|
2023-09-06 17:41:06 +08:00
|
|
|
return nil
|
2023-09-04 11:36:36 +08:00
|
|
|
}
|
|
|
|
} else {
|
2023-09-06 17:41:06 +08:00
|
|
|
return err
|
2023-09-04 11:36:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 释放bz2文件到tar
|
2023-09-06 17:41:06 +08:00
|
|
|
func UnBz2ToTar(name string, callback func(totalLength, processLength int64)) (string, error) {
|
2023-09-04 11:36:36 +08:00
|
|
|
fileBz2, err := os.Open(name)
|
|
|
|
if err != nil {
|
2023-09-06 17:41:06 +08:00
|
|
|
return "", err
|
2023-09-04 11:36:36 +08:00
|
|
|
}
|
|
|
|
defer fileBz2.Close()
|
|
|
|
dirName := fileBz2.Name()
|
|
|
|
dirName = dirName[:strings.LastIndex(dirName, ".")]
|
2023-09-04 20:00:53 +08:00
|
|
|
if !tools.IsExist(dirName) {
|
|
|
|
r := bzip2.NewReader(fileBz2)
|
|
|
|
w, err := os.Create(dirName)
|
|
|
|
if err != nil {
|
2023-09-06 17:41:06 +08:00
|
|
|
return "", err
|
2023-09-04 20:00:53 +08:00
|
|
|
}
|
|
|
|
defer w.Close()
|
|
|
|
writeFile(r, w, 0, callback)
|
|
|
|
} else {
|
2023-09-11 15:28:02 +08:00
|
|
|
term.Section.Println("File already exists")
|
2023-09-04 11:36:36 +08:00
|
|
|
}
|
2023-09-06 17:41:06 +08:00
|
|
|
return dirName, nil
|
2023-09-04 11:36:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeFile(r io.Reader, w *os.File, totalLength int64, callback func(totalLength, processLength int64)) {
|
|
|
|
buf := make([]byte, 1024*10)
|
|
|
|
var written int64
|
|
|
|
for {
|
|
|
|
nr, err := r.Read(buf)
|
|
|
|
if nr > 0 {
|
|
|
|
nw, err := w.Write(buf[0:nr])
|
|
|
|
if nw > 0 {
|
|
|
|
written += int64(nw)
|
|
|
|
}
|
|
|
|
callback(totalLength, written)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if nr != nw {
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func isFileExist(filename string, filesize int64) bool {
|
|
|
|
info, err := os.Stat(filename)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if filesize == info.Size() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
os.Remove(filename)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-09-11 15:28:02 +08:00
|
|
|
// DownloadFile 下载文件
|
|
|
|
// 如果文件存在大小一样不再下载
|
|
|
|
func DownloadFile(url string, localPath string, callback func(totalLength, processLength int64)) error {
|
2023-09-04 11:36:36 +08:00
|
|
|
var (
|
|
|
|
fsize int64
|
|
|
|
buf = make([]byte, 1024*10)
|
|
|
|
written int64
|
|
|
|
)
|
|
|
|
tmpFilePath := localPath + ".download"
|
|
|
|
client := new(http.Client)
|
|
|
|
resp, err := client.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fsize, err = strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if isFileExist(localPath, fsize) {
|
2023-09-11 15:28:02 +08:00
|
|
|
term.Section.Println("File already exists")
|
2023-09-04 11:36:36 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
file, err := os.Create(tmpFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
if resp.Body == nil {
|
2023-09-06 17:41:06 +08:00
|
|
|
return errors.New("body is null")
|
2023-09-04 11:36:36 +08:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2023-09-11 15:52:47 +08:00
|
|
|
total := 100
|
2023-09-11 15:28:02 +08:00
|
|
|
_, fileName := filepath.Split(localPath)
|
2023-09-11 15:52:47 +08:00
|
|
|
p, err := pterm.DefaultProgressbar.WithTotal(total).WithTitle("Download " + fileName).Start()
|
2023-09-11 15:28:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
p.Stop()
|
|
|
|
}()
|
2023-09-11 15:52:47 +08:00
|
|
|
var (
|
|
|
|
count int
|
|
|
|
cn int
|
|
|
|
)
|
2023-09-11 15:28:02 +08:00
|
|
|
var nw int
|
2023-09-04 11:36:36 +08:00
|
|
|
for {
|
|
|
|
nr, er := resp.Body.Read(buf)
|
|
|
|
if nr > 0 {
|
2023-09-11 15:28:02 +08:00
|
|
|
nw, err = file.Write(buf[0:nr])
|
2023-09-04 11:36:36 +08:00
|
|
|
if nw > 0 {
|
|
|
|
written += int64(nw)
|
|
|
|
}
|
2023-09-11 15:28:02 +08:00
|
|
|
if callback != nil {
|
|
|
|
callback(fsize, written)
|
|
|
|
} else {
|
|
|
|
process := int((float64(written) / float64(fsize)) * 100)
|
|
|
|
if process > count {
|
|
|
|
count = process
|
|
|
|
p.Add(1)
|
2023-09-11 15:52:47 +08:00
|
|
|
cn++
|
2023-09-11 15:28:02 +08:00
|
|
|
}
|
|
|
|
}
|
2023-09-04 11:36:36 +08:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if nr != nw {
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if er != nil {
|
|
|
|
if er != io.EOF {
|
|
|
|
err = er
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-09-11 15:52:47 +08:00
|
|
|
if cn < total {
|
|
|
|
p.Add(total - cn)
|
|
|
|
}
|
|
|
|
p.Stop()
|
2023-09-04 11:36:36 +08:00
|
|
|
if err == nil {
|
|
|
|
file.Sync()
|
|
|
|
file.Close()
|
|
|
|
err = os.Rename(tmpFilePath, localPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|