Skip to content

Commit

Permalink
feature: Add fail text option (#301)
Browse files Browse the repository at this point in the history
Signed-off-by: Valentin Kiselev <[email protected]>
  • Loading branch information
mrexox committed Jul 25, 2022
1 parent 42423cd commit b1c0c9d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 28 deletions.
6 changes: 4 additions & 2 deletions internal/config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ type Command struct {
Root string `mapstructure:"root"`
Exclude string `mapstructure:"exclude"`

// DEPRECATED
FailText string `mapstructure:"fail_text"`

// DEPRECATED, will be deleted in 1.2.0
Runner string `mapstructure:"runner"`
}

Expand All @@ -43,7 +45,7 @@ func (c Command) DoSkip(gitState git.State) bool {

type commandRunReplace struct {
Run string `mapstructure:"run"`
Runner string `mapstructure:"runner"` // DEPRECATED
Runner string `mapstructure:"runner"` // DEPRECATED, will be deleted in 1.2.0
}

func mergeCommands(base, extra *viper.Viper) (map[string]*Command, error) {
Expand Down
2 changes: 2 additions & 0 deletions internal/config/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type Script struct {
Skip interface{} `mapstructure:"skip"`
Tags []string `mapstructure:"tags"`

FailText string `mapstructure:"fail_text"`

// DEPRECATED
Run string `mapstructure:"run"`
}
Expand Down
42 changes: 26 additions & 16 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,33 +118,30 @@ Run 'lefthook install' manually.`,
close(resultChan)
}()

var okList, failList []string
var results []runner.Result
for res := range resultChan {
switch res.Status {
case runner.StatusOk:
okList = append(okList, res.Name)
case runner.StatusErr:
failList = append(failList, res.Name)
}
results = append(results, res)
}

if !outputSettings.doSkip(skipSummary) {
printSummary(time.Since(startTime), okList, failList, outputSettings)
printSummary(time.Since(startTime), results, outputSettings)
}

if len(failList) > 0 {
return errors.New("") // No error should be printed
for _, result := range results {
if result.Status == runner.StatusErr {
return errors.New("") // No error should be printed
}
}

return nil
}

func printSummary(
duration time.Duration,
okList, failList []string,
results []runner.Result,
outputSettings skipOutputSettings,
) {
if len(okList) == 0 && len(failList) == 0 {
if len(results) == 0 {
log.Info(log.Cyan("\nSUMMARY: (SKIP EMPTY)"))
return
}
Expand All @@ -154,14 +151,27 @@ func printSummary(
))

if !outputSettings.doSkip(skipSuccess) {
for _, fileName := range okList {
log.Infof("✔️ %s\n", log.Green(fileName))
for _, result := range results {
if result.Status != runner.StatusOk {
continue
}

log.Infof("✔️ %s\n", log.Green(result.Name))
}
}

if !outputSettings.doSkip(skipFailure) {
for _, fileName := range failList {
log.Infof("🥊 %s\n", log.Red(fileName))
for _, result := range results {
if result.Status != runner.StatusErr {
continue
}

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

log.Infof("🥊 %s%s\n", log.Red(result.Name), log.Red(failText))
}
}
}
5 changes: 3 additions & 2 deletions internal/lefthook/runner/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ const (

type Result struct {
Name string
Text string
Status status
}

func resultSuccess(name string) Result {
return Result{Name: name, Status: StatusOk}
}

func resultFail(name string) Result {
return Result{Name: name, Status: StatusErr}
func resultFail(name, text string) Result {
return Result{Name: name, Text: text, Status: StatusErr}
}
16 changes: 8 additions & 8 deletions internal/lefthook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func (r *Runner) RunAll(scriptDirs []string) {
log.StopSpinner()
}

func (r *Runner) fail(name string) {
r.resultChan <- resultFail(name)
func (r *Runner) fail(name, text string) {
r.resultChan <- resultFail(name, text)
r.failed = true
}

Expand Down Expand Up @@ -121,7 +121,7 @@ func (r *Runner) runScript(script *config.Script, path string, file os.FileInfo)
if (file.Mode() & executableMask) == 0 {
if err := r.fs.Chmod(path, executableFileMode); err != nil {
log.Errorf("Couldn't change file mode to make file executable: %s", err)
r.fail(file.Name())
r.fail(file.Name(), "")
return
}
}
Expand All @@ -134,7 +134,7 @@ func (r *Runner) runScript(script *config.Script, path string, file os.FileInfo)
args = append(args, path)
args = append(args, r.args[:]...)

r.run(file.Name(), r.repo.RootPath, args)
r.run(file.Name(), r.repo.RootPath, script.FailText, args)
}

func (r *Runner) runCommands() {
Expand Down Expand Up @@ -178,7 +178,7 @@ func (r *Runner) runCommand(name string, command *config.Command) {
}

if err := command.Validate(); err != nil {
r.fail(name)
r.fail(name, "")
return
}

Expand All @@ -189,7 +189,7 @@ func (r *Runner) runCommand(name string, command *config.Command) {
}

root := filepath.Join(r.repo.RootPath, command.Root)
r.run(name, root, args)
r.run(name, root, command.FailText, args)
}

func (r *Runner) buildCommandArgs(command *config.Command) []string {
Expand Down Expand Up @@ -269,12 +269,12 @@ func prepareFiles(command *config.Command, files []string) string {
return strings.Join(files, " ")
}

func (r *Runner) run(name string, root string, args []string) {
func (r *Runner) run(name, root, failText string, args []string) {
out, err := Execute(root, args)

var execName string
if err != nil {
r.fail(name)
r.fail(name, failText)
execName = fmt.Sprint(log.Red("\n EXECUTE >"), log.Bold(name))
} else {
r.success(name)
Expand Down

0 comments on commit b1c0c9d

Please sign in to comment.