Skip to content

Commit ad87434

Browse files
authored
Add gitee as a provider (#125)
1 parent 2ab7ce4 commit ad87434

File tree

6 files changed

+58
-22
lines changed

6 files changed

+58
-22
lines changed

cmd/fetch.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ func newFetchCmd(context.Context) (cmd *cobra.Command) {
1919

2020
flags := cmd.Flags()
2121
flags.StringVarP(&opt.provider, "provider", "p", "github",
22-
"The provider of hd-home repository. You can pass it a name (github, gitee) or a public git repository URI. Please use option --reset=true if you want to change the provider.")
22+
"The provider of hd-home repository")
2323
flags.StringVarP(&opt.branch, "branch", "b", installer.ConfigBranch,
2424
"The branch of git repository (not support currently)")
2525
flags.BoolVarP(&opt.reset, "reset", "", false,
2626
"If you want to reset the hd-config which means delete and clone it again")
27+
28+
_ = cmd.RegisterFlagCompletionFunc("provider", ArrayCompletion(ProviderGitHub, "gitee"))
2729
return
2830
}
2931

cmd/get.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ Thanks to https://github.com/hunshcn/gh-proxy`)
5757
flags.BoolVarP(&opt.PrintSchema, "print-schema", "", false,
5858
"Print the schema of HDConfig if the flag is true without other function")
5959

60-
_ = cmd.RegisterFlagCompletionFunc("proxy-github", ArrayCompletion("gh.api.99988866.xyz"))
60+
_ = cmd.RegisterFlagCompletionFunc("proxy-github", ArrayCompletion("gh.api.99988866.xyz",
61+
"ghproxy.com", "mirror.ghproxy.com"))
62+
_ = cmd.RegisterFlagCompletionFunc("provider", ArrayCompletion(ProviderGitHub, "gitee"))
6163
return
6264
}
6365

cmd/install.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ Thanks to https://github.com/hunshcn/gh-proxy`)
6060
flags.StringVarP(&opt.Provider, "provider", "", ProviderGitHub, "The file provider")
6161
flags.StringVarP(&opt.OS, "os", "", runtime.GOOS, "The OS of target binary file")
6262
flags.StringVarP(&opt.Arch, "arch", "", runtime.GOARCH, "The arch of target binary file")
63+
64+
_ = cmd.RegisterFlagCompletionFunc("provider", ArrayCompletion(ProviderGitHub, "gitee"))
6365
return
6466
}
6567

cmd/search.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"github.com/linuxsuren/http-downloader/pkg/installer"
77
"github.com/spf13/cobra"
8+
sysos "os"
89
"path"
910
"path/filepath"
1011
"strings"
@@ -24,7 +25,7 @@ func newSearchCmd(context.Context) (cmd *cobra.Command) {
2425
}
2526

