Skip to content

Commit 171d64a

Browse files
authored
fix: github proxy not working when install (#331)
1 parent 00eae5a commit 171d64a

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

cmd/get.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,19 +252,23 @@ func (o *downloadOption) runE(cmd *cobra.Command, args []string) (err error) {
252252
return
253253
}
254254

255-
cmd.Printf("start to download from %s\n", o.URL)
255+
targetURL := o.URL
256+
if o.ProxyGitHub != "" {
257+
targetURL = strings.Replace(targetURL, "github.com", fmt.Sprintf("%s/github.com", o.ProxyGitHub), 1)
258+
}
259+
cmd.Printf("start to download from %s\n", targetURL)
256260
if o.Thread <= 1 {
257261
downloader := &net.ContinueDownloader{}
258262
downloader.WithoutProxy(o.NoProxy).
259263
WithRoundTripper(o.RoundTripper)
260-
err = downloader.DownloadWithContinue(o.URL, o.Output, o.ContinueAt, -1, 0, o.ShowProgress)
264+
err = downloader.DownloadWithContinue(targetURL, o.Output, o.ContinueAt, -1, 0, o.ShowProgress)
261265
} else {
262266
downloader := &net.MultiThreadDownloader{}
263267
downloader.WithKeepParts(o.KeepPart).
264268
WithShowProgress(o.ShowProgress).
265269
WithoutProxy(o.NoProxy).
266270
WithRoundTripper(o.RoundTripper)
267-
err = downloader.Download(o.URL, o.Output, o.Thread)
271+
err = downloader.Download(targetURL, o.Output, o.Thread)
268272
}
269273
return
270274
}

pkg/installer/process.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"path"
1111
"path/filepath"
12+
"strings"
1213
)
1314

1415
// Install installs a package
@@ -64,13 +65,27 @@ func (o *Installer) Install() (err error) {
6465

6566
if configFile.OS == o.Execer.OS() {
6667
if err = os.MkdirAll(configDir, 0750); err != nil {
67-
err = fmt.Errorf("cannot create config dir: %s, error: %v", configDir, err)
68-
return
68+
if strings.Contains(err.Error(), "permission denied") {
69+
err = o.Execer.RunCommandWithSudo("mkdir", "-p", configDir)
70+
}
71+
72+
if err != nil {
73+
err = fmt.Errorf("cannot create config dir: %s, error: %v", configDir, err)
74+
return
75+
}
6976
}
7077

7178
if err = os.WriteFile(configFilePath, []byte(configFile.Content), 0622); err != nil {
72-
err = fmt.Errorf("cannot write config file: %s, error: %v", configFilePath, err)
73-
return
79+
if strings.Contains(err.Error(), "permission denied") {
80+
if err = o.Execer.RunCommandWithSudo("touch", configFilePath); err == nil {
81+
err = o.Execer.RunCommandWithSudo("chmod", "+w", configFilePath)
82+
}
83+
}
84+
85+
if err != nil {
86+
err = fmt.Errorf("cannot write config file: %s, error: %v", configFilePath, err)
87+
return
88+
}
7489
}
7590

7691
fmt.Printf("config file [%s] is ready.\n", configFilePath)

0 commit comments

Comments
 (0)