diff --git a/cmd/get.go b/cmd/get.go index 9fe5eb9..362e1f0 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -252,19 +252,23 @@ func (o *downloadOption) runE(cmd *cobra.Command, args []string) (err error) { return } - cmd.Printf("start to download from %s\n", o.URL) + targetURL := o.URL + if o.ProxyGitHub != "" { + targetURL = strings.Replace(targetURL, "github.com", fmt.Sprintf("%s/github.com", o.ProxyGitHub), 1) + } + cmd.Printf("start to download from %s\n", targetURL) if o.Thread <= 1 { downloader := &net.ContinueDownloader{} downloader.WithoutProxy(o.NoProxy). WithRoundTripper(o.RoundTripper) - err = downloader.DownloadWithContinue(o.URL, o.Output, o.ContinueAt, -1, 0, o.ShowProgress) + err = downloader.DownloadWithContinue(targetURL, o.Output, o.ContinueAt, -1, 0, o.ShowProgress) } else { downloader := &net.MultiThreadDownloader{} downloader.WithKeepParts(o.KeepPart). WithShowProgress(o.ShowProgress). WithoutProxy(o.NoProxy). WithRoundTripper(o.RoundTripper) - err = downloader.Download(o.URL, o.Output, o.Thread) + err = downloader.Download(targetURL, o.Output, o.Thread) } return } diff --git a/pkg/installer/process.go b/pkg/installer/process.go index 6fa4ee2..bc57b9d 100644 --- a/pkg/installer/process.go +++ b/pkg/installer/process.go @@ -9,6 +9,7 @@ import ( "os" "path" "path/filepath" + "strings" ) // Install installs a package @@ -64,13 +65,27 @@ func (o *Installer) Install() (err error) { if configFile.OS == o.Execer.OS() { if err = os.MkdirAll(configDir, 0750); err != nil { - err = fmt.Errorf("cannot create config dir: %s, error: %v", configDir, err) - return + if strings.Contains(err.Error(), "permission denied") { + err = o.Execer.RunCommandWithSudo("mkdir", "-p", configDir) + } + + if err != nil { + err = fmt.Errorf("cannot create config dir: %s, error: %v", configDir, err) + return + } } if err = os.WriteFile(configFilePath, []byte(configFile.Content), 0622); err != nil { - err = fmt.Errorf("cannot write config file: %s, error: %v", configFilePath, err) - return + if strings.Contains(err.Error(), "permission denied") { + if err = o.Execer.RunCommandWithSudo("touch", configFilePath); err == nil { + err = o.Execer.RunCommandWithSudo("chmod", "+w", configFilePath) + } + } + + if err != nil { + err = fmt.Errorf("cannot write config file: %s, error: %v", configFilePath, err) + return + } } fmt.Printf("config file [%s] is ready.\n", configFilePath)