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
|
package main
import (
"fmt"
"log/slog"
"net/url"
"os"
"os/exec"
"strings"
)
func git(dir string, args ...string) error {
cmd := exec.Command("git", args...)
cmd.Dir = dir
// cmd.Stdout = os.Stdout
// cmd.Stderr = os.Stderr
return cmd.Run()
}
func (r RepoConfig) Sync() error {
tmp, err := os.MkdirTemp(os.TempDir(), "horcrux-*")
if err != nil {
return err
}
defer os.RemoveAll(tmp)
if err := git(os.TempDir(), "clone", "--mirror", r.Source, tmp); err != nil {
return err
}
for _, dest := range r.Dest {
token := dest.Forge.Token
if dest.Forge.TokenFile != "" {
tokenBytes, err := os.ReadFile(dest.Forge.TokenFile)
if err != nil {
slog.Error("could not read token file", slog.Any("err", err))
continue
}
token = strings.TrimSpace(string(tokenBytes))
}
client, err := NewForgeClient(dest.Forge.Name, ForgeConfig{
Username: dest.Forge.Username,
Token: token,
RepoName: dest.Forge.RepoName,
Description: fmt.Sprintf("MIRROR of %s", r.Source),
APIURL: dest.Forge.APIURL,
})
if err != nil {
return err
}
ok, err := client.CheckRepoExists()
if err != nil {
return err
}
if !ok {
if err := client.CreateRepo(); err != nil {
return err
}
}
u, err := url.Parse(dest.URL)
if err != nil {
return err
}
u.User = url.UserPassword(dest.Forge.Username, token)
if err := git(tmp, "remote", "add", "--mirror=push", dest.Forge.Name, u.String()); err != nil {
return err
}
if err := git(tmp, "push", "--mirror", "--force-with-lease", dest.Forge.Name); err != nil {
return err
}
}
return nil
}
|