Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor Result handling #689

Merged
merged 3 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"time"

"github.com/evilmartians/lefthook/internal/config"
"github.com/evilmartians/lefthook/internal/lefthook/run"
"github.com/evilmartians/lefthook/internal/lefthook/runner"
"github.com/evilmartians/lefthook/internal/log"
"github.com/evilmartians/lefthook/internal/version"
)
Expand Down Expand Up @@ -117,7 +117,7 @@ Run 'lefthook install' manually.`,
}

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

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

runner := run.NewRunner(
run.Options{
Repo: l.repo,
Hook: hook,
HookName: hookName,
GitArgs: gitArgs,
ResultChan: resultChan,
LogSettings: logSettings,
DisableTTY: cfg.NoTTY || args.NoTTY,
Files: args.Files,
Force: args.Force,
RunOnlyCommands: args.RunOnlyCommands,
},
)

sourceDirs := []string{
filepath.Join(l.repo.RootPath, cfg.SourceDir),
filepath.Join(l.repo.RootPath, cfg.SourceDirLocal),
Expand All @@ -169,12 +154,27 @@ Run 'lefthook install' manually.`,
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

r := runner.New(
runner.Options{
Repo: l.repo,
Hook: hook,
HookName: hookName,
GitArgs: gitArgs,
ResultChan: resultChan,
LogSettings: logSettings,
DisableTTY: cfg.NoTTY || args.NoTTY,
Files: args.Files,
Force: args.Force,
RunOnlyCommands: args.RunOnlyCommands,
},
)

go func() {
runner.RunAll(ctx, sourceDirs)
r.RunAll(ctx, sourceDirs)
close(resultChan)
}()

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

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

func printSummary(
duration time.Duration,
results []run.Result,
results []runner.Result,
logSettings log.Settings,
) {
if logSettings.LogSummary() {
Expand Down Expand Up @@ -227,7 +227,7 @@ func printSummary(

if logSettings.LogSuccess() {
for _, result := range results {
if result.Status != run.StatusOk {
if result.Err != nil {
continue
}

Expand All @@ -237,13 +237,13 @@ func printSummary(

if logSettings.LogFailure() {
for _, result := range results {
if result.Status != run.StatusErr {
if result.Err == nil {
continue
}

var failText string
if len(result.Text) != 0 {
failText = fmt.Sprintf(": %s", result.Text)
failText := result.Err.Error()
if len(failText) != 0 {
failText = fmt.Sprintf(": %s", failText)
}

log.Infof("🥊 %s%s\n", log.Red(result.Name), log.Red(failText))
Expand Down
20 changes: 0 additions & 20 deletions internal/lefthook/run/result.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package run
package runner

import (
"errors"
Expand All @@ -9,7 +9,7 @@ import (
"gopkg.in/alessio/shellescape.v1"

"github.com/evilmartians/lefthook/internal/config"
"github.com/evilmartians/lefthook/internal/lefthook/run/filter"
"github.com/evilmartians/lefthook/internal/lefthook/runner/filter"
"github.com/evilmartians/lefthook/internal/log"
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package run
package runner

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package run
package runner

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package run
package runner

import (
"bytes"
Expand All @@ -22,13 +22,11 @@ import (

"github.com/evilmartians/lefthook/internal/config"
"github.com/evilmartians/lefthook/internal/git"
"github.com/evilmartians/lefthook/internal/lefthook/run/exec"
"github.com/evilmartians/lefthook/internal/lefthook/run/filter"
"github.com/evilmartians/lefthook/internal/lefthook/runner/exec"
"github.com/evilmartians/lefthook/internal/lefthook/runner/filter"
"github.com/evilmartians/lefthook/internal/log"
)

type status int8

const (
executableFileMode os.FileMode = 0o751
executableMask os.FileMode = 0o111
Expand Down Expand Up @@ -59,7 +57,13 @@ type Runner struct {
executor exec.Executor
}

func NewRunner(opts Options) *Runner {
// Result contains name of a command/script and an optional fail string.
type Result struct {
Name string
Err error
}

func New(opts Options) *Runner {
return &Runner{
Options: opts,
executor: exec.CommandExecutor{},
Expand Down Expand Up @@ -107,12 +111,12 @@ func (r *Runner) RunAll(ctx context.Context, sourceDirs []string) {
}

func (r *Runner) fail(name string, err error) {
r.ResultChan <- resultFail(name, err.Error())
r.ResultChan <- Result{Name: name, Err: err}
r.failed.Store(true)
}

func (r *Runner) success(name string) {
r.ResultChan <- resultSuccess(name)
r.ResultChan <- Result{Name: name, Err: nil}
}

func (r *Runner) runLFSHook(ctx context.Context) error {
Expand Down
Loading
Loading