|
| 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 | +} |
0 commit comments