-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
134 lines (113 loc) · 2.62 KB
/
main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"
)
func main() {
flInit := flag.Bool("init", false, "init gh-mirror")
flAdd := flag.String("add", "", "add repo to gh-mirror (username/repo)")
flList := flag.Bool("list", false, "list repos in gh-mirror")
flag.Parse()
log.SetFlags(0)
if *flInit {
initCmd()
return
}
if *flAdd != "" {
addCmd(*flAdd)
return
}
if *flList {
listCmd()
return
}
baseCmd()
}
func initCmd() {
createRoot()
createConfig()
}
func addCmd(repo string) {
if !isValidRepoPath(repo) {
log.Printf("usage: gh-mirror -add username/repo")
log.Fatalf("repo path is invalid: %v", repo)
}
addRepoToConfig(repo)
}
func listCmd() {
config := readConfig()
for _, repo := range config.Repos {
fmt.Println(repo)
}
}
func baseCmd() {
config := readConfig()
for i, repo := range config.Repos {
log.SetPrefix(fmt.Sprintf("[%s]: ", repo))
if !isValidRepoPath(repo) {
log.Fatalf("repo path is invalid: %v", repo)
}
if i > 0 {
log.Printf("sleeping for %d seconds...\n", config.SleepDuration)
time.Sleep(time.Duration(config.SleepDuration) * time.Second)
}
if notExistRepo(repo) {
cloneRepo(repo)
} else {
updateRepo(repo)
}
}
}
func cloneRepo(repo string) {
changeWorkDirToRoot()
cloneCmd := "git"
cloneArgs := []string{"clone", "--mirror", fmt.Sprintf("https://github.com/%s", repo), repo}
runCommand(cloneCmd, cloneArgs)
}
func updateRepo(repo string) {
changeWorkDirToRepo(repo)
updateCmd := "git"
updateArgs := []string{"remote", "-v", "update"}
runCommand(updateCmd, updateArgs)
}
func changeWorkDirToRoot() {
dir := getRootPath()
err := os.Chdir(dir)
if err != nil {
log.Fatalf("could not change work directory: %v", err)
}
log.Printf("change work directory to %v\n", dir)
}
func changeWorkDirToRepo(repo string) {
path := filepath.Join(getRootPath(), repo)
err := os.Chdir(path)
if err != nil {
log.Fatalf("could not change work directory: %v", err)
}
log.Printf("change work directory to %v\n", path)
}
func runCommand(cmd string, args []string) {
log.Printf("%s %s\n", cmd, strings.Join(args, " "))
output, err := exec.Command(cmd, args...).CombinedOutput()
if err != nil {
log.Printf("%s\v", string(output))
log.Fatalf("fatal: could not run command: %v", err)
}
log.Println(string(output))
}
func notExistRepo(repo string) bool {
path := filepath.Join(getRootPath(), repo)
_, err := os.Stat(path)
return os.IsNotExist(err)
}
var reValidRepoPath = regexp.MustCompile(`^[A-Za-z0-9-]+/[A-Za-z0-9\._-]+$`)
func isValidRepoPath(repo string) bool {
return reValidRepoPath.MatchString(repo)
}