添加下载功能

main
15128022404 2 years ago
parent bccfdba267
commit 241d0f6214

@ -0,0 +1,16 @@
{
// 使 IntelliSense
//
// 访: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"args": [ "18" ]
}
]
}

@ -25,12 +25,15 @@ var (
) )
func init() { func init() {
/* ghomeDir, _ = os.Getwd() ghomeDir, _ = os.Getwd()
goroot = filepath.Join(ghomeDir, "go") fmt.Println(ghomeDir)
goroot = filepath.Join(ghomeDir, "java")
fmt.Println(goroot)
downloadsDir = filepath.Join(ghomeDir, "downloads") downloadsDir = filepath.Join(ghomeDir, "downloads")
os.MkdirAll(downloadsDir, 0755) os.MkdirAll(downloadsDir, 0755)
versionsDir = filepath.Join(ghomeDir, "versions") versionsDir = filepath.Join(ghomeDir, "versions")
os.MkdirAll(versionsDir, 0755) */ os.MkdirAll(versionsDir, 0755)
cli.AppHelpTemplate = fmt.Sprintf(`NAME: cli.AppHelpTemplate = fmt.Sprintf(`NAME:
{{.Name}}{{if .Usage}} - {{.Usage}}{{end}} {{.Name}}{{if .Usage}} - {{.Usage}}{{end}}

@ -0,0 +1,57 @@
package cli
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/forget-the-bright/j/internal/pkg/config"
"github.com/forget-the-bright/j/internal/pkg/download"
"github.com/mholt/archiver/v3"
"github.com/urfave/cli/v2"
)
func fundVersion(version string) *config.UrlItem {
for _, v := range config.Url_Items {
if strings.Contains(v.Expected, version) {
return v
}
}
return nil
}
func Install(version string) (err error) {
ui := fundVersion(version)
if ui == nil {
return nil
}
filename := filepath.Join(downloadsDir, ui.In.FileName)
DownloadWithProgress(ui.In.URL, filename)
targetV := filepath.Join(versionsDir, ui.SimpleName)
// 解压安装包
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)
return nil
}
// DownloadWithProgress 下载版本另存为指定文件且显示下载进度
func DownloadWithProgress(url, dst string) (size int64, err error) {
return download.Download(url, dst, os.O_CREATE|os.O_WRONLY, 0644, true)
}

@ -6,14 +6,30 @@ require (
github.com/Masterminds/semver v1.5.0 github.com/Masterminds/semver v1.5.0
github.com/fatih/color v1.15.0 github.com/fatih/color v1.15.0
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213 github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213
github.com/mholt/archiver/v3 v3.5.1
github.com/schollz/progressbar/v3 v3.13.1
github.com/urfave/cli/v2 v2.25.0 github.com/urfave/cli/v2 v2.25.0
) )
require ( require (
github.com/andybalholm/brotli v1.0.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect
github.com/golang/snappy v0.0.2 // indirect
github.com/klauspost/compress v1.11.4 // indirect
github.com/klauspost/pgzip v1.2.5 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/nwaples/rardecode v1.1.0 // indirect
github.com/pierrec/lz4/v4 v4.1.2 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/stretchr/testify v1.8.0 // indirect
github.com/ulikunitz/xz v0.5.9 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/sys v0.6.0 // indirect golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
) )

