forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mount.go
84 lines (76 loc) · 2.07 KB
/
mount.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
package action
import (
"fmt"
"sort"
"github.com/fatih/color"
"github.com/justwatchcom/gopass/config"
"github.com/justwatchcom/gopass/store"
"github.com/justwatchcom/gopass/tree/simple"
"github.com/urfave/cli"
)
// MountRemove removes an existing mount
func (s *Action) MountRemove(c *cli.Context) error {
if len(c.Args()) != 1 {
return fmt.Errorf("usage: gopass mount remove [alias]")
}
if err := s.Store.RemoveMount(c.Args()[0]); err != nil {
color.Yellow("Failed to remove mount: %s", err)
}
if err := s.Store.Config().Save(); err != nil {
return err
}
color.Green("Password Store %s umounted", c.Args()[0])
return nil
}
// MountsPrint prints all existing mounts
func (s *Action) MountsPrint(c *cli.Context) error {
if len(s.Store.Mounts()) < 1 {
fmt.Println("No mounts")
return nil
}
root := simple.New(color.GreenString(fmt.Sprintf("gopass (%s)", s.Store.Path())))
mounts := s.Store.Mounts()
mps := s.Store.MountPoints()
sort.Sort(store.ByPathLen(mps))
for _, alias := range mps {
path := mounts[alias]
if err := root.AddMount(alias, path); err != nil {
fmt.Printf("Failed to add mount: %s\n", err)
}
}
fmt.Fprintln(color.Output, root.Format(0))
return nil
}
// MountsComplete will print a list of existings mount points for bash
// completion
func (s *Action) MountsComplete(*cli.Context) {
for alias := range s.Store.Mounts() {
fmt.Println(alias)
}
}
// MountAdd adds a new mount
func (s *Action) MountAdd(c *cli.Context) error {
alias := c.Args().Get(0)
localPath := c.Args().Get(1)
if alias == "" {
return fmt.Errorf("usage: gopass mount add <alias> [local path]")
}
if localPath == "" {
localPath = config.PwStoreDir(alias)
}
keys := make([]string, 0, 1)
if k := c.String("init"); k != "" {
keys = append(keys, k)
}
if s.Store.Exists(alias) {
fmt.Printf(color.YellowString("WARNING: shadowing %s entry\n"), alias)
}
if err := s.Store.AddMount(alias, localPath, keys...); err != nil {
return err
}
if err := s.Store.Config().Save(); err != nil {
return err
}
color.Green("Mounted %s as %s", alias, localPath)
return nil
}