2016-02-13 01:18:06 -08:00
|
|
|
package vain
|
|
|
|
|
|
|
|
import (
|
2016-02-14 22:19:41 -08:00
|
|
|
"encoding/json"
|
2016-02-15 01:10:14 -08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-02-14 22:19:41 -08:00
|
|
|
"os"
|
2016-02-13 10:44:41 -08:00
|
|
|
"strings"
|
2016-02-13 01:18:06 -08:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2016-02-15 01:10:14 -08:00
|
|
|
// Valid checks that p will not confuse the go tool if added to packages.
|
2016-02-13 10:44:41 -08:00
|
|
|
func Valid(p string, packages []Package) bool {
|
|
|
|
for _, pkg := range packages {
|
2016-02-15 01:10:14 -08:00
|
|
|
if strings.HasPrefix(pkg.path, p) {
|
2016-02-13 10:44:41 -08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-02-15 01:10:14 -08:00
|
|
|
// Storage is a shim to allow for alternate storage types.
|
|
|
|
type Storage interface {
|
2016-03-01 23:34:31 -08:00
|
|
|
Contains(name string) bool
|
2016-02-15 01:10:14 -08:00
|
|
|
Add(p Package) error
|
|
|
|
Remove(path string) error
|
|
|
|
All() []Package
|
|
|
|
}
|
|
|
|
|
|
|
|
// SimpleStore implements a simple json on-disk storage.
|
|
|
|
type SimpleStore struct {
|
2016-02-13 01:18:06 -08:00
|
|
|
l sync.RWMutex
|
|
|
|
p map[string]Package
|
2016-02-14 22:19:41 -08:00
|
|
|
|
|
|
|
dbl sync.Mutex
|
|
|
|
path string
|
2016-02-13 01:18:06 -08:00
|
|
|
}
|
|
|
|
|
2016-02-15 01:38:11 -08:00
|
|
|
// NewSimpleStore returns a ready-to-use SimpleStore storing json at path.
|
|
|
|
func NewSimpleStore(path string) *SimpleStore {
|
2016-02-15 01:10:14 -08:00
|
|
|
return &SimpleStore{
|
2016-02-14 22:19:41 -08:00
|
|
|
path: path,
|
|
|
|
p: make(map[string]Package),
|
2016-02-13 01:18:06 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-01 23:34:31 -08:00
|
|
|
func (ms *SimpleStore) Contains(name string) bool {
|
|
|
|
_, contains := ms.p[name]
|
|
|
|
return contains
|
|
|
|
}
|
|
|
|
|
2016-02-15 01:10:14 -08:00
|
|
|
// Add adds p to the SimpleStore.
|
2016-03-01 23:41:44 -08:00
|
|
|
func (ss *SimpleStore) Add(p Package) error {
|
|
|
|
ss.l.Lock()
|
|
|
|
ss.p[p.path] = p
|
|
|
|
ss.l.Unlock()
|
2016-02-15 01:10:14 -08:00
|
|
|
m := ""
|
2016-03-01 23:41:44 -08:00
|
|
|
if err := ss.Save(); err != nil {
|
2016-02-15 01:10:14 -08:00
|
|
|
m = fmt.Sprintf("unable to store db: %v", err)
|
2016-03-01 23:41:44 -08:00
|
|
|
if err := ss.Remove(p.path); err != nil {
|
2016-02-15 01:10:14 -08:00
|
|
|
m = fmt.Sprintf("%s\nto add insult to injury, could not delete package: %v\n", m, err)
|
|
|
|
}
|
|
|
|
return errors.New(m)
|
|
|
|
}
|
2016-02-13 01:18:06 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-15 01:10:14 -08:00
|
|
|
// Remove removes p from the SimpleStore.
|
2016-03-01 23:41:44 -08:00
|
|
|
func (ss *SimpleStore) Remove(path string) error {
|
|
|
|
ss.l.Lock()
|
|
|
|
delete(ss.p, path)
|
|
|
|
ss.l.Unlock()
|
2016-02-13 01:18:06 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-15 01:10:14 -08:00
|
|
|
// All returns all current packages.
|
2016-03-01 23:41:44 -08:00
|
|
|
func (ss *SimpleStore) All() []Package {
|
2016-02-15 01:10:14 -08:00
|
|
|
r := []Package{}
|
2016-03-01 23:41:44 -08:00
|
|
|
ss.l.RLock()
|
|
|
|
for _, p := range ss.p {
|
2016-02-15 01:10:14 -08:00
|
|
|
r = append(r, p)
|
|
|
|
}
|
2016-03-01 23:41:44 -08:00
|
|
|
ss.l.RUnlock()
|
2016-02-15 01:10:14 -08:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save writes the db to disk.
|
2016-03-01 23:41:44 -08:00
|
|
|
func (ss *SimpleStore) Save() error {
|
2016-02-14 22:19:41 -08:00
|
|
|
// running in-memory only
|
2016-03-01 23:41:44 -08:00
|
|
|
if ss.path == "" {
|
2016-02-14 22:19:41 -08:00
|
|
|
return nil
|
|
|
|
}
|
2016-03-01 23:41:44 -08:00
|
|
|
ss.dbl.Lock()
|
|
|
|
defer ss.dbl.Unlock()
|
|
|
|
f, err := os.Create(ss.path)
|
2016-02-14 22:19:41 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2016-03-01 23:41:44 -08:00
|
|
|
return json.NewEncoder(f).Encode(ss.p)
|
2016-02-14 22:19:41 -08:00
|
|
|
}
|
|
|
|
|
2016-03-01 23:41:44 -08:00
|
|
|
// Load reads the db from disk and populates ss.
|
|
|
|
func (ss *SimpleStore) Load() error {
|
2016-02-14 22:19:41 -08:00
|
|
|
// running in-memory only
|
2016-03-01 23:41:44 -08:00
|
|
|
if ss.path == "" {
|
2016-02-14 22:19:41 -08:00
|
|
|
return nil
|
|
|
|
}
|
2016-03-01 23:41:44 -08:00
|
|
|
ss.dbl.Lock()
|
|
|
|
defer ss.dbl.Unlock()
|
|
|
|
f, err := os.Open(ss.path)
|
2016-02-14 22:19:41 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2016-02-23 22:09:29 -08:00
|
|
|
|
|
|
|
in := map[string]Package{}
|
|
|
|
if err := json.NewDecoder(f).Decode(&in); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for k, v := range in {
|
|
|
|
v.path = k
|
2016-03-01 23:41:44 -08:00
|
|
|
ss.p[k] = v
|
2016-02-23 22:09:29 -08:00
|
|
|
}
|
|
|
|
return nil
|
2016-02-13 01:18:06 -08:00
|
|
|
}
|