Skip to content

Commit 66cc39e

Browse files
committed
feat: optimize error handling
- `reaper run` would continue on error - update README.md
1 parent d5d6536 commit 66cc39e

5 files changed

Lines changed: 40 additions & 15 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,8 @@ For example compose file, see [docker-compose.yml](docker-compose.yml).
9999
```bash
100100
docker compose up -d
101101
```
102+
103+
## Stargazers over time
104+
105+
[![Stargazers over time](https://starchart.cc/LeslieLeung/reaper.svg)](https://starchart.cc/LeslieLeung/reaper)
106+

README_zh.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,7 @@ docker run --rm \
100100
docker compose up -d
101101
```
102102

103+
104+
## Stargazers over time
105+
106+
[![Stargazers over time](https://starchart.cc/LeslieLeung/reaper.svg)](https://starchart.cc/LeslieLeung/reaper)

cmd/rip/rip.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,7 @@ func runRip(cmd *cobra.Command, args []string) {
4848
ui.ErrorfExit("Storage %s not found in config", repo.Storage)
4949
}
5050

51-
rip.Rip(repo, storages)
51+
if err := rip.Rip(repo, storages); err != nil {
52+
ui.ErrorfExit("Error running %s, %s", repo.Name, err)
53+
}
5254
}

cmd/run/run.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ func runRun(cmd *cobra.Command, args []string) {
2525
storages := make([]config.Storage, 0)
2626
for _, storage := range repo.Storage {
2727
if s, ok := storageMap[storage]; !ok {
28-
ui.ErrorfExit("Storage %s not found in config", storage)
28+
ui.Errorf("Storage %s not found in config", storage)
29+
continue
2930
} else {
3031
storages = append(storages, s)
3132
}
3233
}
3334
ui.Printf("Running %s", repo.Name)
34-
rip.Rip(repo, storages)
35+
if err := rip.Rip(repo, storages); err != nil {
36+
ui.Errorf("Error running %s, %s", repo.Name, err)
37+
// move on to next repo
38+
}
3539
}
3640
}

internal/rip/rip.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@ import (
1414
"time"
1515
)
1616

17-
func Rip(repo config.Repository, storages []config.Storage) {
17+
func Rip(repo config.Repository, storages []config.Storage) error {
1818
id := uuid.New().String()
1919
// get current directory
2020
currentDir, _ := os.Getwd()
2121
// create a working directory
2222
err := os.MkdirAll(path.Join(currentDir, ".reaper", id), 0774)
2323
if err != nil {
24-
ui.ErrorfExit("Error creating working directory, %s", err)
24+
ui.Errorf("Error creating working directory, %s", err)
25+
return err
2526
}
2627
err = os.Chmod(path.Join(currentDir, ".reaper", id), 0774)
2728
if err != nil {
28-
ui.ErrorfExit("Error changing permission of working directory, %s", err)
29+
ui.Errorf("Error changing permission of working directory, %s", err)
30+
return err
2931
}
3032

3133
// clone the repo
@@ -35,36 +37,40 @@ func Rip(repo config.Repository, storages []config.Storage) {
3537
})
3638

3739
if err != nil {
38-
ui.ErrorfExit("Error cloning repository, %s", err)
40+
ui.Errorf("Error cloning repository, %s", err)
41+
return err
3942
}
4043

4144
ui.Printf("Repository %s cloned", repo.Name)
4245
files, err := archiver.FilesFromDisk(nil, map[string]string{
4346
path.Join(currentDir, ".reaper", id): repo.Name,
44-
// path.Join(currentDir, ".reaper", id): "repo.Name",
4547
// TODO add file hash
4648
})
4749
if err != nil {
48-
ui.ErrorfExit("Error reading files, %s", err)
50+
ui.Errorf("Error reading files, %s", err)
51+
return err
4952
}
5053

5154
now := time.Now().Format("20060102150405")
5255
base := repo.Name + "-" + now + ".tar.gz"
5356
p := path.Join(currentDir, ".reaper", base)
5457
out, err := os.Create(p)
5558
if err != nil {
56-
ui.ErrorfExit("Error creating archive, %s", err)
59+
ui.Errorf("Error creating archive, %s", err)
60+
return err
5761
}
5862
format := archiver.CompressedArchive{
5963
Compression: archiver.Gz{},
6064
Archival: archiver.Tar{},
6165
}
6266
err = format.Archive(context.Background(), out, files)
6367
if err != nil {
64-
ui.ErrorfExit("Error creating archive, %s", err)
68+
ui.Errorf("Error creating archive, %s", err)
69+
return err
6570
}
6671
if err := out.Close(); err != nil {
67-
ui.ErrorfExit("Error closing archive, %s", err)
72+
ui.Errorf("Error closing archive, %s", err)
73+
return err
6874
}
6975

7076
// handle storages
@@ -74,7 +80,8 @@ func Rip(repo config.Repository, storages []config.Storage) {
7480
fileBackend := storage.File{}
7581
err := fileBackend.PutObjectFromPath(p, path.Join(s.Path, base))
7682
if err != nil {
77-
ui.ErrorfExit("Error storing file, %s", err)
83+
ui.Errorf("Error storing file, %s", err)
84+
return err
7885
}
7986
ui.Printf("File %s stored", path.Join(s.Path, base))
8087
}
@@ -83,10 +90,13 @@ func Rip(repo config.Repository, storages []config.Storage) {
8390
// cleanup
8491
err = os.RemoveAll(path.Join(currentDir, ".reaper", id))
8592
if err != nil {
86-
ui.ErrorfExit("Error cleaning up working directory, %s", err)
93+
ui.Errorf("Error cleaning up working directory, %s", err)
94+
return err
8795
}
8896
err = os.Remove(p)
8997
if err != nil {
90-
ui.ErrorfExit("Error cleaning up archive, %s", err)
98+
ui.Errorf("Error cleaning up archive, %s", err)
99+
return err
91100
}
101+
return nil
92102
}

0 commit comments

Comments
 (0)