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.

82 lines
2.1 KiB
Go

2 years ago
package cli
import (
"errors"
"fmt"
"os"
"path/filepath"
2 years 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"
2 years ago
//"github.com/mholt/archiver/v3"
"github.com/urfave/cli/v2"
)
2 years ago
func fundVersion(version string) *collector.GradleItem {
rs := collector.Collector.Items
for _, v := range rs {
if v.Version == version { //strings.Contains(v.SimpleName, version)
2 years 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)
}
2 years ago
filename := filepath.Join(downloadsDir, ui.FileName)
2 years ago
//判断本地有没有安装包 没有就进入下载
if _, err := os.Stat(filename); err != nil {
2 years ago
DownloadWithProgress(ui.DownloadUrl, filename)
2 years ago
} else {
2 years ago
if ui.Sha256 != check.PrintSha256(filename) {
DownloadWithProgress(ui.DownloadUrl, filename)
2 years ago
}
}
//获取解压目标目录
2 years ago
targetV := filepath.Join(versionsDir, ui.Version)
2 years 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)
}
2 years ago
fmt.Printf("Now using %s\n", ui.Version+" Realse "+ui.ReleaseTime)
2 years 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)
}