Skip to content

Commit

Permalink
Add multi-thread self-upgrade support (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen authored Jan 22, 2021
1 parent b859b9f commit 395067c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ go 1.15
require (
github.com/golang/mock v1.4.4
github.com/google/go-github/v29 v29.0.3
github.com/jenkins-zh/jenkins-cli v0.0.32
github.com/linuxsuren/http-downloader v0.0.2-0.20201207132639-19888a6beaec // indirect
github.com/linuxsuren/http-downloader v0.0.6
github.com/onsi/ginkgo v1.14.2
github.com/onsi/gomega v1.10.3
github.com/onsi/gomega v1.10.4
github.com/spf13/cobra v1.1.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.6.1
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,11 @@ github.com/kr/pty v1.1.8 h1:AkaSdXYQOWeaO3neb8EM634ahkXXe3jYbVh/F9lq+GI=
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/linuxsuren/cobra-extension v0.0.6/go.mod h1:qcEJv7BbL0UpK6MbrTESP/nKf1+z1wQdMAnE1NBl3QQ=
github.com/linuxsuren/http-downloader v0.0.2-0.20201207132639-19888a6beaec h1:N6xw+W4IvgOV0cmhE7iUWKHNZqMUXlT3in+1ORmqIGo=
github.com/linuxsuren/http-downloader v0.0.2-0.20201207132639-19888a6beaec/go.mod h1:zRZY9FCDBuYNDxbI2Ny5suasZsMk7J6q9ecQ3V3PIqI=
github.com/linuxsuren/http-downloader v0.0.6 h1:JuCgWVT4/pw8MAhYe+ngE7BwcO575L1aPDc9Fgx+2ig=
github.com/linuxsuren/http-downloader v0.0.6/go.mod h1:xxgh2OE7WGL9TwDE9L8Gh7Lqq9fFPuHbh5tofUitEfE=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
Expand Down Expand Up @@ -222,6 +225,8 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.10.3 h1:gph6h/qe9GSUw1NhH1gp+qb+h8rXD8Cy60Z32Qw3ELA=
github.com/onsi/gomega v1.10.3/go.mod h1:V9xEwhxec5O8UDM77eCW8vLymOMltsqPVYWrpDsH8xc=
github.com/onsi/gomega v1.10.4 h1:NiTx7EEvBzu9sFOD1zORteLSt3o8gnlvZZwSE9TnY9U=
github.com/onsi/gomega v1.10.4/go.mod h1:g/HbgYopi++010VEqkFgJHKC09uJiW9UkXvMUuKHUCQ=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
Expand Down Expand Up @@ -354,6 +359,8 @@ golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20201006153459-a7d1128ccaa0 h1:wBouT66WTYFXdxfVdz9sVWARVd/2vfGcmI45D2gj45M=
golang.org/x/net v0.0.0-20201006153459-a7d1128ccaa0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb h1:eBmm0M9fYhWpKZLjQUUKka/LtIxf46G4fxeEz5KJr9U=
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down
2 changes: 2 additions & 0 deletions version/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type SelfUpgradeOption struct {
Repo string
Name string
CustomDownloadFunc CustomDownloadFunc
PathSeparate string
Thread int

GitHubClient *github.Client
RoundTripper http.RoundTripper
Expand Down
36 changes: 25 additions & 11 deletions version/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func (o *SelfUpgradeOption) addFlags(flags *pflag.FlagSet) {
fmt.Sprintf("If you want to show the progress of download %s", o.Name))
flags.BoolVarP(&o.Privilege, "privilege", "", true,
fmt.Sprintf("Try to take the privilege from system if there's no write permission on %s", o.Name))
flags.IntVarP(&o.Thread, "thread", "t", 0,
"Download the target binary file in multi-thread mode. It only works when its value is bigger than 1")
}

// RunE is the main point of current command
Expand Down Expand Up @@ -119,10 +121,14 @@ func (o *SelfUpgradeOption) Download(log common.Printer, version, currentVersion
tmpDir := os.TempDir()
output := fmt.Sprintf("%s/%s.tar.gz", tmpDir, o.Name)

if o.PathSeparate == "" {
o.PathSeparate = "-"
}

var fileURL string
if o.CustomDownloadFunc == nil {
fileURL = fmt.Sprintf("https://github.com/%s/%s/releases/download/%s/%s-%s-%s.tar.gz",
o.Org, o.Repo, version, o.Name, runtime.GOOS, runtime.GOARCH)
fileURL = fmt.Sprintf("https://github.com/%s/%s/releases/download/%s/%s%s%s%s%s.tar.gz",
o.Org, o.Repo, version, o.Name, o.PathSeparate, runtime.GOOS, o.PathSeparate, runtime.GOARCH)
} else {
fileURL = o.CustomDownloadFunc(version)

Expand All @@ -137,15 +143,23 @@ func (o *SelfUpgradeOption) Download(log common.Printer, version, currentVersion
_ = os.RemoveAll(output)
}()

downloader := httpdownloader.HTTPDownloader{
RoundTripper: o.RoundTripper,
TargetFilePath: output,
URL: fileURL,
ShowProgress: o.ShowProgress,
}
if err = downloader.DownloadFile(); err != nil {
err = fmt.Errorf("cannot download %s from %s, error: %v", o.Name, fileURL, err)
return
if o.Thread > 1 {
if err = httpdownloader.DownloadFileWithMultipleThread(fileURL, output, o.Thread, o.ShowProgress); err != nil {
err = fmt.Errorf("cannot download %s from %s, error: %v", o.Name, fileURL, err)
return
}
} else {
// keep this exists, it can avoid error due to the new feature
downloader := httpdownloader.HTTPDownloader{
RoundTripper: o.RoundTripper,
TargetFilePath: output,
URL: fileURL,
ShowProgress: o.ShowProgress,
}
if err = downloader.DownloadFile(); err != nil {
err = fmt.Errorf("cannot download %s from %s, error: %v", o.Name, fileURL, err)
return
}
}

if err = o.extractFiles(output); err == nil {
Expand Down

0 comments on commit 395067c

Please sign in to comment.