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 }
|
||||
41
pkg/store/mem.go
Normal file
41
pkg/store/mem.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
|
||||
"github.com/sixt/gomodproxy/pkg/vcs"
|
||||
)
|
||||
|
||||
type memory struct {
|
||||
sync.Mutex
|
||||
cache []Snapshot
|
||||
}
|
||||
|
||||
func Memory() Store { return &memory{} }
|
||||
|
||||
func (m *memory) Put(ctx context.Context, snapshot Snapshot) error {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
for _, item := range m.cache {
|
||||
if item.Module == snapshot.Module && item.Version == snapshot.Version {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
m.cache = append(m.cache, snapshot)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *memory) Get(ctx context.Context, module string, version vcs.Version) (Snapshot, error) {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
for _, snapshot := range m.cache {
|
||||
if snapshot.Module == module && snapshot.Version == version {
|
||||
return snapshot, nil
|
||||
}
|
||||
}
|
||||
return Snapshot{}, errors.New("not found")
|
||||
}
|
||||
|
||||
func (m *memory) Close() error { return nil }
|
||||
25
pkg/store/store.go
Normal file
25
pkg/store/store.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/sixt/gomodproxy/pkg/vcs"
|
||||
)
|
||||
|
||||
type Store interface {
|
||||
Put(ctx context.Context, snapshot Snapshot) error
|
||||
Get(ctx context.Context, module string, version vcs.Version) (Snapshot, error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
type Snapshot struct {
|
||||
Module string
|
||||
Version vcs.Version
|
||||
Timestamp time.Time
|
||||
Data []byte
|
||||
}
|
||||
|
||||
func (s Snapshot) Key() string {
|
||||
return s.Module + "@" + string(s.Version)
|
||||
}
|
||||
Reference in New Issue
Block a user