Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions cmd/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"github.com/linuxsuren/http-downloader/pkg"
"os"
"strings"
"time"

"github.com/linuxsuren/http-downloader/pkg/installer"
"github.com/spf13/cobra"
Expand All @@ -27,15 +29,23 @@ func newFetchCmd(context.Context) (cmd *cobra.Command) {
"The branch of git repository (not support currently)")
flags.BoolVarP(&opt.reset, "reset", "", false,
"If you want to reset the hd-config which means delete and clone it again")
flags.IntVarP(&opt.retry, "retry", "", 6, "Retry times due to timeout error")
flags.DurationVarP(&opt.timeout, "timeout", "", time.Second*10, "Timeout of fetching")

_ = cmd.RegisterFlagCompletionFunc("provider", ArrayCompletion(ProviderGitHub, ProviderGitee))
return
}

func (o *fetchOption) preRunE(c *cobra.Command, _ []string) (err error) {
func (o *fetchOption) setTimeout(c *cobra.Command) {
if c.Context() != nil {
o.fetcher.SetContext(c.Context())
var ctx context.Context
ctx, o.cancel = context.WithTimeout(c.Context(), o.timeout)
o.fetcher.SetContext(ctx)
}
}

func (o *fetchOption) preRunE(c *cobra.Command, _ []string) (err error) {
o.setTimeout(c)
if o.reset {
var configDir string
if configDir, err = o.fetcher.GetConfigDir(); err == nil {
Expand All @@ -48,8 +58,21 @@ func (o *fetchOption) preRunE(c *cobra.Command, _ []string) (err error) {
return
}

func (o *fetchOption) runE(cmd *cobra.Command, _ []string) (err error) {
return o.fetcher.FetchLatestRepo(o.Provider, o.branch, cmd.OutOrStdout())
func (o *fetchOption) runE(c *cobra.Command, _ []string) (err error) {
var i int
for i = 0; i < o.retry; i++ {
err = o.fetcher.FetchLatestRepo(o.Provider, o.branch, c.OutOrStdout())
if err == nil || (!strings.Contains(err.Error(), "context deadline exceeded") &&
!strings.Contains(err.Error(), "i/o timeout")) {
break
}
o.setTimeout(c)
c.Print(".")
}
if i >= 1 {
c.Println()
}
return
}

type fetchOption struct {
Expand All @@ -58,4 +81,7 @@ type fetchOption struct {
branch string
reset bool
fetcher installer.Fetcher
cancel context.CancelFunc
retry int
timeout time.Duration
}
41 changes: 35 additions & 6 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"github.com/linuxsuren/http-downloader/pkg/exec"
"net/http"
"net/url"
sysos "os"
Expand Down Expand Up @@ -126,7 +127,7 @@ func (o *downloadOption) fetch() (err error) {
go func() {
// no need to handle the error due to this is a background task
if o.fetcher != nil {
err = o.fetcher.FetchLatestRepo(o.Provider, installer.ConfigBranch, bytes.NewBuffer(nil))
err = o.fetcher.FetchLatestRepo(o.Provider, installer.ConfigBranch, bytes.NewBuffer([]byte{}))
}
o.wait.Done()
}()
Expand Down Expand Up @@ -169,7 +170,11 @@ func (o *downloadOption) preRunE(cmd *cobra.Command, args []string) (err error)

targetURL := args[0]
o.Package = &installer.HDConfig{}
if !strings.HasPrefix(targetURL, "http://") && !strings.HasPrefix(targetURL, "https://") {
if strings.HasPrefix(targetURL, "magnet:?") {
// download via external tool
o.URL = targetURL
return
} else if !strings.HasPrefix(targetURL, "http://") && !strings.HasPrefix(targetURL, "https://") {
ins := &installer.Installer{
Provider: o.Provider,
OS: o.OS,
Expand All @@ -188,10 +193,6 @@ func (o *downloadOption) preRunE(cmd *cobra.Command, args []string) (err error)
}
o.URL = targetURL

if o.ProxyGitHub != "" {
o.URL = strings.Replace(o.URL, "github.com", fmt.Sprintf("%s/github.com", o.ProxyGitHub), 1)
}

if o.Output == "" {
var urlObj *url.URL
if urlObj, err = url.Parse(o.URL); err == nil {
Expand Down Expand Up @@ -246,6 +247,11 @@ func (o *downloadOption) runE(cmd *cobra.Command, args []string) (err error) {
return
}

if strings.HasPrefix(o.URL, "magnet:?") {
err = downloadMagnetFile(o.ProxyGitHub, o.URL)
return
}

cmd.Printf("start to download from %s\n", o.URL)
if o.Thread <= 1 {
downloader := &net.ContinueDownloader{}
Expand All @@ -262,3 +268,26 @@ func (o *downloadOption) runE(cmd *cobra.Command, args []string) (err error) {
}
return
}

func downloadMagnetFile(proxyGitHub, target string) (err error) {
targetCmd := "gotorrent"
execer := exec.DefaultExecer{}
is := installer.Installer{
Provider: "github",
Execer: execer,
ProxyGitHub: proxyGitHub,
}
if err = is.CheckDepAndInstall(map[string]string{
targetCmd: "linuxsuren/gotorrent",
}); err != nil {
return
}

var targetBinary string
if targetBinary, err = execer.LookPath(targetCmd); err == nil {
sysCallArgs := []string{targetCmd}
sysCallArgs = append(sysCallArgs, []string{"download", target}...)
err = execer.SystemCall(targetBinary, sysCallArgs, sysos.Environ())
}
return
}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ require (
github.com/nxadm/tail v1.4.8 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.11.1 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
Expand All @@ -58,9 +59,9 @@ require (
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/text v0.3.8 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/ini.v1 v1.63.2 // indirect
Expand Down
Loading