|
|
|
package mock
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
"path"
|
|
|
|
"os"
|
|
|
|
// "io"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
"caj-larsson/bog/domain"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type MockBogFile struct {
|
|
|
|
filename string
|
|
|
|
file afero.File
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f MockBogFile) Path() string {
|
|
|
|
return f.filename
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f MockBogFile) Size() int64 {
|
|
|
|
stat, _ := f.file.Stat()
|
|
|
|
return int64(stat.Size())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f MockBogFile) Read(p []byte) (int, error) {
|
|
|
|
return f.file.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f MockBogFile) Write(p []byte) (int, error) {
|
|
|
|
return f.file.Write(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f MockBogFile) Close() error {
|
|
|
|
return f.file.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f MockBogFile) Seek(offset int64, whence int) (int64, error) {
|
|
|
|
return f.file.Seek(offset, whence)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f MockBogFile) Modified() time.Time {
|
|
|
|
stat, _ := f.file.Stat()
|
|
|
|
return stat.ModTime()
|
|
|
|
}
|
|
|
|
|
|
|
|
// The actual repository
|
|
|
|
type MockFileRepository struct {
|
|
|
|
fs afero.Fs
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMockFileRepository() domain.FileDataRepository {
|
|
|
|
return MockFileRepository { afero.NewMemMapFs() }
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r MockFileRepository) Create(filename string, user_agent_label string) (domain.BogInFile, error) {
|
|
|
|
abs_path := path.Join( filename, user_agent_label)
|
|
|
|
dir := path.Dir(abs_path)
|
|
|
|
r.fs.MkdirAll(dir, 0750)
|
|
|
|
file, err := r.fs.OpenFile(abs_path, os.O_RDWR|os.O_CREATE, 0644)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bf := MockBogFile {filename, file}
|
|
|
|
|
|
|
|
return bf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r MockFileRepository) Open(filename string, user_agent_label string) (domain.BogOutFile, error) {
|
|
|
|
abs_path := path.Join(filename, user_agent_label)
|
|
|
|
|
|
|
|
dir := path.Dir(abs_path)
|
|
|
|
r.fs.MkdirAll(dir, 0750)
|
|
|
|
file, err := r.fs.OpenFile(abs_path, os.O_RDONLY, 0644)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, domain.ErrNotExists
|
|
|
|
}
|
|
|
|
|
|
|
|
bf := MockBogFile {filename, file}
|
|
|
|
return bf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r MockFileRepository) Delete(filename string, user_agent_label string) {
|
|
|
|
abs_path := path.Join(filename, user_agent_label)
|
|
|
|
r.fs.Remove(abs_path)
|
|
|
|
}
|