generated from LinuxSuRen/github-go
/
fetch.go
90 lines (79 loc) · 2.19 KB
/
fetch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package cmd
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/linuxsuren/http-downloader/pkg"
"github.com/linuxsuren/http-downloader/pkg/log"
"github.com/linuxsuren/http-downloader/pkg/installer"
"github.com/spf13/cobra"
)
func newFetchCmd(ctx context.Context) (cmd *cobra.Command) {
opt := &fetchOption{
fetcher: &installer.DefaultFetcher{},
}
cmd = &cobra.Command{
Use: "fetch",
Short: "Fetch the latest hd config",
PreRunE: opt.preRunE,
RunE: opt.runE,
GroupID: configGroup.ID,
}
flags := cmd.Flags()
opt.addFlags(flags)
flags.StringVarP(&opt.branch, "branch", "b", installer.ConfigBranch,
"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*60, "Timeout of fetching")
return
}
func (o *fetchOption) setTimeout(c *cobra.Command) {
if c.Context() != nil {
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 {
err = os.RemoveAll(configDir)
err = pkg.ErrorWrap(err, "failed to remove directory: %s, error %v", configDir, err)
} else {
err = fmt.Errorf("failed to get config directory, error %v", err)
}
}
return
}
func (o *fetchOption) runE(c *cobra.Command, _ []string) (err error) {
logger := log.GetLoggerFromContextOrDefault(c)
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 {
logger.Println()
}
return
}
type fetchOption struct {
searchOption
branch string
reset bool
fetcher installer.Fetcher
cancel context.CancelFunc
retry int
timeout time.Duration
}