2627
func search(keyword string) (err error) {
27-
if err = installer.FetchConfig(); err != nil {
28+
if err = installer.FetchLatestRepo("", "", sysos.Stdout); err != nil {
2829
return
2930
}
3031

pkg/installer/check.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"gopkg.in/yaml.v2"
1414
"io/ioutil"
1515
"net/url"
16+
sysos "os"
1617
"path"
1718
"path/filepath"
1819
"runtime"
@@ -74,8 +75,13 @@ func (o *Installer) CheckDepAndInstall(tools map[string]string) (err error) {
7475
// ProviderURLParse parse the URL
7576
func (o *Installer) ProviderURLParse(path string, acceptPreRelease bool) (url string, err error) {
7677
url = path
77-
if o.Provider != ProviderGitHub {
78-
return
78+
if o.Fetch {
79+
// fetch the latest config
80+
fmt.Println("start to fetch the config")
81+
if err = FetchLatestRepo(o.Provider, ConfigBranch, sysos.Stdout); err != nil {
82+
err = fmt.Errorf("unable to fetch the latest config, error: %v", err)
83+
return
84+
}
7985
}
8086

8187
var (
@@ -134,15 +140,6 @@ func (o *Installer) ProviderURLParse(path string, acceptPreRelease bool) (url st
134140
}
135141
o.Name = name
136142

137-
if o.Fetch {
138-
// fetch the latest config
139-
fmt.Println("start to fetch the config")
140-
if err = FetchConfig(); err != nil {
141-
err = fmt.Errorf("unable to fetch the latest config, error: %v", err)
142-
return
143-
}
144-
}
145-
146143
// try to parse from config
147144
userHome, _ := homedir.Dir()
148145
configDir := userHome + "/.config/hd-home"

pkg/installer/fetch.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package installer
33
import (
44
"fmt"
55
"github.com/go-git/go-git/v5"
6+
"github.com/go-git/go-git/v5/config"
67
"github.com/linuxsuren/http-downloader/pkg/common"
78
"github.com/mitchellh/go-homedir"
89
"io"
@@ -18,9 +19,9 @@ const (
1819
ConfigBranch = "master"
1920
)
2021

21-
// FetchConfig fetches the hd-home as the config
22-
func FetchConfig() (err error) {
23-
return FetchLatestRepo(ConfigGitHub, ConfigBranch, os.Stdout)
22+
var configRepos = map[string]string{
23+
"github": ConfigGitHub,
24+
"gitee": "https://gitee.com/LinuxSuRen/hd-home",
2425
}
2526

2627
// GetConfigDir returns the directory of the config
@@ -33,7 +34,21 @@ func GetConfigDir() (configDir string, err error) {
3334
}
3435

3536
// FetchLatestRepo fetches the hd-home as the config
36-
func FetchLatestRepo(repoAddr string, branch string, progress io.Writer) (err error) {
37+
func FetchLatestRepo(provider string, branch string, progress io.Writer) (err error) {
38+
repoAddr, ok := configRepos[provider]
39+
if !ok {
40+
repoAddr = ConfigGitHub
41+
}
42+
43+
if branch == "" {
44+
branch = ConfigBranch
45+
}
46+
47+
remoteName := "origin"
48+
if repoAddr != ConfigGitHub {
49+
remoteName = provider
50+
}
51+
3752
var configDir string
3853
if configDir, err = GetConfigDir(); err != nil {
3954
return
@@ -45,9 +60,15 @@ func FetchLatestRepo(repoAddr string, branch string, progress io.Writer) (err er
4560
var wd *git.Worktree
4661

4762
if wd, err = repo.Worktree(); err == nil {
63+
if err = makeSureRemove(remoteName, repoAddr, repo); err != nil {
64+
err = fmt.Errorf("cannot add remote: %s, address: %s, error: %v", remoteName, repoAddr, err)
65+
return
66+
}
67+
4868
if err = wd.Pull(&git.PullOptions{
49-
Progress: progress,
50-
Force: true,
69+
RemoteName: remoteName,
70+
Progress: progress,
71+
Force: true,
5172
}); err != nil && err != git.NoErrAlreadyUpToDate {
5273
err = fmt.Errorf("failed to pull git repository '%s', error: %v", repo, err)
5374
return
@@ -59,8 +80,9 @@ func FetchLatestRepo(repoAddr string, branch string, progress io.Writer) (err er
5980
}
6081
} else {
6182
if _, err = git.PlainClone(configDir, false, &git.CloneOptions{
62-
URL: repoAddr,
63-
Progress: progress,
83+
RemoteName: remoteName,
84+
URL: repoAddr,
85+
Progress: progress,
6486
}); err != nil {
6587
err = fmt.Errorf("failed to clone git repository '%s' into '%s', error: %v", repoAddr, configDir, err)
6688
}
@@ -73,3 +95,13 @@ func FetchLatestRepo(repoAddr string, branch string, progress io.Writer) (err er
7395
}
7496
return
7597
}
98+
99+
func makeSureRemove(name, repoAddr string, repo *git.Repository) (err error) {
100+
if _, err = repo.Remote(name); err != nil {
101+
_, err = repo.CreateRemote(&config.RemoteConfig{
102+
Name: name,
103+
URLs: []string{repoAddr},
104+
})
105+
}
106+
return
107+
}

0 commit comments

Comments
 (0)