forked from Hucaru/Valhalla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
52 lines (42 loc) · 899 Bytes
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package script
import (
"fmt"
"path/filepath"
"strings"
"sync"
)
type scriptStore struct {
scripts map[string]scriptFile
mutex *sync.RWMutex
}
var loadedScripts = scriptStore{
scripts: make(map[string]scriptFile),
mutex: &sync.RWMutex{},
}
func Get(name string) (string, error) {
return loadedScripts.get(name)
}
func (ss *scriptStore) add(s scriptFile) {
name := filepath.Base(s.name)
name = strings.TrimSuffix(name, filepath.Ext(name))
ss.mutex.Lock()
ss.scripts[name] = s
ss.mutex.Unlock()
}
func (ss *scriptStore) remove(name string) {
ss.mutex.Lock()
delete(ss.scripts, name)
ss.mutex.Unlock()
}
func (ss *scriptStore) get(name string) (string, error) {
s := ""
var err error
ss.mutex.RLock()
if v, ok := ss.scripts[name]; ok {
s = v.contents
} else {
err = fmt.Errorf("Error in getting script with name " + name)
}
ss.mutex.RUnlock()
return s, err
}