Skip to content

Commit

Permalink
refactor: modify storage interface
Browse files Browse the repository at this point in the history
  • Loading branch information
LeslieLeung committed Nov 7, 2023
1 parent 66cc39e commit a0fdcf0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 41 deletions.
24 changes: 6 additions & 18 deletions internal/rip/rip.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rip

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

now := time.Now().Format("20060102150405")
base := repo.Name + "-" + now + ".tar.gz"
p := path.Join(currentDir, ".reaper", base)
out, err := os.Create(p)
if err != nil {
ui.Errorf("Error creating archive, %s", err)
return err
}
// TODO store to a temporary file first if greater than certain size
archive := &bytes.Buffer{}

format := archiver.CompressedArchive{
Compression: archiver.Gz{},
Archival: archiver.Tar{},
}
err = format.Archive(context.Background(), out, files)
err = format.Archive(context.Background(), archive, files)
if err != nil {
ui.Errorf("Error creating archive, %s", err)
return err
}
if err := out.Close(); err != nil {
ui.Errorf("Error closing archive, %s", err)
return err
}

// handle storages
for _, s := range storages {
switch s.Type {
case "file":
fileBackend := storage.File{}
err := fileBackend.PutObjectFromPath(p, path.Join(s.Path, base))
err := fileBackend.PutObject(path.Join(s.Path, base), archive.Bytes())
if err != nil {
ui.Errorf("Error storing file, %s", err)
return err
Expand All @@ -93,10 +86,5 @@ func Rip(repo config.Repository, storages []config.Storage) error {
ui.Errorf("Error cleaning up working directory, %s", err)
return err
}
err = os.Remove(p)
if err != nil {
ui.Errorf("Error cleaning up archive, %s", err)
return err
}
return nil
}
36 changes: 15 additions & 21 deletions internal/storage/file.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package storage

import (
"io"
"os"
"path"
)
Expand All @@ -11,6 +10,21 @@ var _ Storage = (*File)(nil)
type File struct {
}

func (f File) ListObject(prefix string) ([]Object, error) {
// TODO implement me
panic("implement me")
}

func (f File) GetObject(identifier string) (Object, error) {
// TODO implement me
panic("implement me")
}

func (f File) DeleteObject(identifier string) error {
// TODO implement me
panic("implement me")
}

// createPathIfNotExist creates the directory for the given file path if it does not exist.
func createPathIfNotExist(filePath string) error {
dir := path.Dir(filePath)
Expand Down Expand Up @@ -39,23 +53,3 @@ func (f File) PutObject(identifier string, data []byte) error {
}
return os.WriteFile(identifier, data, 0664)
}

func (f File) PutObjectFromPath(path string, identifier string) error {
source, err := os.Open(path)
if err != nil {
return err
}
defer source.Close()

if err := createPathIfNotExist(identifier); err != nil {
return err
}
destination, err := os.Create(identifier)
if err != nil {
return err
}
defer destination.Close()

_, err = io.Copy(destination, source)
return err
}
16 changes: 14 additions & 2 deletions internal/storage/storage.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
package storage

import "time"

type Object struct {
Path string
Content []byte
LastModified time.Time
}

type Storage interface {
// ListObject returns a list of all objects in the storage backend.
ListObject(prefix string) ([]Object, error)
// GetObject returns the object identified by the given identifier.
GetObject(identifier string) (Object, error)
// PutObject stores the data in the storage backend identified by the given identifier.
PutObject(identifier string, data []byte) error
// PutObjectFromPath reads the file at the given path and stores the data in the storage backend identified by the given identifier.
PutObjectFromPath(path string, identifier string) error
// DeleteObject deletes the object identified by the given identifier.
DeleteObject(identifier string) error
}

0 comments on commit a0fdcf0

Please sign in to comment.