Skip to content

Commit 2426552

Browse files
authored
Add GitHub proxy support to download file (#119)
* Add GitHub proxy support to download file * Use strings.Replace instead of strings.ReplaceAll
1 parent 322227d commit 2426552

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

cmd/get.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ func newGetCmd(ctx context.Context) (cmd *cobra.Command) {
3636
"If you accept preRelease as the binary asset from GitHub")
3737
flags.BoolVarP(&opt.AcceptPreRelease, "pre", "", false,
3838
"Same with option --accept-preRelease")
39+
flags.StringVarP(&opt.ProxyGitHub, "proxy-github", "", "",
40+
`The proxy address of github.com, the proxy address will be the prefix of the final address.
41+
Available proxy: gh.api.99988866.xyz
42+
Thanks to https://github.com/hunshcn/gh-proxy`)
3943

4044
flags.IntVarP(&opt.Timeout, "time", "", 10,
4145
`The default timeout in seconds with the HTTP request`)
@@ -52,6 +56,8 @@ func newGetCmd(ctx context.Context) (cmd *cobra.Command) {
5256
flags.StringVarP(&opt.Arch, "arch", "", runtime.GOARCH, "The arch of target binary file")
5357
flags.BoolVarP(&opt.PrintSchema, "print-schema", "", false,
5458
"Print the schema of HDConfig if the flag is true without other function")
59+
60+
_ = cmd.RegisterFlagCompletionFunc("proxy-github", ArrayCompletion("gh.api.99988866.xyz"))
5561
return
5662
}
5763

@@ -64,6 +70,7 @@ type downloadOption struct {
6470
MaxAttempts int
6571
AcceptPreRelease bool
6672
RoundTripper http.RoundTripper
73+
ProxyGitHub string
6774

6875
ContinueAt int64
6976

@@ -119,6 +126,10 @@ func (o *downloadOption) preRunE(cmd *cobra.Command, args []string) (err error)
119126
}
120127
o.URL = targetURL
121128

129+
if o.ProxyGitHub != "" {
130+
o.URL = strings.Replace(o.URL, "github.com", fmt.Sprintf("%s/github.com", o.ProxyGitHub), 1)
131+
}
132+
122133
if o.Output == "" {
123134
var urlObj *url.URL
124135
if urlObj, err = url.Parse(o.URL); err == nil {

cmd/install.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ func newInstallCmd(ctx context.Context) (cmd *cobra.Command) {
4242
"Indicate if install it via go install github.com/xxx/xxx")
4343
flags.StringVarP(&opt.fromBranch, "from-branch", "", "master",
4444
"Only works if the flag --from-source is true")
45+
flags.StringVarP(&opt.ProxyGitHub, "proxy-github", "", "",
46+
`The proxy address of github.com, the proxy address will be the prefix of the final address.
47+
Available proxy: gh.api.99988866.xyz
48+
Thanks to https://github.com/hunshcn/gh-proxy`)
4549

4650
flags.BoolVarP(&opt.Download, "download", "", true,
4751
"If download the package")

cmd/util.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"context"
5+
"github.com/spf13/cobra"
56
"io"
67
"net/http"
78
"os"
@@ -87,3 +88,13 @@ func copyAndCapture(w io.Writer, r io.Reader) ([]byte, error) {
8788
}
8889
}
8990
}
91+
92+
// CompletionFunc is the function for command completion
93+
type CompletionFunc func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)
94+
95+
// ArrayCompletion return a completion which base on an array
96+
func ArrayCompletion(array ...string) CompletionFunc {
97+
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
98+
return array, cobra.ShellCompDirectiveNoFileComp
99+
}
100+
}

0 commit comments

Comments
 (0)