forked from gopasspw/gopass
/
store.go
93 lines (81 loc) · 2.09 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package root
import (
"context"
"fmt"
"strings"
"github.com/gopasspw/gopass/pkg/agent/client"
"github.com/gopasspw/gopass/pkg/backend"
"github.com/gopasspw/gopass/pkg/config"
"github.com/gopasspw/gopass/pkg/store"
"github.com/gopasspw/gopass/pkg/store/sub"
)
// Store is the public facing password store
type Store struct {
cfg *config.Config
mounts map[string]store.Store
url *backend.URL // url of the root store
store *sub.Store
agent *client.Client
}
// New creates a new store
func New(ctx context.Context, cfg *config.Config) (*Store, error) {
if cfg == nil {
cfg = &config.Config{}
}
if cfg.Root != nil && (cfg.Root.Path == nil || cfg.Root.Path.Path == "") {
cfg.Root.Path = backend.FromPath(config.PwStoreDir(""))
}
r := &Store{
cfg: cfg,
mounts: make(map[string]store.Store, len(cfg.Mounts)),
url: cfg.Root.Path,
}
return r, nil
}
// Exists checks the existence of a single entry
func (r *Store) Exists(ctx context.Context, name string) bool {
_, store, name := r.getStore(ctx, name)
return store.Exists(ctx, name)
}
// IsDir checks if a given key is actually a folder
func (r *Store) IsDir(ctx context.Context, name string) bool {
_, store, name := r.getStore(ctx, name)
return store.IsDir(ctx, name)
}
func (r *Store) String() string {
ms := make([]string, 0, len(r.mounts))
for alias, sub := range r.mounts {
ms = append(ms, alias+"="+sub.String())
}
path := ""
if r.store != nil {
path = r.store.Path()
}
return fmt.Sprintf("Store(Path: %s, Mounts: %+v)", path, strings.Join(ms, ","))
}
// Path returns the store path
func (r *Store) Path() string {
if r.url == nil {
return ""
}
return r.url.Path
}
// URL returns the store URL
func (r *Store) URL() string {
if r.url == nil {
return ""
}
return r.url.String()
}
// Alias always returns an empty string
func (r *Store) Alias() string {
return ""
}
// Storage returns the storage backend for the given mount point
func (r *Store) Storage(ctx context.Context, name string) backend.Storage {
_, sub, _ := r.getStore(ctx, name)
if sub == nil || !sub.Valid() {
return nil
}
return sub.Storage()
}