initial commit
This commit is contained in:
49
pkg/store/disk.go
Normal file
49
pkg/store/disk.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/sixt/gomodproxy/pkg/vcs"
|
||||
)
|
||||
|
||||
type disk string
|
||||
|
||||
func Disk(dir string) Store { return disk(dir) }
|
||||
|
||||
func (d disk) Put(ctx context.Context, snapshot Snapshot) error {
|
||||
dir := string(d)
|
||||
timeFile := filepath.Join(dir, snapshot.Key()+".time")
|
||||
zipFile := filepath.Join(dir, snapshot.Key()+".zip")
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(timeFile), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t, err := snapshot.Timestamp.MarshalText()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ioutil.WriteFile(timeFile, t, 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
return ioutil.WriteFile(zipFile, snapshot.Data, 0644)
|
||||
}
|
||||
|
||||
func (d disk) Get(ctx context.Context, module string, version vcs.Version) (Snapshot, error) {
|
||||
dir := string(d)
|
||||
s := Snapshot{Module: module, Version: version}
|
||||
t, err := ioutil.ReadFile(filepath.Join(dir, s.Key()+".time"))
|
||||
if err != nil {
|
||||
return Snapshot{}, err
|
||||
}
|
||||
if err := s.Timestamp.UnmarshalText(t); err != nil {
|
||||
return Snapshot{}, err
|
||||
}
|
||||
s.Data, err = ioutil.ReadFile(filepath.Join(dir, s.Key()+".zip"))
|
||||
return s, err
|
||||
}
|
||||
|
||||
func (d disk) Close() error { return nil }
|
||||
Reference in New Issue
Block a user