Skip to content

Commit 08d69af

Browse files
authored
chore: refactor Result handling (#689)
* chore: refactor results * chore: cleanup * chore: add integrity test on fail_text option
1 parent 96d2b32 commit 08d69af

File tree

14 files changed

+96
-96
lines changed

14 files changed

+96
-96
lines changed

internal/lefthook/run.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/evilmartians/lefthook/internal/config"
15-
"github.com/evilmartians/lefthook/internal/lefthook/run"
15+
"github.com/evilmartians/lefthook/internal/lefthook/runner"
1616
"github.com/evilmartians/lefthook/internal/log"
1717
"github.com/evilmartians/lefthook/internal/version"
1818
)
@@ -117,7 +117,7 @@ Run 'lefthook install' manually.`,
117117
}
118118

119119
startTime := time.Now()
120-
resultChan := make(chan run.Result, len(hook.Commands)+len(hook.Scripts))
120+
resultChan := make(chan runner.Result, len(hook.Commands)+len(hook.Scripts))
121121

122122
if args.FilesFromStdin {
123123
paths, err := io.ReadAll(os.Stdin)
@@ -133,21 +133,6 @@ Run 'lefthook install' manually.`,
133133
args.Files = append(args.Files, files...)
134134
}
135135

136-
runner := run.NewRunner(
137-
run.Options{
138-
Repo: l.repo,
139-
Hook: hook,
140-
HookName: hookName,
141-
GitArgs: gitArgs,
142-
ResultChan: resultChan,
143-
LogSettings: logSettings,
144-
DisableTTY: cfg.NoTTY || args.NoTTY,
145-
Files: args.Files,
146-
Force: args.Force,
147-
RunOnlyCommands: args.RunOnlyCommands,
148-
},
149-
)
150-
151136
sourceDirs := []string{
152137
filepath.Join(l.repo.RootPath, cfg.SourceDir),
153138
filepath.Join(l.repo.RootPath, cfg.SourceDirLocal),
@@ -169,12 +154,27 @@ Run 'lefthook install' manually.`,
169154
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
170155
defer stop()
171156

157+
r := runner.New(
158+
runner.Options{
159+
Repo: l.repo,
160+
Hook: hook,
161+
HookName: hookName,
162+
GitArgs: gitArgs,
163+
ResultChan: resultChan,
164+
LogSettings: logSettings,
165+
DisableTTY: cfg.NoTTY || args.NoTTY,
166+
Files: args.Files,
167+
Force: args.Force,
168+
RunOnlyCommands: args.RunOnlyCommands,
169+
},
170+
)
171+
172172
go func() {
173-
runner.RunAll(ctx, sourceDirs)
173+
r.RunAll(ctx, sourceDirs)
174174
close(resultChan)
175175
}()
176176

177-
var results []run.Result
177+
var results []runner.Result
178178
for res := range resultChan {
179179
results = append(results, res)
180180
}
@@ -186,7 +186,7 @@ Run 'lefthook install' manually.`,
186186
printSummary(time.Since(startTime), results, logSettings)
187187

188188
for _, result := range results {
189-
if result.Status == run.StatusErr {
189+
if result.Err != nil {
190190
return errors.New("") // No error should be printed
191191
}
192192
}
@@ -196,7 +196,7 @@ Run 'lefthook install' manually.`,
196196

197197
func printSummary(
198198
duration time.Duration,
199-
results []run.Result,
199+
results []runner.Result,
200200
logSettings log.Settings,
201201
) {
202202
if logSettings.LogSummary() {
@@ -227,7 +227,7 @@ func printSummary(
227227

228228
if logSettings.LogSuccess() {
229229
for _, result := range results {
230-
if result.Status != run.StatusOk {
230+
if result.Err != nil {
231231
continue
232232
}
233233

@@ -237,13 +237,13 @@ func printSummary(
237237

238238
if logSettings.LogFailure() {
239239
for _, result := range results {
240-
if result.Status != run.StatusErr {
240+
if result.Err == nil {
241241
continue
242242
}
243243

244-
var failText string
245-
if len(result.Text) != 0 {
246-
failText = fmt.Sprintf(": %s", result.Text)
244+
failText := result.Err.Error()
245+
if len(failText) != 0 {
246+
failText = fmt.Sprintf(": %s", failText)
247247
}
248248

249249
log.Infof("🥊 %s%s\n", log.Red(result.Name), log.Red(failText))

internal/lefthook/run/result.go

Lines changed: 0 additions & 20 deletions
This file was deleted.
File renamed without changes.

internal/lefthook/run/prepare_command.go renamed to internal/lefthook/runner/prepare_command.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package run
1+
package runner
22

33
import (
44
"errors"
@@ -9,7 +9,7 @@ import (
99
"gopkg.in/alessio/shellescape.v1"
1010

1111
"github.com/evilmartians/lefthook/internal/config"
12-
"github.com/evilmartians/lefthook/internal/lefthook/run/filter"
12+
"github.com/evilmartians/lefthook/internal/lefthook/runner/filter"
1313
"github.com/evilmartians/lefthook/internal/log"
1414
)
1515

internal/lefthook/run/prepare_command_test.go renamed to internal/lefthook/runner/prepare_command_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package run
1+
package runner
22

33
import (
44
"fmt"

0 commit comments

Comments
 (0)