Skip to content

Commit

Permalink
feat: 添加本地备份提供者支持,更新配置类型并实现相关功能
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Jan 24, 2025
1 parent d2ccb17 commit 15e162d
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 1 deletion.
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type BackupProviderConfigType string

const (
BackupProviderConfigTypeGitea BackupProviderConfigType = "gitea"
BackupProviderConfigTypeFile BackupProviderConfigType = "file"
BackupProviderConfigTypeLocal BackupProviderConfigType = "local"
)

type UnmatchedRepoAction string
Expand Down
117 changes: 117 additions & 0 deletions provider/local/local.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package local

import (
"fmt"
"github.com/TBXark/github-backup/provider/provider"
"log"
"os"
"os/exec"
"path/filepath"
)

type Config struct {
Root string `json:"root"`
Questions bool `json:"questions"`
}

var _ provider.Provider = &Local{}

type Local struct {
conf *Config
}

func NewLocal(conf *Config) *Local {
return &Local{conf: conf}
}

func (l *Local) LoadRepos(owner *provider.Owner) ([]string, error) {
ownerPath := filepath.Join(l.conf.Root, owner.Name)
dirEntries, err := os.ReadDir(ownerPath)
if err != nil {
return nil, err
}
repos := make([]string, 0)
for _, dirEntry := range dirEntries {
if dirEntry.IsDir() && isGitRepository(filepath.Join(ownerPath, dirEntry.Name())) {
repos = append(repos, dirEntry.Name())
}
}
return repos, nil
}

func (l *Local) MigrateRepo(from *provider.Owner, to *provider.Owner, repo *provider.Repo) (string, error) {
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)) {
return "skip", nil
}
ownerPath := filepath.Join(l.conf.Root, to.Name)
_, err := os.Stat(ownerPath)
if err != nil {
if os.IsNotExist(err) {
if e := os.MkdirAll(ownerPath, os.ModePerm); e != nil {
return "", e
}
} else {
return "", err
}
}
repoPath := filepath.Join(ownerPath, repo.Name)
_, err = os.Stat(repoPath)
gitUrl := fmt.Sprintf("[email protected]:%s/%s.git", from.Name, repo.Name)
if err != nil {
if os.IsNotExist(err) {
err = gitClone(gitUrl, repoPath)
if err != nil {
return "success", err
}
} else {
return "", err
}
}
err = gitPull(repoPath)
if err != nil {
return "fail", err
}
return "success", nil
}

func (l *Local) DeleteRepo(owner, repo string) (string, error) {
if l.conf.Questions && !question(fmt.Sprintf("Are you sure you want to delete %s/%s? [y/n]: ", owner, repo)) {
return "skip", nil
}
repoPath := filepath.Join(l.conf.Root, owner, repo)
err := os.RemoveAll(repoPath)
if err != nil {
return "fail", err
}
return "success", nil
}

func isGitRepository(path string) bool {
cmd := exec.Command("git", "rev-parse", "--is-inside-work-tree")
cmd.Dir = path
err := cmd.Run()
return err == nil
}

func gitClone(url, path string) error {
log.Printf("cloning %s", url)
cmd := exec.Command("git", "clone", url, path)
return cmd.Run()
}

func gitPull(path string) error {
log.Printf("pulling %s", path)
cmd := exec.Command("git", "pull")
cmd.Dir = path
return cmd.Run()
}

func question(message string) bool {
var response string
fmt.Print(message)
_, err := fmt.Scanln(&response)
if err != nil {
return false
}
return response == "y"
}
7 changes: 7 additions & 0 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/TBXark/github-backup/config"
"github.com/TBXark/github-backup/provider/gitea"
"github.com/TBXark/github-backup/provider/github"
"github.com/TBXark/github-backup/provider/local"
"github.com/TBXark/github-backup/provider/provider"
"github.com/TBXark/github-backup/utils/matcher"
"log"
Expand All @@ -18,6 +19,12 @@ func BuildBackupProvider(conf *config.BackupProviderConfig) (provider.Provider,
return nil, err
}
return gitea.NewGitea(c), nil
case config.BackupProviderConfigTypeLocal:
c, err := config.Convert[local.Config](conf.Config)
if err != nil {
return nil, err
}
return local.NewLocal(c), nil
}
return nil, fmt.Errorf("unknown backup provider type: %s", conf.Type)
}
Expand Down

0 comments on commit 15e162d

Please sign in to comment.