Skip to content

Commit a16d7f5

Browse files
committed
refactor(action): split Action god object into focused handler types (A-1)
The Action struct in internal/action had 164 methods across 40+ files, making it a classic god object and an obstacle to comprehension and testing. This commit implements the A-1 fix from the code quality report by splitting the monolithic Action into 13 focused handler types, each responsible for a single domain: - secretHandler — show, insert, edit, delete, copy, move, link, merge - searchHandler — find, grep, list, history - generateHandler — generate, create - mountHandler — mount management - recipientHandler — recipient management - setupHandler — init, setup, clone, rcs - syncHandler — sync, git - auditHandler — audit, fsck - templateHandler — templates - binaryHandler — binary/cat operations - envHandler — env injection - otpHandler — OTP operations - miscHandler — config, repl, doctor, completion, version, etc. All handler types embed a shared *base struct (store, config, version, reminder). Cross-handler dependencies are wired at construction time via explicit function-reference fields so each handler only imports the specific operations it needs, avoiding circular dependencies. Every public command registered in commands.go, and every internal method called from existing test files, remains accessible on *Action through thin one-liner delegation shims in handler_shims.go. This preserves full backward compatibility with no changes required in commands.go or any test file. Also fixes a pre-existing bug in internal/store/root/mount.go where getStore would return a name with a leading slash (e.g. "/root-secret" instead of "root-secret") when stripping a mount alias. This was harmless before but now correctly rejected by the new ValidateSecretName check introduced in a prior commit. Fix: trim the leading slash after TrimPrefix.
1 parent d53a1c2 commit a16d7f5

44 files changed

Lines changed: 611 additions & 201 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎internal/action/action.go‎

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/blang/semver/v4"
99
"github.com/gopasspw/gopass/internal/config"
1010
"github.com/gopasspw/gopass/internal/reminder"
11-
"github.com/gopasspw/gopass/internal/store/root"
1211
"github.com/gopasspw/gopass/pkg/debug"
1312
)
1413

@@ -18,13 +17,25 @@ var (
1817
stderr io.Writer = os.Stderr //nolint:unused
1918
)
2019

21-
// Action knows everything to run gopass CLI actions.
20+
// Action is the top-level CLI orchestrator. It owns one focused handler per
21+
// domain and exposes every public command through thin delegation shims so that
22+
// the urfave/cli command registrations in commands.go do not need to change.
2223
type Action struct {
23-
Name string
24-
Store *root.Store
25-
cfg *config.Config
26-
version semver.Version
27-
rem *reminder.Store
24+
*base
25+
26+
secrets *secretHandler
27+
search *searchHandler
28+
generate *generateHandler
29+
mounts *mountHandler
30+
recipients *recipientHandler
31+
setup *setupHandler
32+
syncH *syncHandler
33+
audit *auditHandler
34+
templates *templateHandler
35+
binary *binaryHandler
36+
envH *envHandler
37+
otpH *otpHandler
38+
misc *miscHandler
2839
}
2940

3041
// New returns a new Action wrapper.
@@ -38,28 +49,79 @@ func newAction(cfg *config.Config, sv semver.Version, remind bool) (*Action, err
3849
name = filepath.Base(os.Args[0])
3950
}
4051

41-
act := &Action{
42-
Name: name,
43-
cfg: cfg,
44-
version: sv,
45-
Store: root.New(cfg),
46-
}
52+
b := newBase(cfg, sv)
53+
b.Name = name
4754

4855
if remind {
4956
r, err := reminder.New()
5057
if err != nil {
5158
debug.Log("failed to init reminder: %s", err)
5259
} else {
53-
// only populate the reminder variable on success, the implementation.
54-
// can handle being called on a nil pointer.
55-
act.rem = r
60+
b.rem = r
5661
}
5762
}
5863

