-
Notifications
You must be signed in to change notification settings - Fork 2
/
mirror.go
78 lines (66 loc) · 2.05 KB
/
mirror.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
package cmd
import (
"context"
"fmt"
"github.com/linuxsuren/cgit/pkg"
"github.com/spf13/cobra"
"net/url"
"strings"
)
type mirrorOption struct {
enable bool
remote string
}
// NewMirrorCmd returns the mirror command
func NewMirrorCmd(ctx context.Context) (cmd *cobra.Command) {
opt := mirrorOption{}
cmd = &cobra.Command{
Use: "mirror",
Short: "Toggle the git mirror",
RunE: opt.runE,
}
flags := cmd.Flags()
flags.BoolVarP(&opt.enable, "enable", "e", true, "Enable/disable the git mirror")
flags.StringVarP(&opt.remote, "remote", "r", "origin", "The remote of git repository")
return
}
func (o *mirrorOption) runE(cmd *cobra.Command, args []string) (err error) {
var remoteURLStr string
var remotePushURLStr string
if remoteURLStr, err = pkg.ExecCommandWithOutput("git", "", "remote", "get-url", o.remote); err != nil {
return
}
if remotePushURLStr, err = pkg.ExecCommandWithOutput("git", "", "remote", "get-url", "--push", o.remote); err != nil {
return
}
// remove the .git tail
remoteURLStr = strings.TrimSuffix(remoteURLStr, ".git")
gitProtocol := strings.HasPrefix(remoteURLStr, "git@")
if gitProtocol {
remoteURLStr = strings.ReplaceAll(remoteURLStr, "git@github.com:", "https://github.com/")
}
var remoteURL *url.URL
if remoteURL, err = url.Parse(remoteURLStr); err != nil {
cmd.Println("error with parse URL", remoteURLStr)
return
}
if o.enable {
remoteURL.Host = "github.com.cnpmjs.org"
} else {
remoteURL.Host = "github.com"
}
targetRemoteURLStr := remoteURL.String()
if strings.HasPrefix(remotePushURLStr, "git@") && !o.enable {
// the mirror git server does not support git protocol
targetRemoteURLStr = strings.ReplaceAll(targetRemoteURLStr, fmt.Sprintf("https://%s/", remoteURL.Host),
fmt.Sprintf("git@%s:", remoteURL.Host))
targetRemoteURLStr += ".git"
}
if err = pkg.ExecCommandInDir("git", "", "remote", "set-url", o.remote, targetRemoteURLStr); err != nil {
return
}
if err = pkg.ExecCommandInDir("git", "", "remote", "set-url", "--push", o.remote, remotePushURLStr); err != nil {
return
}
return
}