@@ -3,6 +3,7 @@ package installer
3
3
import (
4
4
"fmt"
5
5
"github.com/go-git/go-git/v5"
6
+ "github.com/go-git/go-git/v5/config"
6
7
"github.com/linuxsuren/http-downloader/pkg/common"
7
8
"github.com/mitchellh/go-homedir"
8
9
"io"
@@ -18,9 +19,9 @@ const (
18
19
ConfigBranch = "master"
19
20
)
20
21
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" ,
24
25
}
25
26
26
27
// GetConfigDir returns the directory of the config
@@ -33,7 +34,21 @@ func GetConfigDir() (configDir string, err error) {
33
34
}
34
35
35
36
// 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
+
37
52
var configDir string
38
53
if configDir , err = GetConfigDir (); err != nil {
39
54
return
@@ -45,9 +60,15 @@ func FetchLatestRepo(repoAddr string, branch string, progress io.Writer) (err er
45
60
var wd * git.Worktree
46
61
47
62
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
+
48
68
if err = wd .Pull (& git.PullOptions {
49
- Progress : progress ,
50
- Force : true ,
69
+ RemoteName : remoteName ,
70
+ Progress : progress ,
71
+ Force : true ,
51
72
}); err != nil && err != git .NoErrAlreadyUpToDate {
52
73
err = fmt .Errorf ("failed to pull git repository '%s', error: %v" , repo , err )
53
74
return
@@ -59,8 +80,9 @@ func FetchLatestRepo(repoAddr string, branch string, progress io.Writer) (err er
59
80
}
60
81
} else {
61
82
if _ , err = git .PlainClone (configDir , false , & git.CloneOptions {
62
- URL : repoAddr ,
63
- Progress : progress ,
83
+ RemoteName : remoteName ,
84
+ URL : repoAddr ,
85
+ Progress : progress ,
64
86
}); err != nil {
65
87
err = fmt .Errorf ("failed to clone git repository '%s' into '%s', error: %v" , repoAddr , configDir , err )
66
88
}
@@ -73,3 +95,13 @@ func FetchLatestRepo(repoAddr string, branch string, progress io.Writer) (err er
73
95
}
74
96
return
75
97
}
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