forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clone.go
72 lines (57 loc) · 1.66 KB
/
clone.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
package action
import (
"fmt"
"os"
"os/exec"
"github.com/justwatchcom/gopass/fsutil"
"github.com/urfave/cli"
)
// Clone will fetch and mount a new password store from a git repo
func (s *Action) Clone(c *cli.Context) error {
if len(c.Args()) < 1 {
return fmt.Errorf("Usage: gopass clone repo [mount]")
}
repo := c.Args()[0]
mount := ""
if len(c.Args()) > 1 {
mount = c.Args()[1]
}
path := c.String("path")
if path == "" {
path = pwStoreDir(mount)
}
if mount == "" && s.Store.Initialized() {
return fmt.Errorf("Can not clone %s to the root store, as this store is already initialized. Please try cloning to a submount: `gopass clone %s sub`", repo, repo)
}
// clone repo
if err := gitClone(repo, path); err != nil {
return err
}
// add mount
if mount != "" {
if !s.Store.Initialized() {
return fmt.Errorf("Root-Store is not initialized. Clone or init root store first")
}
if err := s.Store.AddMount(mount, path); err != nil {
return fmt.Errorf("Failed to add mount: %s", err)
}
fmt.Printf("Mounted password store %s at mount point `%s` ...\n", path, mount)
}
// save new mount in config file
if err := writeConfig(s.Store); err != nil {
return fmt.Errorf("Failed to update config: %s", err)
}
fmt.Printf("Your password store is ready to use! Has a look around: `gopass %s`\n", mount)
return nil
}
func gitClone(repo, path string) error {
if fsutil.IsDir(path) {
return fmt.Errorf("%s is a directory that already exists", path)
}
fmt.Printf("Cloning repository %s to %s ...\n", repo, path)
cmd := exec.Command("git", "clone", repo, path)
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
return cmd.Run()
}