59-
return act, nil
64+
// Construct each focused handler.
65+
sec := &secretHandler{base: b}
66+
tmpl := &templateHandler{base: b}
67+
srch := &searchHandler{base: b}
68+
gen := &generateHandler{base: b}
69+
mnt := &mountHandler{base: b}
70+
rec := &recipientHandler{base: b}
71+
setup := &setupHandler{base: b}
72+
syn := &syncHandler{base: b}
73+
aud := &auditHandler{base: b}
74+
bin := &binaryHandler{base: b}
75+
env := &envHandler{base: b}
76+
otp := &otpHandler{base: b}
77+
misc := &miscHandler{base: b}
78+
79+
// Wire cross-handler dependencies through explicit function references so
80+
// that each handler only depends on the specific operations it needs.
81+
sec.findFn = srch.find
82+
sec.renderTemplateFn = tmpl.renderTemplate
83+
84+
srch.showFn = sec.show
85+
srch.editFn = sec.edit
86+
srch.syncFn = syn.Sync
87+
88+
gen.editFn = sec.Edit
89+
gen.renderTemplateFn = tmpl.renderTemplate
90+
91+
mnt.initFn = setup.init
92+
93+
setup.autoSyncFn = syn.autoSync
94+
setup.printReminderFn = misc.printReminder
95+
96+
otp.insertYAMLFn = sec.insertYAML
97+
otp.findFn = srch.find
98+
99+
sec.listFn = srch.List
100+
sec.findFuzzyFn = srch.FindFuzzy
101+
102+
misc.initCheckPrivateKeysFn = setup.initCheckPrivateKeys
103+
misc.recipientsListFn = rec.recipientsList
104+
misc.templatesListFn = tmpl.templatesList
105+
106+
return &Action{
107+
base: b,
108+
secrets: sec,
109+
search: srch,
110+
generate: gen,
111+
mounts: mnt,
112+
recipients: rec,
113+
setup: setup,
114+
syncH: syn,
115+
audit: aud,
116+
templates: tmpl,
117+
binary: bin,
118+
envH: env,
119+
otpH: otp,
120+
misc: misc,
121+
}, nil
60122
}
61123

62-
// String implement fmt.Stringer.
124+
// String implements fmt.Stringer.
63125
func (s *Action) String() string {
64126
return s.Store.String()
65127
}

