forked from gopasspw/gopass
/
store.go
94 lines (80 loc) · 2.22 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
94
package root
import (
"context"
"fmt"
"strings"
"github.com/justwatchcom/gopass/backend/gpg"
gpgcli "github.com/justwatchcom/gopass/backend/gpg/cli"
"github.com/justwatchcom/gopass/config"
"github.com/justwatchcom/gopass/store/sub"
"github.com/justwatchcom/gopass/utils/fsutil"
"github.com/pkg/errors"
)
type gpger interface {
FindPublicKeys(context.Context, ...string) (gpg.KeyList, error)
}
// Store is the public facing password store
type Store struct {
cfg *config.Config
gpg gpger
mounts map[string]*sub.Store
path string // path to the root store
store *sub.Store
version string
}
// New creates a new store
func New(ctx context.Context, cfg *config.Config) (*Store, error) {
if cfg == nil {
cfg = &config.Config{}
}
if cfg.Root.Path == "" {
return nil, errors.Errorf("need path")
}
r := &Store{
cfg: cfg,
gpg: gpgcli.New(gpgcli.Config{}),
mounts: make(map[string]*sub.Store, len(cfg.Mounts)),
path: cfg.Root.Path,
version: cfg.Version,
}
// create the base store
r.store = sub.New("", r.Path())
// initialize all mounts
for alias, sc := range cfg.Mounts {
path := fsutil.CleanPath(sc.Path)
if err := r.addMount(ctx, alias, path); err != nil {
fmt.Printf("Failed to initialized mount %s (%s): %s. Ignoring\n", alias, path, err)
continue
}
}
// check for duplicate mounts
if err := r.checkMounts(); err != nil {
return nil, errors.Errorf("checking mounts failed: %s", err)
}
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(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(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())
}
return fmt.Sprintf("Store(Path: %s, Mounts: %+v)", r.store.Path(), strings.Join(ms, ","))
}
// Path returns the store path
func (r *Store) Path() string {
return r.path
}
// Alias always returns an empty string
func (r *Store) Alias() string {
return ""
}