forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcs.go
173 lines (136 loc) · 4.75 KB
/
rcs.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package action
import (
"context"
"errors"
"fmt"
"os"
"github.com/fatih/color"
"github.com/gopasspw/gopass/internal/action/exit"
"github.com/gopasspw/gopass/internal/backend"
"github.com/gopasspw/gopass/internal/cui"
"github.com/gopasspw/gopass/internal/out"
si "github.com/gopasspw/gopass/internal/store"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/debug"
"github.com/gopasspw/gopass/pkg/termio"
"github.com/urfave/cli/v2"
)
// RCSInit initializes a git repo including basic configuration.
func (s *Action) RCSInit(c *cli.Context) error {
ctx := ctxutil.WithGlobalFlags(c)
store := c.String("store")
un := termio.DetectName(c.Context, c)
ue := termio.DetectEmail(c.Context, c)
ctx = backend.WithStorageBackendString(ctx, c.String("storage"))
// default to git.
if !backend.HasStorageBackend(ctx) {
ctx = backend.WithStorageBackend(ctx, backend.GitFS)
}
if err := s.rcsInit(ctx, store, un, ue); err != nil {
return exit.Error(exit.Git, err, "failed to initialize %s: %s", backend.StorageBackendName(backend.GetStorageBackend(ctx)), err)
}
return nil
}
func (s *Action) rcsInit(ctx context.Context, store, un, ue string) error {
be := backend.GetStorageBackend(ctx)
// TODO this should rather ask s.Store if it HasRCSInit or something.
if be == backend.FS {
return nil
}
bn := backend.StorageBackendName(be)
userName, userEmail := s.getUserData(ctx, store, un, ue)
if err := s.Store.RCSInit(ctx, store, userName, userEmail); err != nil {
if errors.Is(err, backend.ErrNotSupported) {
debug.Log("RCSInit not supported for backend %s in %q", bn, store)
return nil
}
if gtv := os.Getenv("GPG_TTY"); gtv == "" {
out.Printf(ctx, "Git initialization failed. You may want to try to 'export GPG_TTY=$(tty)' and start over.")
}
return fmt.Errorf("failed to run RCS init: %w", err)
}
out.Printf(ctx, "Initialized %s repository (%s) for %s / %s...", be, bn, un, ue)
return nil
}
func (s *Action) getUserData(ctx context.Context, store, name, email string) (string, string) {
if name != "" && email != "" {
debug.Log("Username: %s, Email: %s (provided)", name, email)
return name, email
}
// 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, _ := cui.AskForGitConfigUser(ctx, s.Store.Crypto(ctx, store))
if name == "" {
if userName == "" {
userName = termio.DetectName(ctx, nil)
}
var err error
name, err = termio.AskForString(ctx, color.CyanString("Please enter a user name for password store git config"), userName)
if err != nil {
out.Errorf(ctx, "Failed to ask for user input: %s", err)
}
}
if email == "" {
if userEmail == "" {
userEmail = termio.DetectEmail(ctx, nil)
}
var err error
email, err = termio.AskForString(ctx, color.CyanString("Please enter an email address for password store git config"), userEmail)
if err != nil {
out.Errorf(ctx, "Failed to ask for user input: %s", err)
}
}
debug.Log("Username: %s, Email: %s (detected)", name, email)
return name, email
}
// RCSAddRemote adds a new git remote.
func (s *Action) RCSAddRemote(c *cli.Context) error {
ctx := ctxutil.WithGlobalFlags(c)
store := c.String("store")
remote := c.Args().Get(0)
url := c.Args().Get(1)
if remote == "" || url == "" {
return exit.Error(exit.Usage, nil, "Usage: %s git remote add <REMOTE> <URL>", s.Name)
}
return s.Store.RCSAddRemote(ctx, store, remote, url)
}
// RCSRemoveRemote removes a git remote.
func (s *Action) RCSRemoveRemote(c *cli.Context) error {
ctx := ctxutil.WithGlobalFlags(c)
store := c.String("store")
remote := c.Args().Get(0)
if remote == "" {
return exit.Error(exit.Usage, nil, "Usage: %s git remote rm <REMOTE>", s.Name)
}
return s.Store.RCSRemoveRemote(ctx, store, remote)
}
// RCSPull pulls from a git remote.
func (s *Action) RCSPull(c *cli.Context) error {
ctx := ctxutil.WithGlobalFlags(c)
store := c.String("store")
origin := c.Args().Get(0)
branch := c.Args().Get(1)
return s.Store.RCSPull(ctx, store, origin, branch)
}
// RCSPush pushes to a git remote.
func (s *Action) RCSPush(c *cli.Context) error {
ctx := ctxutil.WithGlobalFlags(c)
store := c.String("store")
origin := c.Args().Get(0)
branch := c.Args().Get(1)
if err := s.Store.RCSPush(ctx, store, origin, branch); err != nil {
if errors.Is(err, si.ErrGitNoRemote) {
out.Noticef(ctx, "No Git remote. Not pushing")
return nil
}
return exit.Error(exit.Git, err, "Failed to push to remote")
}
out.OKf(ctx, "Pushed to git remote")
return nil
}
// RCSStatus prints the rcs status.
func (s *Action) RCSStatus(c *cli.Context) error {
ctx := ctxutil.WithGlobalFlags(c)
store := c.String("store")
return s.Store.RCSStatus(ctx, store)
}