Skip to content

Commit

Permalink
feat: support for setting multiple mirror sites in the G_MIRROR envir…
Browse files Browse the repository at this point in the history
…onment variable. voidint#70
  • Loading branch information
voidint committed Jun 8, 2022
1 parent f1afc1a commit a235977
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
9 changes: 2 additions & 7 deletions cli/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"os/exec"
"path/filepath"
"runtime"
"strings"

ct "github.com/daviddengcn/go-colortext"
"github.com/dixonwille/wlog/v3"
"github.com/dixonwille/wmenu/v5"
"github.com/mholt/archiver/v3"
"github.com/urfave/cli/v2"
"github.com/voidint/g/collector"
"github.com/voidint/g/collector/official"
"github.com/voidint/g/version"
)

Expand All @@ -29,13 +29,8 @@ func install(ctx *cli.Context) (err error) {
return cli.Exit(fmt.Sprintf("[g] %q version has been installed.", vname), 1)
}

var url string
if url = os.Getenv(mirrorEnv); url == "" {
url = official.DefaultDownloadPageURL
}

// 查找版本
c, err := collector.NewCollector(url)
c, err := collector.NewCollector(strings.Split(os.Getenv(mirrorEnv), ",")...)
if err != nil {
return cli.Exit(errstring(err), 1)
}
Expand Down
8 changes: 1 addition & 7 deletions cli/ls_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/k0kubun/go-ansi"
"github.com/urfave/cli/v2"
"github.com/voidint/g/collector"
"github.com/voidint/g/collector/official"
"github.com/voidint/g/version"
)

Expand All @@ -24,12 +23,7 @@ func listRemote(ctx *cli.Context) (err error) {
return cli.ShowSubcommandHelp(ctx)
}

var url string
if url = os.Getenv(mirrorEnv); url == "" {
url = official.DefaultDownloadPageURL
}

c, err := collector.NewCollector(url)
c, err := collector.NewCollector(strings.Split(os.Getenv(mirrorEnv), ",")...)
if err != nil {
return cli.Exit(errstring(err), 1)
}
Expand Down
23 changes: 18 additions & 5 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,23 @@ type Collector interface {
AllVersions() (items []*version.Version, err error)
}

// NewCollector 返回采集器实例
func NewCollector(url string) (Collector, error) {
if strings.HasPrefix(aliyun.DownloadPageURL, url) {
return aliyun.NewCollector()
// NewCollector 返回首个可用的采集器实例
func NewCollector(urls ...string) (c Collector, err error) {
if len(urls) == 0 {
urls = []string{official.DefaultDownloadPageURL}
}
return official.NewCollector(url)
for i := range urls {
urls[i] = strings.TrimSpace(urls[i])

if urls[i] != "" && (strings.HasPrefix(aliyun.DownloadPageURL, urls[i]) || strings.HasPrefix(urls[i], aliyun.DownloadPageURL)) {
if c, err = aliyun.NewCollector(); err == nil {
return c, nil
}
}

if c, err = official.NewCollector(urls[i]); err == nil {
return c, nil
}
}
return c, err
}
3 changes: 3 additions & 0 deletions collector/official/official_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type Collector struct {

// NewCollector 返回采集器实例
func NewCollector(url string) (*Collector, error) {
if url == "" {
url = DefaultDownloadPageURL
}
pURL, err := stdurl.Parse(url)
if err != nil {
return nil, err
Expand Down

0 comments on commit a235977

Please sign in to comment.