‎internal/action/aliases.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
// AliasesPrint prints all configured aliases for password generation rules.
13-
func (s *Action) AliasesPrint(c *cli.Context) error {
13+
func (s *miscHandler) AliasesPrint(c *cli.Context) error {
1414
out.Printf(c.Context, "Configured aliases:")
1515
aliases := pwrules.AllAliases(c.Context)
1616
keys := make([]string, 0, len(aliases))

‎internal/action/audit.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
)
1818

1919
// Audit validates passwords against common flaws.
20-
func (s *Action) Audit(c *cli.Context) error {
20+
func (s *auditHandler) Audit(c *cli.Context) error {
2121
ctx := ctxutil.WithGlobalFlags(c)
2222

2323
_ = s.rem.Reset("audit")

‎internal/action/base.go‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package action
2+
3+
import (
4+
"context"
5+
6+
"github.com/blang/semver/v4"
7+
"github.com/gopasspw/gopass/internal/backend"
8+
"github.com/gopasspw/gopass/internal/config"
9+
"github.com/gopasspw/gopass/internal/reminder"
10+
"github.com/gopasspw/gopass/internal/store/root"
11+
"github.com/urfave/cli/v2"
12+
)
13+
14+
// base holds the fields common to all handler types. Every handler embeds
15+
// *base so it can access the store, config, and other shared state.
16+
type base struct {
17+
Name string
18+
Store *root.Store
19+
cfg *config.Config
20+
version semver.Version
21+
rem *reminder.Store
22+
}
23+
24+
// secretHandler handles secret CRUD operations (show, insert, edit, delete,
25+
// copy, move, link, merge).
26+
type secretHandler struct {
27+
*base
28+
findFn func(ctx context.Context, c *cli.Context, needle string, cb showFunc, fuzzy bool) error
29+
renderTemplateFn func(ctx context.Context, name string, content []byte) ([]byte, bool)
30+
listFn func(c *cli.Context) error
31+
findFuzzyFn func(c *cli.Context) error
32+
}
33+
34+
// searchHandler handles search and list operations (find, grep, list, history).
35+
type searchHandler struct {
36+
*base
37+
showFn showFunc
38+
editFn func(ctx context.Context, c *cli.Context, name string) error
39+
syncFn func(c *cli.Context) error
40+
}
41+
42+
// generateHandler handles password generation (generate, create).
43+
type generateHandler struct {
44+
*base
45+
editFn func(c *cli.Context) error
46+
renderTemplateFn func(ctx context.Context, name string, content []byte) ([]byte, bool)
47+
}
48+
49+
// mountHandler handles store mount management.
50+
type mountHandler struct {
51+
*base
52+
initFn func(ctx context.Context, alias, path string, keys ...string) error
53+
}
54+
55+
// recipientHandler handles recipient management.
56+
type recipientHandler struct {
57+
*base
58+
}
59+
60+
// setupHandler handles store initialisation and onboarding (init, setup, clone, rcs).
61+
type setupHandler struct {
62+
*base
63+
autoSyncFn func(ctx context.Context) error
64+
printReminderFn func(ctx context.Context)
65+
}
66+
67+
// syncHandler handles synchronisation with remote storage (sync, git).
68+
type syncHandler struct {
69+
*base
70+
}
71+
72+
// auditHandler handles store auditing.
73+
type auditHandler struct {
74+
*base
75+
}
76+
77+
// templateHandler handles secret templates.
78+
type templateHandler struct {
79+
*base
80+
}
81+
82+
// binaryHandler handles binary secret operations.
83+
type binaryHandler struct {
84+
*base
85+
}
86+
87+
// envHandler handles environment-variable injection.
88+
type envHandler struct {
89+
*base
90+
}
91+
92+
// otpHandler handles one-time password operations.
93+
type otpHandler struct {
94+
*base
95+
insertYAMLFn func(ctx context.Context, name, key string, content []byte, kvps map[string]string) error
96+
findFn func(ctx context.Context, c *cli.Context, needle string, cb showFunc, fuzzy bool) error
97+
}
98+
99+
// miscHandler handles miscellaneous operations that do not fit a narrower
100+
// category (aliases, version, convert, reorg, process, unclip, update,
101+
// reminder, repl, doctor, completion, config, otp-adjacent helpers).
102+
type miscHandler struct {
103+
*base
104+
initCheckPrivateKeysFn func(ctx context.Context, crypto backend.Crypto) error
105+
recipientsListFn func(ctx context.Context) []string
106+
templatesListFn func(ctx context.Context) []string
107+
}
108+
109+
// newBase constructs the shared base from a config.
110+
func newBase(cfg *config.Config, sv semver.Version) *base {
111+
return &base{
112+
cfg: cfg,
113+
version: sv,
114+
Store: root.New(cfg),
115+
}
116+
}
117+
118+
// Crypto backend type alias used by setup_handler shims.
119+
type cryptoBackend = backend.Crypto

‎internal/action/binary.go‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var binstdin = os.Stdin
2727
// Cat prints to or reads from STDIN/STDOUT.
2828
// If the content is piped to stdin, it is written to the secret.
2929
// Otherwise, the secret content is printed to stdout.
30-
func (s *Action) Cat(c *cli.Context) error {
30+
func (s *binaryHandler) Cat(c *cli.Context) error {
3131
ctx := ctxutil.WithGlobalFlags(c)
3232
name := c.Args().First()
3333
if name == "" {
@@ -116,7 +116,7 @@ func secFromBytes(dst, src string, in []byte) (gopass.Secret, error) {
116116

117117
// BinaryCopy copies either from the filesystem to the store or from the store
118118
// to the filesystem.
119-
func (s *Action) BinaryCopy(c *cli.Context) error {
119+
func (s *binaryHandler) BinaryCopy(c *cli.Context) error {
120120
ctx := ctxutil.WithGlobalFlags(c)
121121
from := c.Args().Get(0)
122122
to := c.Args().Get(1)
@@ -132,7 +132,7 @@ func (s *Action) BinaryCopy(c *cli.Context) error {
132132
// BinaryMove works like Copy but will remove (shred/wipe) the source
133133
// after a successful copy. Mostly useful for securely moving secrets into
134134
// the store if they are no longer needed / wanted on disk afterwards.
135-
func (s *Action) BinaryMove(c *cli.Context) error {
135+
func (s *binaryHandler) BinaryMove(c *cli.Context) error {
136136
ctx := ctxutil.WithGlobalFlags(c)
137137
from := c.Args().Get(0)
138138
to := c.Args().Get(1)
@@ -157,7 +157,7 @@ func isFilePath(s string) bool {
157157
}
158158

159159
// isInStore returns true if the given file is in the store or a mounted substore.
160-
func (s *Action) isInStore(fn string) bool {
160+
func (s *binaryHandler) isInStore(fn string) bool {
161161
fp, err := filepath.Abs(fn)
162162
if err != nil {
163163
return false
@@ -184,7 +184,7 @@ func (s *Action) isInStore(fn string) bool {
184184
// 2. From the store to the filesystem.
185185
//
186186
// Copying secrets in the store must be done through the regular copy command.
187-
func (s *Action) binaryCopy(ctx context.Context, c *cli.Context, from, to string, deleteSource bool) error {
187+
func (s *binaryHandler) binaryCopy(ctx context.Context, c *cli.Context, from, to string, deleteSource bool) error {
188188
if from == "" || to == "" {
189189
op := "copy"
190190
if deleteSource {
@@ -224,7 +224,7 @@ func (s *Action) binaryCopy(ctx context.Context, c *cli.Context, from, to string
224224
}
225225
}
226226

227-
func (s *Action) binaryCopyFromFileToStore(ctx context.Context, from, to string, deleteSource bool) error {
227+
func (s *binaryHandler) binaryCopyFromFileToStore(ctx context.Context, from, to string, deleteSource bool) error {
228228
// if the source is a file the destination must not to avoid ambiguities.
229229
// if necessary this can be resolved by using a absolute path for the file
230230
// and a relative one for the secret.
@@ -260,7 +260,7 @@ func (s *Action) binaryCopyFromFileToStore(ctx context.Context, from, to string,
260260
return nil
261261
}
262262

263-
func (s *Action) binaryCopyFromStoreToFile(ctx context.Context, from, to string, deleteSource bool) error {
263+
func (s *binaryHandler) binaryCopyFromStoreToFile(ctx context.Context, from, to string, deleteSource bool) error {
264264
// if the source is no file we assume it's a secret and to is a filename
265265
// (which may already exist or not).
266266

@@ -289,7 +289,7 @@ func (s *Action) binaryCopyFromStoreToFile(ctx context.Context, from, to string,
289289
return nil
290290
}
291291

292-
func (s *Action) binaryValidate(ctx context.Context, buf []byte, name string) error {
292+
func (s *binaryHandler) binaryValidate(ctx context.Context, buf []byte, name string) error {
293293
h := sha256.New()
294294
_, _ = h.Write(buf)
295295
fileSum := hex.EncodeToString(h.Sum(nil))
@@ -328,7 +328,7 @@ func isBase64Encoded(sec gopass.Secret) bool {
328328
return false
329329
}
330330

331-
func (s *Action) binaryGet(ctx context.Context, name string) ([]byte, error) {
331+
func (s *binaryHandler) binaryGet(ctx context.Context, name string) ([]byte, error) {
332332
sec, err := s.Store.Get(ctx, name)
333333
if err != nil {
334334
return nil, fmt.Errorf("failed to read %q from the store: %w", name, err)
@@ -358,7 +358,7 @@ func (s *Action) binaryGet(ctx context.Context, name string) ([]byte, error) {
358358

359359
// Sum decodes binary content and computes the SHA256 checksum.
360360
// It prints the checksum to stdout.
361-
func (s *Action) Sum(c *cli.Context) error {
361+
func (s *binaryHandler) Sum(c *cli.Context) error {
362362
ctx := ctxutil.WithGlobalFlags(c)
363363
name := c.Args().First()
364364
if name == "" {

0 commit comments

Comments
 (0)
⌂ 换网址