You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.1 KiB
Go

1 year ago
package cli
import (
"errors"
"fmt"
"os"
"path/filepath"
1 year ago
"github.com/forget-the-bright/grvm/internal/pkg/archiver"
"github.com/forget-the-bright/grvm/internal/pkg/check"
"github.com/forget-the-bright/grvm/internal/pkg/collector"
"github.com/forget-the-bright/grvm/internal/pkg/download"
1 year ago
//"github.com/mholt/archiver/v3"
"github.com/urfave/cli/v2"
)
1 year ago
func fundVersion(version string) *collector.GradleItem {
1 year ago
collector.Init()
1 year ago
rs := collector.Collector.Items
for _, v := range rs {
if v.Version == version { //strings.Contains(v.SimpleName, version)
1 year ago
return v
}
}
return nil
}
func downloadAndInstall(version string) (err error) {
ui := fundVersion(version)
if ui == nil {
return cli.Exit(errors.New(version+" version is not supported"), 1)
}
1 year ago
filename := filepath.Join(downloadsDir, ui.FileName)
1 year ago
//判断本地有没有安装包 没有就进入下载
if _, err := os.Stat(filename); err != nil {
1 year ago
DownloadWithProgress(ui.DownloadUrl, filename)
1 year ago
} else {
1 year ago
if ui.Sha256 != check.PrintSha256(filename) {
DownloadWithProgress(ui.DownloadUrl, filename)
1 year ago
}
}
//获取解压目标目录
1 year ago
targetV := filepath.Join(versionsDir, ui.Version)
1 year ago
// 检查版本是否已经安装
if finfo, err := os.Stat(targetV); err == nil && finfo.IsDir() {
return cli.Exit(fmt.Sprintf("[grvm] %q version has been installed.", version), 1)
}
// 解压安装包
if err = archiver.Unarchive(filename, targetV, true); err != nil {
return cli.Exit(errstring(err), 1)
}
// 重新建立软链接
_ = os.Remove(goroot)
if err = mkSymlink(targetV, goroot); err != nil {
return cli.Exit(errstring(err), 1)
}
1 year ago
fmt.Printf("Now using %s\n", ui.Version+" Realse "+ui.ReleaseTime)
1 year ago
return nil
}
func install(ctx *cli.Context) (err error) {
version := ctx.Args().First()
if version == "" {
return cli.ShowSubcommandHelp(ctx)
}
return downloadAndInstall(version)
}
func Install(version string) (err error) {
return downloadAndInstall(version)
}
// DownloadWithProgress 下载版本另存为指定文件且显示下载进度
func DownloadWithProgress(url, dst string) (size int64, err error) {
return download.Download(url, dst, os.O_CREATE|os.O_WRONLY, 0644, true)
}