@ -1,22 +1,74 @@
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww= github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y= github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/andybalholm/brotli v1.0.1 h1:KqhlKozYbRtJvsPrrEeXcO+N2l6NYT5A2QAFmSULpEc=
github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 h1:iFaUwBSo5Svw6L7HYpRu/0lE3e0BaElwnNO1qkNQxBY=
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5/go.mod h1:qssHWj60/X5sZFNxpG4HBPDHVqxNm4DfnCKgrbZOT+s=
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/golang/snappy v0.0.2 h1:aeE13tS0IiQgFjYdoL8qN3K1N2bXXtI6Vi51/y7BpMw=
github.com/golang/snappy v0.0.2/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213 h1:qGQQKEcAR99REcMpsXCp3lJ03zYT1PkRd3kQGPn9GVg= github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213 h1:qGQQKEcAR99REcMpsXCp3lJ03zYT1PkRd3kQGPn9GVg=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw= github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/compress v1.11.4 h1:kz40R/YWls3iqT9zX9AHN3WoVsrAWVyui5sxuLqiXqU=
github.com/klauspost/compress v1.11.4/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/pgzip v1.2.5 h1:qnWYvvKqedOF2ulHpMG72XQol4ILEJ8k2wwRl/Km8oE=
github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mholt/archiver/v3 v3.5.1 h1:rDjOBX9JSF5BvoJGvjqK479aL70qh9DIpZCl+k7Clwo=
github.com/mholt/archiver/v3 v3.5.1/go.mod h1:e3dqJ7H78uzsRSEACH1joayhuSyhnonssnDhppzS1L4=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/nwaples/rardecode v1.1.0 h1:vSxaY8vQhOcVr4mm5e8XllHWTiM4JF507A0Katqw7MQ=
github.com/nwaples/rardecode v1.1.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0=
github.com/pierrec/lz4/v4 v4.1.2 h1:qvY3YFXRQE/XB8MlLzJH7mSzBs74eA2gg52YTk6jUPM=
github.com/pierrec/lz4/v4 v4.1.2/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/schollz/progressbar/v3 v3.13.1 h1:o8rySDYiQ59Mwzy2FELeHY5ZARXZTVJC7iHD6PEFUiE=
github.com/schollz/progressbar/v3 v3.13.1/go.mod h1:xvrbki8kfT1fzWzBT/UZd9L6GA+jdL7HAgq2RFnO6fQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/ulikunitz/xz v0.5.9 h1:RsKRIA2MO8x56wkkcd3LbtcE/uMszhb6DpRf+3uwa3I=
github.com/ulikunitz/xz v0.5.9/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/urfave/cli/v2 v2.25.0 h1:ykdZKuQey2zq0yin/l7JOm9Mh+pg72ngYMeB0ABn6q8= github.com/urfave/cli/v2 v2.25.0 h1:ykdZKuQey2zq0yin/l7JOm9Mh+pg72ngYMeB0ABn6q8=
github.com/urfave/cli/v2 v2.25.0/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc= github.com/urfave/cli/v2 v2.25.0/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo=
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

@ -0,0 +1,102 @@
package config
type JavaFileItem struct {
FileName string
URL string
}
type UrlItem struct {
In *JavaFileItem
Expected string
SimpleName string
}
var Url_Items = []*UrlItem{
{
In: &JavaFileItem{
FileName: "openjdk-19+36_windows-x64_bin.zip",
URL: "https://download.java.net/openjdk/jdk19/ri/openjdk-19+36_windows-x64_bin.zip",
},
Expected: "jdk-19",
SimpleName: "19",
},
{
In: &JavaFileItem{
FileName: "openjdk-18+36_windows-x64_bin.zip",
URL: "https://download.java.net/openjdk/jdk18/ri/openjdk-18+36_windows-x64_bin.zip",
},
Expected: "jdk-18",
SimpleName: "18",
},
{
In: &JavaFileItem{
FileName: "openjdk-17+35_windows-x64_bin.zip",
URL: "https://download.java.net/openjdk/jdk17/ri/openjdk-17+35_windows-x64_bin.zip",
},
Expected: "jdk-17",
SimpleName: "17",
},
{
In: &JavaFileItem{
FileName: "openjdk-16+36_windows-x64_bin.zip",
URL: "https://download.java.net/openjdk/jdk16/ri/openjdk-16+36_windows-x64_bin.zip",
},
Expected: "jdk-16",
SimpleName: "16",
},
{
In: &JavaFileItem{
FileName: "openjdk-15+36_windows-x64_bin.zip",
URL: "https://download.java.net/openjdk/jdk15/ri/openjdk-15+36_windows-x64_bin.zip",
},
Expected: "jdk-15",
SimpleName: "15",
},
{
In: &JavaFileItem{
FileName: "openjdk-14+36_windows-x64_bin.zip",
URL: "https://download.java.net/openjdk/jdk14/ri/openjdk-14+36_windows-x64_bin.zip",
},
Expected: "jdk-14",
SimpleName: "14",
},
{
In: &JavaFileItem{
FileName: "openjdk-13+33_windows-x64_bin.zip",
URL: "https://download.java.net/openjdk/jdk13/ri/openjdk-13+33_windows-x64_bin.zip",
},
Expected: "jdk-13",
SimpleName: "13",
},
{
In: &JavaFileItem{
FileName: "openjdk-12+32_windows-x64_bin.zip",
URL: "https://download.java.net/openjdk/jdk12/ri/openjdk-12+32_windows-x64_bin.zip",
},
Expected: "jdk-12",
SimpleName: "12",
},
{
In: &JavaFileItem{
FileName: "openjdk-11+28_windows-x64_bin.zip",
URL: "https://download.java.net/openjdk/jdk11/ri/openjdk-11+28_windows-x64_bin.zip",
},
Expected: "jdk-11",
SimpleName: "11",
},
{
In: &JavaFileItem{
FileName: "jdk-9+181_windows-x64_ri.zip",
URL: "https://download.java.net/openjdk/jdk9/ri/jdk-9+181_windows-x64_ri.zip",
},
Expected: "jdk-9",
SimpleName: "9",
},
{
In: &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",
},
Expected: "jdk-8",
SimpleName: "8",
},
}

