Skip to content

Commit a0fdcf0

Browse files
committed
refactor: modify storage interface
1 parent 66cc39e commit a0fdcf0

3 files changed

Lines changed: 35 additions & 41 deletions

File tree

internal/rip/rip.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package rip
22

33
import (
4+
"bytes"
45
"context"
56
"github.com/go-git/go-git/v5"
67
"github.com/google/uuid"
@@ -44,7 +45,6 @@ func Rip(repo config.Repository, storages []config.Storage) error {
4445
ui.Printf("Repository %s cloned", repo.Name)
4546
files, err := archiver.FilesFromDisk(nil, map[string]string{
4647
path.Join(currentDir, ".reaper", id): repo.Name,
47-
// TODO add file hash
4848
})
4949
if err != nil {
5050
ui.Errorf("Error reading files, %s", err)
@@ -53,32 +53,25 @@ func Rip(repo config.Repository, storages []config.Storage) error {
5353

5454
now := time.Now().Format("20060102150405")
5555
base := repo.Name + "-" + now + ".tar.gz"
56-
p := path.Join(currentDir, ".reaper", base)
57-
out, err := os.Create(p)
58-
if err != nil {
59-
ui.Errorf("Error creating archive, %s", err)
60-
return err
61-
}
56+
// TODO store to a temporary file first if greater than certain size
57+
archive := &bytes.Buffer{}
58+
6259
format := archiver.CompressedArchive{
6360
Compression: archiver.Gz{},
6461
Archival: archiver.Tar{},
6562
}
66-
err = format.Archive(context.Background(), out, files)
63+
err = format.Archive(context.Background(), archive, files)
6764
if err != nil {
6865
ui.Errorf("Error creating archive, %s", err)
6966
return err
7067
}
71-
if err := out.Close(); err != nil {
72-
ui.Errorf("Error closing archive, %s", err)
73-
return err
74-
}
7568

7669
// handle storages
7770
for _, s := range storages {
7871
switch s.Type {
7972
case "file":
8073
fileBackend := storage.File{}
81-
err := fileBackend.PutObjectFromPath(p, path.Join(s.Path, base))
74+
err := fileBackend.PutObject(path.Join(s.Path, base), archive.Bytes())
8275
if err != nil {
8376
ui.Errorf("Error storing file, %s", err)
8477
return err
@@ -93,10 +86,5 @@ func Rip(repo config.Repository, storages []config.Storage) error {
9386
ui.Errorf("Error cleaning up working directory, %s", err)
9487
return err
9588
}
96-
err = os.Remove(p)
97-
if err != nil {
98-
ui.Errorf("Error cleaning up archive, %s", err)
99-
return err
100-
}
10189
return nil
10290
}

internal/storage/file.go

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package storage
22

33
import (
4-
"io"
54
"os"
65
"path"
76
)
@@ -11,6 +10,21 @@ var _ Storage = (*File)(nil)
1110
type File struct {
1211
}
1312

13+
func (f File) ListObject(prefix string) ([]Object, error) {
14+
// TODO implement me
15+
panic("implement me")
16+
}
17+
18+
func (f File) GetObject(identifier string) (Object, error) {
19+
// TODO implement me
20+
panic("implement me")
21+
}
22+
23+
func (f File) DeleteObject(identifier string) error {
24+
// TODO implement me
25+
panic("implement me")
26+
}
27+
1428
// createPathIfNotExist creates the directory for the given file path if it does not exist.
1529
func createPathIfNotExist(filePath string) error {
1630
dir := path.Dir(filePath)
@@ -39,23 +53,3 @@ func (f File) PutObject(identifier string, data []byte) error {
3953
}
4054
return os.WriteFile(identifier, data, 0664)
4155
}
42-
43-
func (f File) PutObjectFromPath(path string, identifier string) error {
44-
source, err := os.Open(path)
45-
if err != nil {
46-
return err
47-
}
48-
defer source.Close()
49-
50-
if err := createPathIfNotExist(identifier); err != nil {
51-
return err
52-
}
53-
destination, err := os.Create(identifier)
54-
if err != nil {
55-
return err
56-
}
57-
defer destination.Close()
58-
59-
_, err = io.Copy(destination, source)
60-
return err
61-
}

internal/storage/storage.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
package storage
22

3+
import "time"
4+
5+
type Object struct {
6+
Path string
7+
Content []byte
8+
LastModified time.Time
9+
}
10+
311
type Storage interface {
12+
// ListObject returns a list of all objects in the storage backend.
13+
ListObject(prefix string) ([]Object, error)
14+
// GetObject returns the object identified by the given identifier.
15+
GetObject(identifier string) (Object, error)
416
// PutObject stores the data in the storage backend identified by the given identifier.
517
PutObject(identifier string, data []byte) error
6-
// PutObjectFromPath reads the file at the given path and stores the data in the storage backend identified by the given identifier.
7-
PutObjectFromPath(path string, identifier string) error
18+
// DeleteObject deletes the object identified by the given identifier.
19+
DeleteObject(identifier string) error
820
}

0 commit comments

Comments
 (0)