Skip to content

Commit 15e162d

Browse files
committed
feat: 添加本地备份提供者支持,更新配置类型并实现相关功能
1 parent d2ccb17 commit 15e162d

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type BackupProviderConfigType string
88

99
const (
1010
BackupProviderConfigTypeGitea BackupProviderConfigType = "gitea"
11-
BackupProviderConfigTypeFile BackupProviderConfigType = "file"
11+
BackupProviderConfigTypeLocal BackupProviderConfigType = "local"
1212
)
1313

1414
type UnmatchedRepoAction string

provider/local/local.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package local
2+
3+
import (
4+
"fmt"
5+
"github.com/TBXark/github-backup/provider/provider"
6+
"log"
7+
"os"
8+
"os/exec"
9+
"path/filepath"
10+
)
11+
12+
type Config struct {
13+
Root string `json:"root"`
14+
Questions bool `json:"questions"`
15+
}
16+
17+
var _ provider.Provider = &Local{}
18+
19+
type Local struct {
20+
conf *Config
21+
}
22+
23+
func NewLocal(conf *Config) *Local {
24+
return &Local{conf: conf}
25+
}
26+
27+
func (l *Local) LoadRepos(owner *provider.Owner) ([]string, error) {
28+
ownerPath := filepath.Join(l.conf.Root, owner.Name)
29+
dirEntries, err := os.ReadDir(ownerPath)
30+
if err != nil {
31+
return nil, err
32+
}
33+
repos := make([]string, 0)
34+
for _, dirEntry := range dirEntries {
35+
if dirEntry.IsDir() && isGitRepository(filepath.Join(ownerPath, dirEntry.Name())) {
36+
repos = append(repos, dirEntry.Name())
37+
}
38+
}
39+
return repos, nil
40+
}
41+
42+
func (l *Local) MigrateRepo(from *provider.Owner, to *provider.Owner, repo *provider.Repo) (string, error) {
43+
if l.conf.Questions && !question(fmt.Sprintf("Are you sure you want to migrate %s/%s to %s/%s? [y/n]: ", from.Name, repo.Name, to.Name, repo.Name)) {
44+
return "skip", nil
45+
}
46+
ownerPath := filepath.Join(l.conf.Root, to.Name)
47+
_, err := os.Stat(ownerPath)
48+
if err != nil {
49+
if os.IsNotExist(err) {
50+
if e := os.MkdirAll(ownerPath, os.ModePerm); e != nil {
51+
return "", e
52+
}
53+
} else {
54+
return "", err
55+
}
56+
}
57+
repoPath := filepath.Join(ownerPath, repo.Name)
58+
_, err = os.Stat(repoPath)
59+
gitUrl := fmt.Sprintf("git@github.com:%s/%s.git", from.Name, repo.Name)
60+
if err != nil {
61+
if os.IsNotExist(err) {
62+
err = gitClone(gitUrl, repoPath)
63+
if err != nil {
64+
return "success", err
65+
}
66+
} else {
67+
return "", err
68+
}
69+
}
70+
err = gitPull(repoPath)
71+
if err != nil {
72+
return "fail", err
73+
}
74+
return "success", nil
75+
}
76+
77+
func (l *Local) DeleteRepo(owner, repo string) (string, error) {
78+
if l.conf.Questions && !question(fmt.Sprintf("Are you sure you want to delete %s/%s? [y/n]: ", owner, repo)) {
79+
return "skip", nil
80+
}
81+
repoPath := filepath.Join(l.conf.Root, owner, repo)
82+
err := os.RemoveAll(repoPath)
83+
if err != nil {
84+
return "fail", err
85+
}
86+
return "success", nil
87+
}
88+
89+
func isGitRepository(path string) bool {
90+
cmd := exec.Command("git", "rev-parse", "--is-inside-work-tree")
91+
cmd.Dir = path
92+
err := cmd.Run()
93+
return err == nil
94+
}
95+
96+
func gitClone(url, path string) error {
97+
log.Printf("cloning %s", url)
98+
cmd := exec.Command("git", "clone", url, path)
99+
return cmd.Run()
100+
}
101+
102+
func gitPull(path string) error {
103+
log.Printf("pulling %s", path)
104+
cmd := exec.Command("git", "pull")
105+
cmd.Dir = path
106+
return cmd.Run()
107+
}
108+
109+
func question(message string) bool {
110+
var response string
111+
fmt.Print(message)
112+
_, err := fmt.Scanln(&response)
113+
if err != nil {
114+
return false
115+
}
116+
return response == "y"
117+
}

task.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/TBXark/github-backup/config"
66
"github.com/TBXark/github-backup/provider/gitea"
77
"github.com/TBXark/github-backup/provider/github"
8+
"github.com/TBXark/github-backup/provider/local"
89
"github.com/TBXark/github-backup/provider/provider"
910
"github.com/TBXark/github-backup/utils/matcher"
1011
"log"
@@ -18,6 +19,12 @@ func BuildBackupProvider(conf *config.BackupProviderConfig) (provider.Provider,
1819
return nil, err
1920
}
2021
return gitea.NewGitea(c), nil
22+
case config.BackupProviderConfigTypeLocal:
23+
c, err := config.Convert[local.Config](conf.Config)
24+
if err != nil {
25+
return nil, err
26+
}
27+
return local.NewLocal(c), nil
2128
}
2229
return nil, fmt.Errorf("unknown backup provider type: %s", conf.Type)
2330
}

0 commit comments

Comments
 (0)