@ -0,0 +1,72 @@
package download
import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"net/http"
"os"
"time"
"github.com/forget-the-bright/j/internal/pkg/errs"
"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)
if err != nil {
return 0, errs.NewDownloadError(srcURL, err)
}
defer resp.Body.Close()
f, err := os.OpenFile(filename, flag, perm)
if err != nil {
return 0, errs.NewDownloadError(srcURL, err)
}
defer f.Close()
var dst io.Writer
if withProgress {
bar := progressbar.NewOptions64(
resp.ContentLength,
progressbar.OptionEnableColorCodes(true),
progressbar.OptionSetTheme(progressbar.Theme{
Saucer: "=",
SaucerHead: ">",
SaucerPadding: " ",
BarStart: "[",
BarEnd: "]",
}),
progressbar.OptionSetWidth(15),
progressbar.OptionSetDescription("Downloading"),
progressbar.OptionSetWriter(ansi.NewAnsiStdout()),
progressbar.OptionShowBytes(true),
progressbar.OptionThrottle(65*time.Millisecond),
progressbar.OptionShowCount(),
progressbar.OptionOnCompletion(func() {
_, _ = fmt.Fprint(ansi.NewAnsiStdout(), "\n")
}),
// progressbar.OptionSpinnerType(35),
// progressbar.OptionFullWidth(),
)
_ = bar.RenderBlank()
dst = io.MultiWriter(f, bar)
} else {
dst = f
}
return io.Copy(dst, resp.Body)
}
// DownloadAsBytes 返回下载资源的原始字节切片
func DownloadAsBytes(srcURL string) (data []byte, err error) {
resp, err := http.Get(srcURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}

@ -0,0 +1,88 @@
package errs
import (
"errors"
"fmt"
"strings"
)
var (
// ErrVersionNotFound 版本不存在
ErrVersionNotFound = errors.New("version not found")
// ErrPackageNotFound 版本包不存在
ErrPackageNotFound = errors.New("installation package not found")
)
var (
// ErrUnsupportedChecksumAlgorithm 不支持的校验和算法
ErrUnsupportedChecksumAlgorithm = errors.New("unsupported checksum algorithm")
// ErrChecksumNotMatched 校验和不匹配
ErrChecksumNotMatched = errors.New("file checksum does not match the computed checksum")
// ErrChecksumFileNotFound 校验和文件不存在
ErrChecksumFileNotFound = errors.New("checksum file not found")
)
// URLUnreachableError URL不可达错误
type URLUnreachableError struct {
err error
url string
}
// NewURLUnreachableError 返回URL不可达错误实例
func NewURLUnreachableError(url string, err error) error {
return &URLUnreachableError{
err: err,
url: url,
}
}
func (e URLUnreachableError) Error() string {
var buf strings.Builder
buf.WriteString(fmt.Sprintf("URL %q is unreachable", e.url))
if e.err != nil {
buf.WriteString(" ==> " + e.err.Error())
}
return buf.String()
}
func (e URLUnreachableError) Err() error {
return e.err
}
func (e URLUnreachableError) URL() string {
return e.url
}
// DownloadError 下载失败错误
type DownloadError struct {
url string
err error
}
// NewDownloadError 返回下载失败错误实例
func NewDownloadError(url string, err error) error {
return &DownloadError{
url: url,
err: err,
}
}
// Error 返回错误字符串
func (e DownloadError) Error() string {
var buf strings.Builder
buf.WriteString(fmt.Sprintf("Resource(%s) download failed", e.url))
if e.err != nil {
buf.WriteString(" ==> " + e.err.Error())
}
return buf.String()
}
// Err 返回错误对象
func (e DownloadError) Err() error {
return e.err
}
// URL 返回资源URL
func (e DownloadError) URL() string {
return e.url
}

@ -1,6 +1,10 @@
package main package main
import "github.com/forget-the-bright/j/cli" import (
"os"
"github.com/forget-the-bright/j/cli"
)
func main() { func main() {
/* dir, _ := os.Getwd() /* dir, _ := os.Getwd()
@ -10,5 +14,9 @@ func main() {
fmt.Println(os.Args[1]) fmt.Println(os.Args[1])
cli.Use(os.Args[1]) //"1.17.9" cli.Use(os.Args[1]) //"1.17.9"
} */ } */
cli.Run() //cli.Run()
if len(os.Args) > 1 {
cli.Install(os.Args[1])
}
} }

Loading…
Cancel
Save