Skip to content

Commit 1ff4952

Browse files
author
Alexis Destrez
committed
Swap clicfg hook function arguments for easier usage
1 parent 14027a6 commit 1ff4952

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

cfg/cfg.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111
type SourceFunc[T any] func(ctx context.Context, cfg *T) error
1212

1313
// BeforeCommandExecutionHook replaces dest with provided config sources.
14-
func BeforeCommandExecutionHook[T any](sources []SourceFunc[T], dest *T) cli.HookFunc {
14+
func BeforeCommandExecutionHook[T any](dest *T, source SourceFunc[T], sources ...SourceFunc[T]) cli.HookFunc {
15+
sources = append([]SourceFunc[T]{source}, sources...)
16+
1517
return func(ctx context.Context) error {
1618
cfg := new(T)
1719

cfg/cfg_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func Test_BeforeCommandExecutionHook(t *testing.T) {
1515
var cfg config
1616

1717
t.Run("ok", func(t *testing.T) {
18-
assert.NilError(t, BeforeCommandExecutionHook([]SourceFunc[config]{
18+
assert.NilError(t, BeforeCommandExecutionHook(&cfg,
1919
func(_ context.Context, cfg *config) error {
2020
cfg.A += "1"
2121
return nil
@@ -28,16 +28,18 @@ func Test_BeforeCommandExecutionHook(t *testing.T) {
2828
cfg.A += "3"
2929
return nil
3030
},
31-
}, &cfg)(context.Background()))
31+
)(context.Background()))
3232

3333
assert.Check(t, cfg.A == "123")
3434
})
3535

3636
t.Run("error", func(t *testing.T) {
3737
expectedErr := errors.New("boom")
3838

39-
assert.ErrorIs(t, BeforeCommandExecutionHook([]SourceFunc[config]{
40-
func(context.Context, *config) error { return expectedErr },
41-
}, &cfg)(context.Background()), expectedErr)
39+
assert.ErrorIs(t, BeforeCommandExecutionHook(&cfg,
40+
func(context.Context, *config) error {
41+
return expectedErr
42+
},
43+
)(context.Background()), expectedErr)
4244
})
4345
}

0 commit comments

Comments
 (0)