From b7b913bc747d0921793e8fb8951c46ff0b250aa5 Mon Sep 17 00:00:00 2001 From: 15128022404 <1421485150@qq.com> Date: Fri, 31 Mar 2023 22:57:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 2 +- README.md | 2 +- cli/cli.go | 13 +- cli/install.go | 42 ++-- cli/ls-remote.go | 22 +- cli/ls_all.go | 2 +- go.mod | 2 +- internal/pkg/collector/collector.go | 54 +++-- internal/pkg/collector/gradle_collector.go | 37 +++ .../pkg/collector/gradle_collector_test.go | 66 +++++- internal/pkg/collector/openjdk_collector.go | 215 ------------------ .../pkg/collector/openjdk_collector_test.go | 188 --------------- internal/pkg/config/urls.go | 2 +- internal/pkg/download/download.go | 5 +- internal/pkg/proxy/proxy.go | 49 ++++ main.go | 2 +- makefile | 2 +- 17 files changed, 211 insertions(+), 494 deletions(-) create mode 100644 internal/pkg/collector/gradle_collector.go delete mode 100644 internal/pkg/collector/openjdk_collector.go delete mode 100644 internal/pkg/collector/openjdk_collector_test.go create mode 100644 internal/pkg/proxy/proxy.go diff --git a/.vscode/launch.json b/.vscode/launch.json index 95af279..20b2cab 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "request": "launch", "mode": "auto", "program": "${fileDirname}", - "args": ["ls-remote" ] + "args": ["install","6.8.3" ] } ] } \ No newline at end of file diff --git a/README.md b/README.md index d8d1dd8..94db895 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Gradle_Version_Manager ## 安装 - [下载](https://github.com/forget-the-bright/j/releases) 下载自己需要的版本, 到自己自定义的目录 修改可执行文件名称为j + [下载](https://github.com/forget-the-bright/grvm/releases) 下载自己需要的版本, 到自己自定义的目录 修改可执行文件名称为j 默认文件下载安装在用户目录下 ```.grvm```目录,目录下 ```versions```, ```downloads```, ```gradle``` 分别是本地安装目录,安装包下载目录,当前使用的java版本目录 diff --git a/cli/cli.go b/cli/cli.go index 7d2c815..3f92fb9 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -13,7 +13,7 @@ import ( "github.com/Masterminds/semver" "github.com/fatih/color" - "github.com/forget-the-bright/j/internal/build" + "github.com/forget-the-bright/grvm/internal/build" "github.com/urfave/cli/v2" ) @@ -25,15 +25,6 @@ var ( ) func init() { - /* ghomeDir, _ = os.Getwd() - fmt.Println(ghomeDir) - goroot = filepath.Join(ghomeDir, "java") - fmt.Println(goroot) - downloadsDir = filepath.Join(ghomeDir, "downloads") - os.MkdirAll(downloadsDir, 0755) - versionsDir = filepath.Join(ghomeDir, "versions") - os.MkdirAll(versionsDir, 0755) */ - cli.AppHelpTemplate = fmt.Sprintf(`NAME: {{.Name}}{{if .Usage}} - {{.Usage}}{{end}} @@ -106,7 +97,7 @@ func ghome() (dir string) { return dir } homeDir, _ := os.UserHomeDir() - return filepath.Join(homeDir, ".j") + return filepath.Join(homeDir, ".grvm") } diff --git a/cli/install.go b/cli/install.go index f9ea508..8fff7af 100644 --- a/cli/install.go +++ b/cli/install.go @@ -6,23 +6,19 @@ import ( "os" "path/filepath" - "github.com/forget-the-bright/j/internal/pkg/archiver" - "github.com/forget-the-bright/j/internal/pkg/check" - "github.com/forget-the-bright/j/internal/pkg/collector" - "github.com/forget-the-bright/j/internal/pkg/config" - "github.com/forget-the-bright/j/internal/pkg/download" + "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" //"github.com/mholt/archiver/v3" "github.com/urfave/cli/v2" ) -func fundVersion(version string) *config.UrlItem { - config.Url_Items = collector.ConvertCollectorToUrlItem(collector.GetOpenJDKArchiveReleasesInfo(), false) - for _, v := range config.Url_Items { - if v.SimpleName == version { //strings.Contains(v.SimpleName, version) - if version != "8" { - v.In.Sha256 = collector.GetSha256ByUrl(v.In.Sha256, true) - } +func fundVersion(version string) *collector.GradleItem { + rs := collector.Collector.Items + for _, v := range rs { + if v.Version == version { //strings.Contains(v.SimpleName, version) return v } } @@ -35,18 +31,18 @@ func downloadAndInstall(version string) (err error) { return cli.Exit(errors.New(version+" version is not supported"), 1) } - filename := filepath.Join(downloadsDir, ui.In.FileName) + filename := filepath.Join(downloadsDir, ui.FileName) //判断本地有没有安装包 没有就进入下载 if _, err := os.Stat(filename); err != nil { - DownloadWithProgress(ui.In.URL, filename) + DownloadWithProgress(ui.DownloadUrl, filename) } else { - if ui.In.Sha256 != check.PrintSha256(filename) { - DownloadWithProgress(ui.In.URL, filename) + if ui.Sha256 != check.PrintSha256(filename) { + DownloadWithProgress(ui.DownloadUrl, filename) } } //获取解压目标目录 - targetV := filepath.Join(versionsDir, ui.SimpleName) + targetV := filepath.Join(versionsDir, ui.Version) // 检查版本是否已经安装 if finfo, err := os.Stat(targetV); err == nil && finfo.IsDir() { @@ -56,23 +52,13 @@ func downloadAndInstall(version string) (err error) { if err = archiver.Unarchive(filename, targetV, true); err != nil { return cli.Exit(errstring(err), 1) } - /* // 解压安装包 - if err = archiver.Unarchive(filename, versionsDir); err != nil { - fmt.Println(err.Error()) - return cli.Exit(errstring(err), 1) - } - // 目录重命名 - if err = os.Rename(filepath.Join(versionsDir, ui.Expected), targetV); err != nil { - fmt.Println(err.Error()) - return cli.Exit(errstring(err), 1) - } */ // 重新建立软链接 _ = os.Remove(goroot) if err = mkSymlink(targetV, goroot); err != nil { return cli.Exit(errstring(err), 1) } - fmt.Printf("Now using %s\n", ui.Expected) + fmt.Printf("Now using %s\n", ui.Version+" Realse "+ui.ReleaseTime) return nil } diff --git a/cli/ls-remote.go b/cli/ls-remote.go index 625321d..d5e39e5 100644 --- a/cli/ls-remote.go +++ b/cli/ls-remote.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/fatih/color" - "github.com/forget-the-bright/j/internal/pkg/collector" + "github.com/forget-the-bright/grvm/internal/pkg/collector" "github.com/k0kubun/go-ansi" "github.com/urfave/cli/v2" ) @@ -17,16 +17,26 @@ func remoteVersionLength(version string) string { return version } +func remoteVersionLength2(version string) string { + yu := 17 - len(version) + for i := 0; i < yu; i++ { + version += " " + } + return version +} + func listRemote(*cli.Context) (err error) { use_version := inuse(goroot) out := ansi.NewAnsiStdout() - rs := collector.ConvertCollectorToUrlItem(collector.GetOpenJDKArchiveReleasesInfo(), false) - color.New(color.FgGreen).Fprintf(out, " %s\n", " version info") + rs := collector.Collector.Items + color.New(color.FgGreen).Fprintf(out, " %s\n", " version info RelaseTime") for _, v := range rs { - if v.SimpleName == use_version { //strings.Contains(v.SimpleName, version) - color.New(color.FgGreen).Fprintf(out, "* %s\n", remoteVersionLength(v.SimpleName)+" "+v.Expected) + if v.Version == use_version { //strings.Contains(v.SimpleName, version) + color.New(color.FgGreen).Fprintf(out, "* %s\n", remoteVersionLength(v.Version)+" "+ + remoteVersionLength2(collector.GetFileNameNoSuffix(v.FileName))+" "+v.ReleaseTime) } else { - fmt.Fprintf(out, " %s\n", remoteVersionLength(v.SimpleName)+" "+v.Expected) + fmt.Fprintf(out, " %s\n", remoteVersionLength(v.Version)+" "+ + remoteVersionLength2(collector.GetFileNameNoSuffix(v.FileName))+" "+v.ReleaseTime) } } return nil diff --git a/cli/ls_all.go b/cli/ls_all.go index 1d4c017..9ea1618 100644 --- a/cli/ls_all.go +++ b/cli/ls_all.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/fatih/color" - "github.com/forget-the-bright/j/internal/pkg/config" + "github.com/forget-the-bright/grvm/internal/pkg/config" "github.com/k0kubun/go-ansi" "github.com/urfave/cli/v2" ) diff --git a/go.mod b/go.mod index f2c8281..bc719d4 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/forget-the-bright/j +module github.com/forget-the-bright/grvm go 1.20 diff --git a/internal/pkg/collector/collector.go b/internal/pkg/collector/collector.go index 3435f9c..395ab5b 100644 --- a/internal/pkg/collector/collector.go +++ b/internal/pkg/collector/collector.go @@ -6,36 +6,40 @@ import ( "strings" ) -type Collector struct { - Version string - Windows_X64 *Op_Item - Linux_X64 *Op_Item - Linux_AArch64 *Op_Item - Mac_X64 *Op_Item - Mac_AArch64 *Op_Item +var Collector *GradleCollector + +type GradleCollector struct { + Items []*GradleItem } -type Op_Item struct { - FileType string - Arch string - Url string - Sha256Url string - FileName string +type GradleItem struct { + Version string + ReleaseTime string + FileName string + FileType string + Sha256 string + Sha256Url string + DownloadUrl string } -var Archive_Releases_Collectors []*Collector -var Collectors []*Collector +func init() { + Collector = &GradleCollector{ + Items: getGradleAllInfo(), + } +} -var Collector_Archive_Url string = Collector_Url + "/archive/" -var Collector_Url string = "https://jdk.java.net" +var Collector_Release_Checksums string = "https://gradle.org/release-checksums" +var Collector_Archive_Url string = "https://gradle.org/releases/" -func build_Op_Item(file_type, arch, download_url, sha256_url, file_name string) *Op_Item { - return &Op_Item{ - FileType: file_type, - Arch: arch, - Url: download_url, - Sha256Url: sha256_url, - FileName: file_name, +func build_GradleItem(version, version_time, sha256 string) *GradleItem { + return &GradleItem{ + Version: version, + ReleaseTime: version_time, + FileName: "gradle-" + version + "-bin.zip", + FileType: "zip", + Sha256: sha256, + Sha256Url: "https://downloads.gradle-dn.com/distributions/gradle-" + version + "-bin.zip.sha256", + DownloadUrl: "https://downloads.gradle-dn.com/distributions/gradle-" + version + "-bin.zip", } } @@ -44,7 +48,7 @@ func getFileNameByDownLoadUrl(url string) string { file_name := downloads[len(downloads)-1] return file_name } -func getFileNameNoSuffix(file_name string) string { +func GetFileNameNoSuffix(file_name string) string { return strings.ReplaceAll(file_name, "."+getFileTypeByFileName(file_name), "") } diff --git a/internal/pkg/collector/gradle_collector.go b/internal/pkg/collector/gradle_collector.go new file mode 100644 index 0000000..8ed5683 --- /dev/null +++ b/internal/pkg/collector/gradle_collector.go @@ -0,0 +1,37 @@ +package collector + +import ( + "github.com/PuerkitoBio/goquery" + "github.com/forget-the-bright/grvm/internal/pkg/config" + "github.com/forget-the-bright/grvm/internal/pkg/proxy" +) + +func getGradleAllInfo() []*GradleItem { + resp, _ := proxy.HttpGetByProxy(Collector_Archive_Url) + resp_checksums, _ := proxy.HttpGetByProxy(Collector_Release_Checksums) + + defer resp.Body.Close() + defer resp_checksums.Body.Close() + doc_selector, _ := goquery.NewDocumentFromReader(resp.Body) + doc__checksums_selector, _ := goquery.NewDocumentFromReader(resp_checksums.Body) + + grvms := make([]*GradleItem, 0) + //获取所有的sha256 + lis := doc__checksums_selector.Find(".layout__main").Find("ul[style]") + + resources_docs := doc_selector.Find(".resources-contents") + //获取所有的版本号 + a_docs := resources_docs.Find("a[name]") + //获取所有的发布时间 + version_times := resources_docs.Find("p[class='u-text-with-icon u-no-margin-bottom u-no-margin-top']") + + a_docs.Each(func(j int, a_doc *goquery.Selection) { + version := a_doc.AttrOr("name", "") + sha256 := lis.Eq(j).Find("li").Eq(0).Find("code").Text() + version_time := version_times.Eq(j).Find("span").Eq(1).Text() + //fmt.Printf("version: %v time: %v sha256: %v\n", version, "", sha256) + grvms = append(grvms, build_GradleItem(version, version_time, sha256)) + }) + //fmt.Printf("len(grvms): %v\n", len(grvms)) + return config.ReverseArray(grvms) +} diff --git a/internal/pkg/collector/gradle_collector_test.go b/internal/pkg/collector/gradle_collector_test.go index 8947398..3e40b05 100644 --- a/internal/pkg/collector/gradle_collector_test.go +++ b/internal/pkg/collector/gradle_collector_test.go @@ -20,6 +20,45 @@ func Test_GetGradleSha256ByUrl(t *testing.T) { } +func Test_getGradleAllInfo(t *testing.T) { + resp, _ := httpGetByProxy("https://gradle.org/releases/") + resp_checksums, _ := httpGetByProxy("https://gradle.org/release-checksums/") + //resp, _ := http.Get("https://gradle.org/releases/") + defer resp.Body.Close() + defer resp_checksums.Body.Close() + doc_selector, _ := goquery.NewDocumentFromReader(resp.Body) + doc__checksums_selector, _ := goquery.NewDocumentFromReader(resp_checksums.Body) + t.Run("", func(t *testing.T) { + grvms := make([]*GradleItem, 0) + //获取所有的sha256 + lis := doc__checksums_selector.Find(".layout__main").Find("ul[style]") + + resources_docs := doc_selector.Find(".resources-contents") + //获取所有的版本号 + a_docs := resources_docs.Find("a[name]") + //获取所有的发布时间 + version_times := resources_docs.Find("p[class='u-text-with-icon u-no-margin-bottom u-no-margin-top']") + + a_docs.Each(func(j int, a_doc *goquery.Selection) { + version := a_doc.AttrOr("name", "") + sha256 := lis.Eq(j).Find("li").Eq(0).Find("code").Text() + version_time := version_times.Eq(j).Find("span").Eq(1).Text() + fmt.Printf("version: %v time: %v sha256: %v\n", version, "", sha256) + grvms = append(grvms, &GradleItem{ + Version: version, + ReleaseTime: version_time, + FileName: "gradle-" + version + "-bin.zip", + FileType: "zip", + Sha256: sha256, + Sha256Url: "https://downloads.gradle-dn.com/distributions/gradle-" + version + "-bin.zip.sha256", + DownloadUrl: "https://downloads.gradle-dn.com/distributions/gradle-" + version + "-bin.zip", + }) + }) + fmt.Printf("len(grvms): %v\n", len(grvms)) + }) + +} + func Test_getCollectorVersionShaSum(t *testing.T) { resp, _ := httpGetByProxy("https://gradle.org/release-checksums/") @@ -31,28 +70,31 @@ func Test_getCollectorVersionShaSum(t *testing.T) { maps := make(map[string]string) doc_selector, _ := goquery.NewDocumentFromReader(resp.Body) t.Run("", func(t *testing.T) { + grvms := make([]*GradleItem, 0) docs := doc_selector.Find(".layout__main") a_docs := docs.Find("a[name]") a_docs.Each(func(j int, a_doc *goquery.Selection) { version := a_doc.AttrOr("name", "") if version != "" { versions = append(versions, &version) - } }) lis := docs.Find("ul[style]") - for i := 0; i < len(versions); i++ { - sha256 := lis.Eq(i).Find("li").Eq(i).Find("code").Text() - maps[*versions[i]] = sha256 - fmt.Printf("version: %v , sha256: %v\n", *versions[i], sha256) + sha256 := lis.Eq(i).Find("li").Eq(0).Find("code").Text() + version := *versions[i] + maps[version] = sha256 + fmt.Printf("version: %v , sha256: %v\n", version, sha256) + grvms = append(grvms, &GradleItem{ + Version: version, + FileName: "gradle-" + version + "-bin.zip", + FileType: "zip", + Sha256: sha256, + Sha256Url: "https://downloads.gradle-dn.com/distributions/gradle-" + version + "-bin.zip.sha256", + DownloadUrl: "https://downloads.gradle-dn.com/distributions/gradle-" + version + "-bin.zip", + }) } - /* fmt.Printf("len(versions): %v\n", len(versions)) - s := lis.Eq(0).Find("li").Eq(0).Find("code") - fmt.Printf("lis: %v\n", s.Text()) - docs.Each(func(j int, doc *goquery.Selection) { - - }) */ + fmt.Printf("len(grvms): %v\n", len(grvms)) }) } @@ -79,7 +121,7 @@ func httpGetByProxy(url string) (*http.Response, error) { } func getClientProxy() *http.Client { - proxyUrl, err := url.Parse("http://127.0.0.1:8000") + proxyUrl, err := url.Parse("http://127.0.0.1:7890") if err != nil { panic(err) } diff --git a/internal/pkg/collector/openjdk_collector.go b/internal/pkg/collector/openjdk_collector.go deleted file mode 100644 index 5372ec3..0000000 --- a/internal/pkg/collector/openjdk_collector.go +++ /dev/null @@ -1,215 +0,0 @@ -package collector - -import ( - "fmt" - "net/http" - "runtime" - "strings" - - "github.com/PuerkitoBio/goquery" - "github.com/forget-the-bright/j/internal/pkg/config" -) - -func ConvertCollectorToUrlItem(colls []*Collector, isGetSha256 bool) []*config.UrlItem { - var rs = make([]*config.UrlItem, 0) - for _, coll := range colls { - var item *Op_Item - switch runtime.GOOS { - case "linux": - if runtime.GOARCH == "aarch64" { - item = coll.Linux_AArch64 - } else { - item = coll.Linux_X64 - } - case "windows": - item = coll.Windows_X64 - case "darwin": - if runtime.GOARCH == "aarch64" { - item = coll.Mac_AArch64 - } else { - item = coll.Mac_X64 - } - default: - item = nil - } - if item != nil { - rs = append(rs, &config.UrlItem{ - In: &config.JavaFileItem{ - FileName: item.FileName, - URL: item.Url, - Sha256: GetSha256ByUrl(item.Sha256Url, isGetSha256), - }, - SimpleName: coll.Version, - Expected: getFileNameNoSuffix(item.FileName), - }) - } - } - switch runtime.GOOS { - case "windows": - rs = append(rs, &config.UrlItem{ - In: &config.JavaFileItem{ - FileName: "openjdk-8u42-b03-windows-i586-14_jul_2022.zip", - URL: "https://download.java.net/openjdk/jdk8u42/ri/openjdk-8u42-b03-windows-i586-14_jul_2022.zip", - Sha256: "0314134bd981db63c7ca68d262ef896383b5694307a14bac81af88b5ad926279", - }, - Expected: "openjdk-8u42-b03-windows-i586-14_jul_2022", - SimpleName: "8", - }) - case "linux": - rs = append(rs, &config.UrlItem{ - In: &config.JavaFileItem{ - FileName: "openjdk-8u42-b03-linux-x64-14_jul_2022.tar.gz", - URL: "https://download.java.net/openjdk/jdk8u42/ri/openjdk-8u42-b03-linux-x64-14_jul_2022.tar.gz", - Sha256: "dd5fc6ef5ebffb88cd66af5258226c31f6c719fdcd855d95464fdb2cab051baa", - }, - Expected: "openjdk-8u42-b03-linux-x64-14_jul_2022", - SimpleName: "8", - }) - - } - return config.ReverseArray(rs) -} - -func GetOpenJDKVesionUrlInfo() []*Collector { - resp, _ := http.Get(Collector_Archive_Url) - Collectors = make([]*Collector, 0) - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - fmt.Println("false") - } - doc_selector, _ := goquery.NewDocumentFromReader(resp.Body) - divs := doc_selector.Find("#sidebar").Find(".links") - divs.Each(func(j int, div *goquery.Selection) { - link_about := div.Find(".about").Text() - if link_about == "Reference Implementations" { - a_docs := div.Find("a") - a_docs.Each(func(j int, a_doc *goquery.Selection) { - info_url := a_doc.AttrOr("href", "") - versions := strings.Split(a_doc.Text(), " ") - version := versions[len(versions)-1] - version_url := Collector_Url + strings.ReplaceAll(info_url, ".", "") - Collectors = append(Collectors, getVersionByUrl(version, version_url)) - //fmt.Println(version) - //fmt.Println(version_url) - }) - } - //fmt.Printf("link_about: %v\n", link_about) - }) - return Collectors -} - -func GetOpenJDKArchiveReleasesInfo() []*Collector { - Archive_Releases_Collectors = make([]*Collector, 0) - resp, _ := http.Get(Collector_Archive_Url) - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - fmt.Println("false") - } - docs, _ := goquery.NewDocumentFromReader(resp.Body) - tbody_docs := docs.Find(".builds").Find("tbody").Children() - var collector_Item *Collector - tbody_docs.Each(func(j int, tr *goquery.Selection) { - th := tr.Find("th") - if th.Length() == 1 { - val := th.Text() - if val != "Source" { - s2 := strings.Split(val, "(build ") - version_str := strings.Trim(strings.Trim(s2[0], " "), " GA") - //fmt.Println("\n" + version_str) - collector_Item = &Collector{ - Version: version_str, - } - Archive_Releases_Collectors = append(Archive_Releases_Collectors, collector_Item) - } - } - if th.Length() == 2 { - a_item := tr.Find("td").Find("a") - file_type := strings.Trim(a_item.First().Text(), "\n") - download_url := a_item.First().AttrOr("href", "") - file_name := getFileNameByDownLoadUrl(download_url) - sha256_url := a_item.Last().AttrOr("href", "") - releases := strings.Split(th.First().Text(), "/") - - switch releases[0] { - case "Windows": - collector_Item.Windows_X64 = build_Op_Item(file_type, "x64", download_url, sha256_url, file_name) - case "Mac": - if len(releases) == 1 || releases[1] == "x64" { - collector_Item.Mac_X64 = build_Op_Item(file_type, "x64", download_url, sha256_url, file_name) - } else { - collector_Item.Mac_AArch64 = build_Op_Item(file_type, releases[1], download_url, sha256_url, file_name) - } - case "Linux": - if len(releases) == 1 || releases[1] == "x64" { - collector_Item.Linux_X64 = build_Op_Item(file_type, "x64", download_url, sha256_url, file_name) - } else { - collector_Item.Linux_X64 = build_Op_Item(file_type, releases[1], download_url, sha256_url, file_name) - } - } - } - }) - return Archive_Releases_Collectors -} - -func getVersionByUrl(version, url string) *Collector { - var coll = Collector{ - Version: version, - } - resp, _ := http.Get(url) - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - fmt.Println("false") - } - doc_selector, _ := goquery.NewDocumentFromReader(resp.Body) - - divs := doc_selector.Find("#main").Find("ul") - - if divs.Length() <= 1 || version == "8" { - li_docs := divs.Find("li") - linux_x64_a := li_docs.Eq(0).Find("a") - windows_x64_a := li_docs.Eq(1).Find("a") - - linux_url := linux_x64_a.Eq(0).AttrOr("href", "") - linux_file_name := getFileNameByDownLoadUrl(linux_url) - linux_file_type := getFileTypeByFileName(linux_file_name) - linux_sha256_url := linux_x64_a.Eq(1).AttrOr("href", "") - - windows_url := windows_x64_a.Eq(0).AttrOr("href", "") - windows_file_name := getFileNameByDownLoadUrl(windows_url) - windows_file_type := getFileTypeByFileName(windows_file_name) - windows_sha256_url := windows_x64_a.Eq(1).AttrOr("href", "") - - coll.Linux_X64 = &Op_Item{ - Arch: "x64", - Url: linux_url, - Sha256Url: linux_sha256_url, - FileName: linux_file_name, - FileType: linux_file_type, - } - coll.Windows_X64 = &Op_Item{ - Arch: "x64", - Url: windows_url, - Sha256Url: windows_sha256_url, - FileName: windows_file_name, - FileType: windows_file_type, - } - } else { - divs_eq0 := divs.Eq(0) - li_docs := divs_eq0.Find("li") - linux_x64_a := li_docs.Eq(0).Find("a") - - linux_url := linux_x64_a.Eq(0).AttrOr("href", "") - linux_file_name := getFileNameByDownLoadUrl(linux_url) - linux_file_type := getFileTypeByFileName(linux_file_name) - linux_sha256_url := linux_x64_a.Eq(1).AttrOr("href", "") - coll.Linux_X64 = &Op_Item{ - Arch: "x64", - Url: linux_url, - Sha256Url: linux_sha256_url, - FileName: linux_file_name, - FileType: linux_file_type, - } - - } - return &coll -} diff --git a/internal/pkg/collector/openjdk_collector_test.go b/internal/pkg/collector/openjdk_collector_test.go deleted file mode 100644 index c6392a1..0000000 --- a/internal/pkg/collector/openjdk_collector_test.go +++ /dev/null @@ -1,188 +0,0 @@ -package collector - -import ( - "fmt" - "io/ioutil" - "net/http" - "strings" - "testing" - - "github.com/PuerkitoBio/goquery" -) - -func Test_GetSha256ByUrl(t *testing.T) { - resp, _ := http.Get("https://download.java.net/java/GA/jdk19.0.1/afdd2e245b014143b62ccb916125e3ce/10/GPL/openjdk-19.0.1_windows-x64_bin.zip.sha256") - defer resp.Body.Close() - bytes, _ := ioutil.ReadAll(resp.Body) - t.Run("", func(t *testing.T) { - fmt.Println(string(bytes)) - }) - -} - -func Test_getOpenJdkVersion(t *testing.T) { - resp, _ := http.Get(Collector_Archive_Url) - colls := make([]*Collector, 0) - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - fmt.Println("false") - } - doc_selector, _ := goquery.NewDocumentFromReader(resp.Body) - t.Run("", func(t *testing.T) { - divs := doc_selector.Find("#sidebar").Find(".links") - divs.Each(func(j int, div *goquery.Selection) { - link_about := div.Find(".about").Text() - if link_about == "Reference Implementations" { - a_docs := div.Find("a") - a_docs.Each(func(j int, a_doc *goquery.Selection) { - info_url := a_doc.AttrOr("href", "") - versions := strings.Split(a_doc.Text(), " ") - version := versions[len(versions)-1] - version_url := Collector_Url + strings.ReplaceAll(info_url, ".", "") - colls = append(colls, test_getVersionByUrl(version, version_url)) - //fmt.Println(version) - //fmt.Println(version_url) - }) - } - fmt.Printf("link_about: %v\n", link_about) - }) - temp := colls - fmt.Printf("len(temp): %v\n", len(temp)) - //fmt.Printf("divs.Length(): %v\n", divs.Length()) - }) -} - -func test_getVersionByUrl(version, url string) *Collector { - var coll = Collector{ - Version: version, - } - resp, _ := http.Get(url) - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - fmt.Println("false") - } - doc_selector, _ := goquery.NewDocumentFromReader(resp.Body) - - divs := doc_selector.Find("#main").Find("ul") - - if divs.Length() <= 1 || version == "8" { - li_docs := divs.Find("li") - linux_x64_a := li_docs.Eq(0).Find("a") - windows_x64_a := li_docs.Eq(1).Find("a") - - linux_url := linux_x64_a.Eq(0).AttrOr("href", "") - linux_file_name := getFileNameByDownLoadUrl(linux_url) - linux_file_type := getFileTypeByFileName(linux_file_name) - linux_sha256_url := linux_x64_a.Eq(1).AttrOr("href", "") - - windows_url := windows_x64_a.Eq(0).AttrOr("href", "") - windows_file_name := getFileNameByDownLoadUrl(windows_url) - windows_file_type := getFileTypeByFileName(windows_file_name) - windows_sha256_url := windows_x64_a.Eq(1).AttrOr("href", "") - - coll.Linux_X64 = &Op_Item{ - Arch: "x64", - Url: linux_url, - Sha256Url: linux_sha256_url, - FileName: linux_file_name, - FileType: linux_file_type, - } - coll.Windows_X64 = &Op_Item{ - Arch: "x64", - Url: windows_url, - Sha256Url: windows_sha256_url, - FileName: windows_file_name, - FileType: windows_file_type, - } - } else { - divs_eq0 := divs.Eq(0) - li_docs := divs_eq0.Find("li") - linux_x64_a := li_docs.Eq(0).Find("a") - - linux_url := linux_x64_a.Eq(0).AttrOr("href", "") - linux_file_name := getFileNameByDownLoadUrl(linux_url) - linux_file_type := getFileTypeByFileName(linux_file_name) - linux_sha256_url := linux_x64_a.Eq(1).AttrOr("href", "") - coll.Linux_X64 = &Op_Item{ - Arch: "x64", - Url: linux_url, - Sha256Url: linux_sha256_url, - FileName: linux_file_name, - FileType: linux_file_type, - } - - } - return &coll -} - -func Test_getArchiveVersion(t *testing.T) { - Archive_Releases_Collectors = make([]*Collector, 0) - resp, _ := http.Get(Collector_Archive_Url) - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - fmt.Println("false") - } - c, _ := goquery.NewDocumentFromReader(resp.Body) - t.Run("", func(t *testing.T) { - s := c.Find(".builds").Find("tbody").Children() - /* s.Each(func(j int, tr *goquery.Selection) { - th := tr.Find("th") - if th.Length() == 1 { - val := th.Text() - if val != "Source" { - s2 := strings.Split(val, "(build ") - fmt.Println(strings.Trim(strings.Trim(s2[0], " "), " GA")) - } - } - }) */ - var collector_Item *Collector - //flag := false - s.Each(func(j int, tr *goquery.Selection) { - th := tr.Find("th") - if th.Length() == 1 { - val := th.Text() - if val != "Source" { - s2 := strings.Split(val, "(build ") - version_str := strings.Trim(strings.Trim(s2[0], " "), " GA") - fmt.Println("\n" + version_str) - collector_Item = &Collector{ - Version: version_str, - } - Archive_Releases_Collectors = append(Archive_Releases_Collectors, collector_Item) - } - } - if th.Length() == 2 { - a_item := tr.Find("td").Find("a") - file_type := strings.Trim(a_item.First().Text(), "\n") - download_url := a_item.First().AttrOr("href", "") - file_name := getFileNameByDownLoadUrl(download_url) - sha256_url := a_item.Last().AttrOr("href", "") - releases := strings.Split(th.First().Text(), "/") - - switch releases[0] { - case "Windows": - collector_Item.Mac_X64 = build_Op_Item(file_type, "x64", download_url, sha256_url, file_name) - case "Mac": - if len(releases) == 1 || releases[1] == "x64" { - collector_Item.Mac_X64 = build_Op_Item(file_type, "x64", download_url, sha256_url, file_name) - } else { - collector_Item.Mac_AArch64 = build_Op_Item(file_type, "x64", download_url, sha256_url, file_name) - } - case "Linux": - if len(releases) == 1 || releases[1] == "x64" { - collector_Item.Linux_X64 = build_Op_Item(file_type, "x64", download_url, sha256_url, file_name) - } else { - collector_Item.Linux_X64 = build_Op_Item(file_type, "x64", download_url, sha256_url, file_name) - } - } - //fmt.Printf("%v %v\n", strings.Trim(a_item.First().Text(), "\n"), a_item.Last().Text()) - //fmt.Printf("%v %v\n", th.First().Text(), th.Last().Text()) - //fmt.Printf("%v\n%v\n", a_item.First().AttrOr("href", ""), a_item.Last().AttrOr("href", "")) - //fmt.Printf("th.Text(): %v th.len:%v\n", th.Text(), thLen) - } - }) - }) - c2 := Archive_Releases_Collectors - fmt.Printf("len(c2): %v\n", len(c2)) - fmt.Println(c2) -} diff --git a/internal/pkg/config/urls.go b/internal/pkg/config/urls.go index 666f185..bd790f3 100644 --- a/internal/pkg/config/urls.go +++ b/internal/pkg/config/urls.go @@ -20,7 +20,7 @@ type UrlItem struct { var Url_Items []*UrlItem -func ReverseArray(arr []*UrlItem) []*UrlItem { +func ReverseArray[T interface{}](arr []*T) []*T { for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 { arr[i], arr[j] = arr[j], arr[i] } diff --git a/internal/pkg/download/download.go b/internal/pkg/download/download.go index 08ad9cf..6e62709 100644 --- a/internal/pkg/download/download.go +++ b/internal/pkg/download/download.go @@ -9,14 +9,15 @@ import ( "os" "time" - "github.com/forget-the-bright/j/internal/pkg/errs" + "github.com/forget-the-bright/grvm/internal/pkg/errs" + "github.com/forget-the-bright/grvm/internal/pkg/proxy" "github.com/k0kubun/go-ansi" "github.com/schollz/progressbar/v3" ) // Download 下载资源并另存为 func Download(srcURL string, filename string, flag int, perm fs.FileMode, withProgress bool) (size int64, err error) { - resp, err := http.Get(srcURL) + resp, err := proxy.HttpGetByProxy(srcURL) if err != nil { return 0, errs.NewDownloadError(srcURL, err) } diff --git a/internal/pkg/proxy/proxy.go b/internal/pkg/proxy/proxy.go new file mode 100644 index 0000000..b196daa --- /dev/null +++ b/internal/pkg/proxy/proxy.go @@ -0,0 +1,49 @@ +package proxy + +import ( + "net/http" + "net/url" + "os" + "strings" +) + +func HttpGetByProxy(url string) (*http.Response, error) { + if cli := getClientProxy(); cli != nil { + request, _ := http.NewRequest("GET", url, nil) + return cli.Do(request) + } else { + return http.Get(url) + } + +} + +func getClientProxy() *http.Client { + if proxy := getEnvProxy(); proxy == "" { + return nil + } else { + proxyUrl, err := url.Parse(proxy) + if err != nil { + panic(err) + } + return &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}} + } +} + +func getEnvProxy() string { + if proxy_url := getEnvAny("HTTP_PROXY", "http_proxy"); proxy_url != "" { + if !strings.Contains(proxy_url, "http://") { + return "http://" + proxy_url + } + return proxy_url + } + return "" +} + +func getEnvAny(names ...string) string { + for _, n := range names { + if val := os.Getenv(n); val != "" { + return val + } + } + return "" +} diff --git a/main.go b/main.go index 817cb7c..6b70f9e 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,6 @@ package main -import "github.com/forget-the-bright/j/cli" +import "github.com/forget-the-bright/grvm/cli" func main() { cli.Run() diff --git a/makefile b/makefile index 9123296..5d65adf 100644 --- a/makefile +++ b/makefile @@ -2,7 +2,7 @@ GO = CGO_ENABLED=0 GO111MODULE=on GOPROXY=https://goproxy.cn,direct go BUILD_DATE := $(shell date '+%Y-%m-%d %H:%M:%S') GIT_BRANCH := $(shell git symbolic-ref --short -q HEAD) GIT_COMMIT_HASH := $(shell git rev-parse HEAD|cut -c 1-8) -GO_FLAGS := -v -ldflags="-X 'github.com/forget-the-bright/j/internal/build.Build=$(BUILD_DATE)' -X 'github.com/forget-the-bright/j/internal/build.Commit=$(GIT_COMMIT_HASH)' -X 'github.com/forget-the-bright/j/internal/build.Branch=$(GIT_BRANCH)'" +GO_FLAGS := -v -ldflags="-X 'github.com/forget-the-bright/grvm/internal/build.Build=$(BUILD_DATE)' -X 'github.com/forget-the-bright/grvm/internal/build.Commit=$(GIT_COMMIT_HASH)' -X 'github.com/forget-the-bright/grvm/internal/build.Branch=$(GIT_BRANCH)'" all: install test clean