forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
72 lines (61 loc) · 2.07 KB
/
git.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 (
"context"
"github.com/fatih/color"
"github.com/justwatchcom/gopass/utils/out"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
// Git runs git commands inside the store or mounts
func (s *Action) Git(ctx context.Context, c *cli.Context) error {
store := c.String("store")
recurse := true
if c.IsSet("no-recurse") {
recurse = !c.Bool("no-recurse")
}
force := c.Bool("force")
if err := s.Store.Git(ctx, store, recurse, force, c.Args()...); err != nil {
return s.exitError(ctx, ExitGit, err, "git operation failed: %s", err)
}
return nil
}
// GitInit initializes a git repo including basic configuration
func (s *Action) GitInit(ctx context.Context, c *cli.Context) error {
store := c.String("store")
sk := c.String("sign-key")
if err := s.gitInit(ctx, store, sk); err != nil {
return s.exitError(ctx, ExitGit, err, "failed to initialize git: %s", err)
}
return nil
}
func (s *Action) gitInit(ctx context.Context, store, sk string) error {
out.Green(ctx, "Initializing git repository ...")
if sk == "" {
s, err := s.askForPrivateKey(ctx, color.CyanString("Please select a key for signing Git Commits"))
if err == nil {
sk = s
}
}
// for convenience, set defaults to user-selected values from available private keys
// NB: discarding returned error since this is merely a best-effort look-up for convenience
userName, userEmail, _ := s.askForGitConfigUser(ctx)
if userName == "" {
var err error
userName, err = s.askForString(ctx, color.CyanString("Please enter a user name for password store git config"), userName)
if err != nil {
return errors.Wrapf(err, "failed to ask for user input")
}
}
if userEmail == "" {
var err error
userEmail, err = s.askForString(ctx, color.CyanString("Please enter an email address for password store git config"), userEmail)
if err != nil {
return errors.Wrapf(err, "failed to ask for user input")
}
}
if err := s.Store.GitInit(ctx, store, sk, userName, userEmail); err != nil {
return errors.Wrapf(err, "failed to run git init")
}
out.Green(ctx, "Git initialized")
return nil
}