Skip to content

Commit

Permalink
chore: use new config options
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Nov 1, 2024
1 parent 0e78e9c commit acc7ba0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 44 deletions.
20 changes: 0 additions & 20 deletions .lefthook.toml

This file was deleted.

10 changes: 9 additions & 1 deletion internal/config/do.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,13 @@ func (do *Do) PrintableName(id string) string {
return do.Script
}

return "(" + id + ")"
return "[" + id + "]"
}

func (g *Group) PrintableName(id string) string {
if len(g.Name) != 0 {
return g.Name
}

return "[" + id + "]"
}
12 changes: 10 additions & 2 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,17 @@ func printSummary(
)
}

logResults(0, results, logSettings)
}

func logResults(indent int, results []runner.Result, logSettings log.Settings) {
if logSettings.LogSuccess() {
for _, result := range results {
if !result.Success() {
continue
}

log.Success(result.Name)
log.Success(indent, result.Name)
}
}

Expand All @@ -241,7 +245,11 @@ func printSummary(
continue
}

log.Failure(result.Name, result.Text())
log.Failure(indent, result.Name, result.Text())

if len(result.Sub) > 0 {
logResults(indent+1, result.Sub, logSettings)
}
}
}
}
Expand Down
23 changes: 10 additions & 13 deletions internal/lefthook/runner/do.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package runner
import (
"context"
"errors"
"fmt"
"path/filepath"
"strconv"
"sync"
Expand All @@ -16,8 +15,8 @@ import (
)

var (
errDoContainsBothRunAndScript = errors.New("action contains both `run` and `script` which is not permitted")
errEmptyDo = errors.New("action does not contain execution instructions")
errDoContainsBothRunAndScript = errors.New("both `run` and `script` are not permitted")
errEmptyDo = errors.New("no execution instrations")
errEmptyGroup = errors.New("empty groups are not permitted")
)

Expand Down Expand Up @@ -56,6 +55,7 @@ func (r *Runner) do(ctx context.Context) []Result {
}

func (r *Runner) runDo(ctx context.Context, id string, do *config.Do) Result {
// Check if do action is properly configured
if len(do.Run) > 0 && len(do.Script) > 0 {
return failed(do.PrintableName(id), errDoContainsBothRunAndScript.Error())
}
Expand Down Expand Up @@ -158,11 +158,8 @@ func (r *Runner) doAction(ctx context.Context, id string, do *config.Do) Result
return succeeded(name)
}

func (r *Runner) doGroup(ctx context.Context, id string, group *config.Group) Result {
name := group.Name
if len(name) == 0 {
name = fmt.Sprintf("group (%s)", id)
}
func (r *Runner) doGroup(ctx context.Context, groupId string, group *config.Group) Result {
name := group.PrintableName(groupId)

if len(group.Do) == 0 {
return failed(name, errEmptyGroup.Error())
Expand All @@ -172,24 +169,24 @@ func (r *Runner) doGroup(ctx context.Context, id string, group *config.Group) Re
resultsChan := make(chan Result, len(r.Hook.Do))
var wg sync.WaitGroup

for idx, subdo := range group.Do {
subid := fmt.Sprintf("%s.%d", id, idx)
for i, subdo := range group.Do {
id := strconv.Itoa(i)

if r.failed.Load() && group.Piped {
r.logSkip(subdo.PrintableName(subid), "broken pipe")
r.logSkip(subdo.PrintableName(id), "broken pipe")
continue
}

if !group.Parallel {
results = append(results, r.runDo(ctx, subid, subdo))
results = append(results, r.runDo(ctx, id, subdo))
continue
}

wg.Add(1)
go func(id string, do *config.Do) {
defer wg.Done()
resultsChan <- r.runDo(ctx, id, do)
}(subid, subdo)
}(id, subdo)
}

wg.Wait()
Expand Down
16 changes: 8 additions & 8 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,24 +280,24 @@ func LogMeta(hookName string) {
)
}

func Success(name string) {
format := "✔️ %s\n"
func Success(indent int, name string) {
format := "%s✔️ %s\n"
if !Colorized() {
format = " %s\n"
format = "%s✓ %s\n"
}
Infof(format, Green(name))
Infof(format, strings.Repeat(" ", indent), Green(name))
}

func Failure(name, failText string) {
func Failure(indent int, name, failText string) {
if len(failText) != 0 {
failText = fmt.Sprintf(": %s", failText)
}

format := "🥊 %s%s\n"
format := "%s🥊 %s%s\n"
if !Colorized() {
format = " %s%s\n"
format = "%s✗ %s%s\n"
}
Infof(format, Red(name), Red(failText))
Infof(format, strings.Repeat(" ", indent), Red(name), Red(failText))
}

func box(left, right string) {
Expand Down
19 changes: 19 additions & 0 deletions lefthook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pre-commit:
parallel: true
do:
- run: make lint
glob: '*.go'
stage_fixed: true

- run: lychee --max-concurrency 3 {staged_files}
name: lychee
glob: '*.md'
exclude:
- CHANGELOG.md

- run: make test
glob: '*.go'

- run: typos --write-changes {staged_files}
name: typos
stage_fixed: true

0 comments on commit acc7ba0

Please sign in to comment.