Skip to content

Commit 9f92951

Browse files
committed
Support templates and Git args in setup
1 parent dd503d7 commit 9f92951

File tree

6 files changed

+75
-31
lines changed

6 files changed

+75
-31
lines changed

internal/log/setup.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package log
2+
3+
import "github.com/charmbracelet/lipgloss"
4+
5+
func LogSetup() {
6+
Styled().
7+
WithLeftBorder(lipgloss.ThickBorder(), ColorYellow).
8+
WithPadding(execLogPadding).
9+
Info(Yellow("setup ❯ "))
10+
}

internal/run/controller/command/build_command.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package command
22

33
import (
4-
"strconv"
54
"strings"
65

7-
"github.com/alessio/shellescape"
8-
96
"github.com/evilmartians/lefthook/v2/internal/config"
107
"github.com/evilmartians/lefthook/v2/internal/run/controller/command/replacer"
118
"github.com/evilmartians/lefthook/v2/internal/run/controller/filter"
@@ -86,21 +83,15 @@ func (b *Builder) buildCommand(params *JobParams) ([]string, []string, error) {
8683

8784
// buildReplacer creates the replacer with all supported templates for files and arguments.
8885
func (b *Builder) buildReplacer(params *JobParams) replacer.Replacer {
89-
predefined := make(map[string]string)
90-
predefined["{0}"] = strings.Join(b.opts.GitArgs, " ")
91-
for i, arg := range b.opts.GitArgs {
92-
predefined["{"+strconv.Itoa(i+1)+"}"] = arg
93-
}
94-
for key, replacement := range b.opts.Templates {
95-
predefined["{"+key+"}"] = replacement
96-
}
97-
predefined["{lefthook_job_name}"] = shellescape.Quote(params.Name)
98-
9986
if len(b.opts.ForceFiles) > 0 {
100-
return replacer.NewMocked(b.opts.ForceFiles, predefined)
87+
return replacer.NewMocked(b.opts.ForceFiles).
88+
AddTemplates(b.opts.Templates).
89+
AddGitArgs(b.opts.GitArgs)
10190
}
10291

103-
return replacer.New(b.git, params.Root, params.FilesCmd, predefined)
92+
return replacer.New(b.git, params.Root, params.FilesCmd).
93+
AddTemplates(b.opts.Templates).
94+
AddGitArgs(b.opts.GitArgs)
10495
}
10596

10697
func (b *Builder) buildFilter(params *JobParams) *filter.Filter {

internal/run/controller/command/replacer/replacer.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"regexp"
66
"runtime"
7+
"strconv"
78
"strings"
89

910
"github.com/alessio/shellescape"
@@ -31,7 +32,6 @@ func New(
3132
git *git.Repository,
3233
root string,
3334
filesCmd string,
34-
templates map[string]string,
3535
) Replacer {
3636
var (
3737
staged = git.StagedFiles
@@ -56,11 +56,35 @@ func New(
5656
config.SubAllFiles: all,
5757
config.SubFiles: cmd,
5858
},
59-
templates: templates,
6059
}
6160
}
6261

63-
func NewMocked(files []string, templates map[string]string) Replacer {
62+
func (r Replacer) AddTemplates(templates map[string]string) Replacer {
63+
if r.templates == nil {
64+
r.templates = make(map[string]string)
65+
}
66+
67+
for key, replacement := range templates {
68+
r.templates["{"+key+"}"] = replacement
69+
}
70+
71+
return r
72+
}
73+
74+
func (r Replacer) AddGitArgs(args []string) Replacer {
75+
if r.templates == nil {
76+
r.templates = make(map[string]string)
77+
}
78+
79+
r.templates["{0}"] = strings.Join(args, " ")
80+
for i, arg := range args {
81+
r.templates["{"+strconv.Itoa(i+1)+"}"] = arg
82+
}
83+
84+
return r
85+
}
86+
87+
func NewMocked(files []string) Replacer {
6488
forceFilesFn := func() ([]string, error) { return files, nil } //nolint:unparam
6589

6690
return Replacer{
@@ -71,7 +95,6 @@ func NewMocked(files []string, templates map[string]string) Replacer {
7195
config.SubAllFiles: forceFilesFn,
7296
config.SubFiles: forceFilesFn,
7397
},
74-
templates: templates,
7598
}
7699
}
77100

internal/run/controller/command/replacer/replacer_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,11 @@ func Test_ReplaceAndSplit_CustomTemplates(t *testing.T) {
212212
assert := assert.New(t)
213213

214214
// Create a replacer with custom templates (note: keys include braces)
215-
r := NewMocked([]string{"file1.js"}, map[string]string{
216-
"{use-mise}": `eval "$(mise env)"`,
217-
})
215+
r := NewMocked([]string{"file1.js"}).AddTemplates(
216+
map[string]string{
217+
"use-mise": `eval "$(mise env)"`,
218+
},
219+
)
218220

219221
// Discover templates in the command (use empty filter)
220222
emptyFilter := &filter.Filter{}
@@ -233,7 +235,7 @@ func Test_ReplaceAndSplit_CustomTemplates(t *testing.T) {
233235
assert := assert.New(t)
234236

235237
// Create a replacer with a file that needs escaping
236-
r := NewMocked([]string{"file with spaces.js"}, map[string]string{})
238+
r := NewMocked([]string{"file with spaces.js"})
237239

238240
// Discover templates in the command (use empty filter)
239241
emptyFilter := &filter.Filter{}

internal/run/controller/controller.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,9 @@ func (c *Controller) RunHook(ctx context.Context, opts Options, hook *config.Hoo
7272
}
7373
}
7474

75-
log.StopSpinner()
76-
if err := c.setup(ctx, hook.Setup); err != nil {
75+
if err := c.setup(ctx, opts, hook.Setup); err != nil {
7776
log.Warnf("Failed to run setup: %s\n", err)
7877
}
79-
log.StartSpinner()
8078

8179
if !opts.DisableTTY && !hook.Follow {
8280
log.StartSpinner()

internal/run/controller/setup.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,42 @@ package controller
22

33
import (
44
"context"
5+
"os"
56

67
"github.com/evilmartians/lefthook/v2/internal/config"
8+
"github.com/evilmartians/lefthook/v2/internal/log"
9+
"github.com/evilmartians/lefthook/v2/internal/run/controller/command/replacer"
710
"github.com/evilmartians/lefthook/v2/internal/run/controller/exec"
11+
"github.com/evilmartians/lefthook/v2/internal/system"
812
)
913

10-
func (c *Controller) setup(ctx context.Context, setupInstructions []*config.SetupInstruction) error {
14+
func (c *Controller) setup(
15+
ctx context.Context,
16+
opts Options,
17+
setupInstructions []*config.SetupInstruction,
18+
) error {
1119
if len(setupInstructions) == 0 {
1220
return nil
1321
}
1422

23+
log.StopSpinner()
24+
defer log.StartSpinner()
25+
26+
replacer := replacer.New(c.git, "", "").
27+
AddTemplates(opts.Templates).
28+
AddGitArgs(opts.GitArgs)
29+
1530
commands := make([]string, 0, len(setupInstructions))
1631
for _, instr := range setupInstructions {
17-
commands = append(commands, instr.Run)
32+
if err := replacer.Discover(instr.Run, nil); err != nil {
33+
return err
34+
}
35+
36+
rawCommands, _ := replacer.ReplaceAndSplit(instr.Run, system.MaxCmdLen())
37+
commands = append(commands, rawCommands...)
1838
}
1939

20-
return c.run(ctx, "setup", true, exec.Options{
21-
Commands: commands,
22-
})
40+
log.LogSetup()
41+
42+
return c.executor.Execute(ctx, exec.Options{Commands: commands}, system.NullReader, os.Stdout)
2343
}

0 commit comments

Comments
 (0)