-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathvault.go
76 lines (63 loc) · 1.38 KB
/
vault.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package opvault
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
)
// Vault Errors
var (
ErrVaultMustBeDir = errors.New("vault must be a directory")
ErrInvalidProfile = errors.New("invalid profile")
)
type Vault struct {
dir string
}
func Open(dir string) (*Vault, error) {
dirStat, err := os.Stat(dir)
if err != nil {
return nil, err.(*os.PathError).Err
}
if !dirStat.IsDir() {
return nil, ErrVaultMustBeDir
}
return &Vault{dir}, nil
}
func (v *Vault) ProfileNames() ([]string, error) {
entries, err := ioutil.ReadDir(v.dir)
if err != nil {
return nil, err
}
profiles := []string{}
for _, entry := range entries {
if entry.IsDir() {
profileStat, err := os.Stat(filepath.Join(v.dir, entry.Name(), "profile.js"))
if err == nil && profileStat.Mode().IsRegular() {
profiles = append(profiles, entry.Name())
}
}
}
return profiles, nil
}
func (v *Vault) Profile(profile string) (*Profile, error) {
profileStat, err := os.Stat(filepath.Join(v.dir, profile, "profile.js"))
if err != nil {
if err.(*os.PathError).Err == os.ErrNotExist {
return nil, ErrInvalidProfile
}
return nil, err.(*os.PathError).Err
}
if !profileStat.Mode().IsRegular() {
return nil, ErrInvalidProfile
}
p := &Profile{
vault: v,
profile: profile,
data: make(map[string]interface{}),
}
err = p.readData()
if err != nil {
return nil, err
}
return p, nil
}