From 6374adc1eae5b93034a3ecccbeeed8162eb5359b Mon Sep 17 00:00:00 2001 From: rick <1450685+LinuxSuRen@users.noreply.github.com> Date: Sat, 9 Apr 2022 21:36:50 +0800 Subject: [PATCH] Support version as an URL --- README-zh.md | 11 ++- README.md | 8 +-- cmd/util_test.go | 2 +- pkg/installer.go | 156 ----------------------------------------- pkg/installer/check.go | 15 ++++ pkg/installer/types.go | 1 + pkg/net.go | 15 ---- pkg/os/apt/init.go | 1 - pkg/os/apt/kubectl.go | 102 --------------------------- pkg/os/yum/init.go | 1 - pkg/os/yum/kubectl.go | 65 ----------------- pkg/retry_client.go | 19 ----- 12 files changed, 26 insertions(+), 370 deletions(-) delete mode 100644 pkg/installer.go delete mode 100644 pkg/net.go delete mode 100644 pkg/os/apt/kubectl.go delete mode 100644 pkg/os/yum/kubectl.go delete mode 100644 pkg/retry_client.go diff --git a/README-zh.md b/README-zh.md index 2809ef5..f885b2b 100644 --- a/README-zh.md +++ b/README-zh.md @@ -11,7 +11,7 @@ 通过命令:`brew install linuxsuren/linuxsuren/hd` 来安装 或者,对于 Linux 用户可以直接通过命令下载: -``` +```shell curl -L https://github.com/linuxsuren/http-downloader/releases/latest/download/hd-linux-amd64.tar.gz | tar xzv mv hd /usr/local/bin ``` @@ -20,26 +20,25 @@ mv hd /usr/local/bin # 用法 -``` +```shell hd get https://github.com/jenkins-zh/jenkins-cli/releases/latest/download/jcli-linux-amd64.tar.gz --thread 6 ``` 或者,用一个更加简便的办法: -``` +```shell hd get jenkins-zh/jenkins-cli/jcli -t 6 ``` 获取,你也可以安装一个来自 GitHub 的软件包: -``` +```shell hd install jenkins-zh/jenkins-cli/jcli -t 6 ``` -``` 或者,你也可以从 GitHub 上下载预发布的二进制包: -``` +```shell hd get --pre ks ``` diff --git a/README.md b/README.md index e0bb3f7..3900030 100644 --- a/README.md +++ b/README.md @@ -23,19 +23,19 @@ Want to go through the code? [GitPod](https://gitpod.io/#https://github.com/linu # Usage ## Download -``` +```shell hd get https://github.com/jenkins-zh/jenkins-cli/releases/latest/download/jcli-linux-amd64.tar.gz --thread 6 ``` Or use a simple way instead of typing the whole URL: -``` +```shell hd get jenkins-zh/jenkins-cli/jcli -t 6 ``` Or you might want to download a pre-released binary package from GitHub: -``` +```shell hd get --pre ks ``` @@ -76,7 +76,7 @@ CMD ["ks"] You can import it from `github.com/linuxsuren/http-downloader/pkg/installer`, then put the following code to your CLI. It can help you to download desired tools: -``` +```go is := installer.Installer{ Provider: "github", } diff --git a/cmd/util_test.go b/cmd/util_test.go index fd3f98b..6bac4e6 100644 --- a/cmd/util_test.go +++ b/cmd/util_test.go @@ -87,4 +87,4 @@ func TestArrayCompletion(t *testing.T) { array, direct := function(nil, nil, "") assert.Equal(t, []string{"a", "b"}, array) assert.Equal(t, cobra.ShellCompDirectiveNoFileComp, direct) -} \ No newline at end of file +} diff --git a/pkg/installer.go b/pkg/installer.go deleted file mode 100644 index 1034c5e..0000000 --- a/pkg/installer.go +++ /dev/null @@ -1,156 +0,0 @@ -package pkg - -// -//import ( -// "archive/tar" -// "compress/gzip" -// "fmt" -// "github.com/spf13/cobra" -// "io" -// "os" -// "os/exec" -// "path/filepath" -// "strings" -// "syscall" -//) -// -//type Installer struct { -//} -// -//func (o *Installer) Download(cmd *cobra.Command, args []string) (err error) { -// if o.Download { -// if err = o.downloadOption.runE(cmd, args); err != nil { -// return -// } -// } -// -// targetBinary := o.name -// if o.Package != nil && o.Package.TargetBinary != "" { -// // this is the desired binary file -// targetBinary = o.Package.TargetBinary -// } -// -// var source string -// var target string -// tarFile := o.Output -// if o.Tar { -// if err = o.extractFiles(tarFile, o.name); err == nil { -// source = fmt.Sprintf("%s/%s", filepath.Dir(tarFile), o.name) -// target = fmt.Sprintf("/usr/local/bin/%s", targetBinary) -// } else { -// err = fmt.Errorf("cannot extract %s from tar file, error: %v", tarFile, err) -// } -// } else { -// source = o.downloadOption.Output -// target = fmt.Sprintf("/usr/local/bin/%s", targetBinary) -// } -// -// if err == nil { -// if o.Package != nil && o.Package.PreInstalls != nil { -// if err = execCommand(o.Package.PreInstalls.Cmd, o.Package.PreInstalls.Args...); err != nil { -// return -// } -// } -// -// if o.Package != nil && o.Package.Installation != nil { -// err = execCommand(o.Package.Installation.Cmd, o.Package.Installation.Args...) -// } else { -// err = o.overWriteBinary(source, target) -// } -// -// if err == nil && o.Package != nil && o.Package.PostInstalls != nil { -// err = execCommand(o.Package.PostInstalls.Cmd, o.Package.PostInstalls.Args...) -// } -// -// if err == nil && o.Package != nil && o.Package.TestInstalls != nil { -// err = execCommand(o.Package.TestInstalls.Cmd, o.Package.TestInstalls.Args...) -// } -// -// if err == nil && o.CleanPackage { -// if cleanErr := os.RemoveAll(tarFile); cleanErr != nil { -// cmd.Println("cannot remove file", tarFile, ", error:", cleanErr) -// } -// } -// } -// return -//} -// -//func (o *Installer) overWriteBinary(sourceFile, targetPath string) (err error) { -// fmt.Println("install", sourceFile, "to", targetPath) -// switch runtime.GOOS { -// case "linux", "darwin": -// if err = execCommand("chmod", "u+x", sourceFile); err != nil { -// return -// } -// -// if err = execCommand("rm", "-rf", targetPath); err != nil { -// return -// } -// -// var cp string -// if cp, err = exec.LookPath("mv"); err == nil { -// err = syscall.Exec(cp, []string{"mv", sourceFile, targetPath}, os.Environ()) -// } -// default: -// sourceF, _ := os.Open(sourceFile) -// targetF, _ := os.OpenFile(targetPath, os.O_CREATE|os.O_RDWR, 0600) -// if _, err = io.Copy(targetF, sourceF); err != nil { -// err = fmt.Errorf("cannot copy %s from %s to %v, error: %v", o.name, sourceFile, targetPath, err) -// } -// -// if err == nil { -// _ = os.RemoveAll(sourceFile) -// } -// } -// return -//} -// -//func (o *Installer) extractFiles(tarFile, targetName string) (err error) { -// var f *os.File -// var gzf *gzip.Reader -// if f, err = os.Open(tarFile); err != nil { -// return -// } -// defer func() { -// _ = f.Close() -// }() -// -// if gzf, err = gzip.NewReader(f); err != nil { -// return -// } -// -// tarReader := tar.NewReader(gzf) -// var header *tar.Header -// var found bool -// for { -// if header, err = tarReader.Next(); err == io.EOF { -// err = nil -// break -// } else if err != nil { -// break -// } -// name := header.Name -// -// switch header.Typeflag { -// case tar.TypeReg: -// if name != targetName && !strings.HasSuffix(name, "/"+targetName) { -// continue -// } -// var targetFile *os.File -// if targetFile, err = os.OpenFile(fmt.Sprintf("%s/%s", filepath.Dir(tarFile), targetName), -// os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode)); err != nil { -// break -// } -// if _, err = io.Copy(targetFile, tarReader); err != nil { -// break -// } -// found = true -// _ = targetFile.Close() -// } -// } -// -// if err == nil && !found { -// err = fmt.Errorf("cannot found item '%s' from '%s'", targetName, tarFile) -// } -// return -//} diff --git a/pkg/installer/check.go b/pkg/installer/check.go index 0f57e1e..2976824 100644 --- a/pkg/installer/check.go +++ b/pkg/installer/check.go @@ -230,6 +230,21 @@ func (o *Installer) ProviderURLParse(path string, acceptPreRelease bool) (packag } } + // parse the version if it's an URL + if cfg.Version != "" && (strings.HasPrefix(cfg.Version, "http://") || strings.HasPrefix(cfg.Version, "https://")) { + var resp *http.Response + if resp, err = http.Get(cfg.Version); err != nil || resp.StatusCode != http.StatusOK { + err = fmt.Errorf("cannot get version from '%s', error is '%v', status code is '%d'", cfg.Version, err, resp.StatusCode) + return + } + var data []byte + if data, err = ioutil.ReadAll(resp.Body); err != nil { + err = fmt.Errorf("failed to get version from '%s', error is '%v'", cfg.Version, err) + return + } + hdPkg.Version = string(data) + } + if cfg.URL != "" { // it does not come from GitHub release tmp, _ := template.New("hd").Parse(cfg.URL) diff --git a/pkg/installer/types.go b/pkg/installer/types.go index ee5b2eb..e175499 100644 --- a/pkg/installer/types.go +++ b/pkg/installer/types.go @@ -27,6 +27,7 @@ type HDConfig struct { PreInstalls []CmdWithArgs `yaml:"preInstalls"` PostInstalls []CmdWithArgs `yaml:"postInstalls"` TestInstalls []CmdWithArgs `yaml:"testInstalls"` + Version string `yaml:"version"` Org, Repo string } diff --git a/pkg/net.go b/pkg/net.go deleted file mode 100644 index 3f6366d..0000000 --- a/pkg/net.go +++ /dev/null @@ -1,15 +0,0 @@ -package pkg - -/** - * This file was deprecated, please use the following package instead - * github.com/linuxsuren/http-downloader/pkg/net - */ - -import ( - "github.com/linuxsuren/http-downloader/pkg/net" -) - -// GetExternalIP returns an external ip of current machine -func GetExternalIP() (string, error) { - return net.GetExternalIP() -} diff --git a/pkg/os/apt/init.go b/pkg/os/apt/init.go index 2a70788..5cdf3ba 100644 --- a/pkg/os/apt/init.go +++ b/pkg/os/apt/init.go @@ -10,7 +10,6 @@ func SetInstallerRegistry(registry core.InstallerRegistry) { registry.Registry("vim", &vimInstallerInUbuntu{}) registry.Registry("golang", &golangInstallerInUbuntu{}) registry.Registry("git", &gitInstallerInUbuntu{}) - registry.Registry("kubectl", &kubectlInstallerInUbuntu{}) registry.Registry("bash-completion", &bashCompletionInstallerInUbuntu{}) registry.Registry("asciinema", &asciinemaInstallerInUbuntu{}) registry.Registry("ffmpge", &ffmpegInstallerInUbuntu{}) diff --git a/pkg/os/apt/kubectl.go b/pkg/os/apt/kubectl.go deleted file mode 100644 index e433afd..0000000 --- a/pkg/os/apt/kubectl.go +++ /dev/null @@ -1,102 +0,0 @@ -package apt - -import ( - // Enable go embed - _ "embed" - "fmt" - "github.com/linuxsuren/http-downloader/pkg/exec" - "io" - "io/ioutil" - "net/http" - "runtime" -) - -//go:embed resource/kubernetes.list -var kuberneteList string - -// kubectlInstallerInUbuntu is the installer of kubectl in CentOS -type kubectlInstallerInUbuntu struct { - count int -} - -// Available check if support current platform -func (d *kubectlInstallerInUbuntu) Available() (ok bool) { - if runtime.GOOS == "linux" { - _, err := exec.LookPath("apt-get") - ok = err == nil - } - return -} - -func fetchKeyring() (err error) { - var response *http.Response - if response, err = http.Get("https://packages.cloud.google.com/apt/doc/apt-key.gpg"); err != nil { - err = fmt.Errorf("failed to download kubernetes apt-key.gpg, error: %v", err) - return - } - - var data []byte - if data, err = io.ReadAll(response.Body); err != nil { - err = fmt.Errorf("failed to read response body from apt-key.gpg, error: %v", err) - return - } - - keyring := "/usr/share/keyrings/kubernetes-archive-keyring.gpg" - if err = ioutil.WriteFile(keyring, data, 0644); err != nil { - err = fmt.Errorf("failed to save %s, error: %v", keyring, err) - } - return -} - -// Install installs the kubectl -func (d *kubectlInstallerInUbuntu) Install() (err error) { - if err = fetchKeyring(); err != nil { - err = fmt.Errorf("failed to save kubernetes-archive-kerying.gpg") - return - } - - repo := "/etc/apt/sources.list.d/kubernetes.list" - if err = ioutil.WriteFile(repo, []byte(kuberneteList), 0644); err != nil { - err = fmt.Errorf("failed to save %s, error: %v", repo, err) - return - } - - if err = exec.RunCommand("apt-get", "update", "-y"); err != nil { - return - } - if err = exec.RunCommand("apt-get", "install", "-y", "apt-transport-https", - "ca-certificates"); err != nil { - return - } - if err = exec.RunCommand("apt-get", "update", "-y"); err != nil { - return - } - if err = exec.RunCommand("apt-get", "install", "-y", "kubectl"); err != nil { - return - } - return -} - -// Uninstall uninstalls the kubectl -func (d *kubectlInstallerInUbuntu) Uninstall() (err error) { - err = exec.RunCommand("apt-get", "remove", "-y", "kubectl") - return -} - -// WaitForStart waits for the service be started -func (d *kubectlInstallerInUbuntu) WaitForStart() (ok bool, err error) { - ok = true - return -} - -// Start starts the kubectl service -func (d *kubectlInstallerInUbuntu) Start() error { - fmt.Println("not supported yet") - return nil -} - -// Stop stops the kubectl service -func (d *kubectlInstallerInUbuntu) Stop() error { - fmt.Println("not supported yet") - return nil -} diff --git a/pkg/os/yum/init.go b/pkg/os/yum/init.go index 628896a..2eec17f 100644 --- a/pkg/os/yum/init.go +++ b/pkg/os/yum/init.go @@ -10,6 +10,5 @@ func SetInstallerRegistry(registry core.InstallerRegistry) { registry.Registry("vim", &vimInstallerInCentOS{}) registry.Registry("golang", &golangInstallerInCentOS{}) registry.Registry("git", &gitInstallerInCentOS{}) - registry.Registry("kubectl", &kubectlInstallerInCentOS{}) registry.Registry("bash-completion", &bashCompletionInstallerInCentOS{}) } diff --git a/pkg/os/yum/kubectl.go b/pkg/os/yum/kubectl.go deleted file mode 100644 index a857d2e..0000000 --- a/pkg/os/yum/kubectl.go +++ /dev/null @@ -1,65 +0,0 @@ -package yum - -import ( - // Enable go embed - _ "embed" - "fmt" - "github.com/linuxsuren/http-downloader/pkg/exec" - "io/ioutil" - "runtime" -) - -//go:embed resource/kubernetes.repo -var kubernetesRepo string - -// kubectlInstallerInCentOS is the installer of kubectl in CentOS -type kubectlInstallerInCentOS struct { - count int -} - -// Available check if support current platform -func (d *kubectlInstallerInCentOS) Available() (ok bool) { - if runtime.GOOS == "linux" { - _, err := exec.LookPath("yum") - ok = err == nil - } - return -} - -// Install installs the kubectl -func (d *kubectlInstallerInCentOS) Install() (err error) { - repo := "/etc/yum.repos.d/kubernetes.repo" - if err = ioutil.WriteFile(repo, []byte(kubernetesRepo), 0644); err != nil { - err = fmt.Errorf("failed to save %s, error: %v", repo, err) - return - } - - if err = exec.RunCommand("yum", "install", "-y", "kubectl"); err != nil { - return - } - return -} - -// Uninstall uninstalls the kubectl -func (d *kubectlInstallerInCentOS) Uninstall() (err error) { - err = exec.RunCommand("yum", "remove", "-y", "kubectl") - return -} - -// WaitForStart waits for the service be started -func (d *kubectlInstallerInCentOS) WaitForStart() (ok bool, err error) { - ok = true - return -} - -// Start starts the kubectl service -func (d *kubectlInstallerInCentOS) Start() error { - fmt.Println("not supported yet") - return nil -} - -// Stop stops the kubectl service -func (d *kubectlInstallerInCentOS) Stop() error { - fmt.Println("not supported yet") - return nil -} diff --git a/pkg/retry_client.go b/pkg/retry_client.go deleted file mode 100644 index 9dde7c1..0000000 --- a/pkg/retry_client.go +++ /dev/null @@ -1,19 +0,0 @@ -package pkg - -/** - * This file was deprecated, please use the following package instead - * github.com/linuxsuren/http-downloader/pkg/net - */ - -import ( - "github.com/linuxsuren/http-downloader/pkg/net" - "net/http" -) - -// RetryClient is the wrap of http.Client -type RetryClient net.RetryClient - -// Do is the wrap of http.Client.Do -func (c *RetryClient) Do(req *http.Request) (rsp *http.Response, err error) { - return (*net.RetryClient)(c).Do(req) -}