Skip to content

Commit

Permalink
Add modular output
Browse files Browse the repository at this point in the history
  • Loading branch information
0legovich committed Jun 3, 2020
1 parent 1298853 commit 6cbdb29
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 5 deletions.
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -168,6 +168,20 @@ fixer:
$ lefthook run fixer
```

* ### **Modular output**
If you want to display supporting information optionally:
```yml
output:
- meta #(version and which hook running)
- failures #(output from runners with exit code nonzero)
- success #(output from runners with exit code 0)
- summary #(summary block)
```

Or if you don't want to display supporting information:
```yml
output: false
```

---

Expand Down
71 changes: 66 additions & 5 deletions cmd/run.go
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -54,6 +55,11 @@ const (
filesConfigKey string = "files"
colorsConfigKey string = "colors"
parallelConfigKey string = "parallel"
outputConfigKey string = "output"
outputMeta string = "meta"
outputFailures string = "failures"
outputSuccess string = "success"
outputSummary string = "summary"
subFiles string = "{files}"
subAllFiles string = "{all_files}"
subStagedFiles string = "{staged_files}"
Expand Down Expand Up @@ -110,8 +116,7 @@ func RunCmdExecutor(args []string, fs afero.Fs) error {
var wg sync.WaitGroup

startTime := time.Now()
log.Println(au.Cyan("Lefthook v" + version))
log.Println(au.Cyan("RUNNING HOOKS GROUP:"), au.Bold(hooksGroup))
printMeta(hooksGroup)

if isPipedAndParallel(hooksGroup) {
log.Println(au.Brown("Config error! Conflicted options 'piped' and 'parallel'. Remove one of this option from hook group."))
Expand Down Expand Up @@ -146,7 +151,6 @@ func RunCmdExecutor(args []string, fs afero.Fs) error {

commands := getCommands(hooksGroup)
if len(commands) != 0 {

for _, commandName := range commands {
wg.Add(1)
if getParallel(hooksGroup) {
Expand Down Expand Up @@ -257,12 +261,17 @@ func executeCommand(hooksGroup, commandName string, wg *sync.WaitGroup) {
defer func() { ptyOut.Close() }() // Make sure to close the pty at the end.
// Copy stdin to the pty and the pty to stdout.
go func() { io.Copy(ptyOut, os.Stdin) }()
io.Copy(os.Stdout, ptyOut)
comandOutput, _ := ioutil.ReadAll(ptyOut)
ptyOut.Close()
// pty part end

if command.Wait() == nil {
printSuccess(comandOutput)

okList = append(okList, commandName)
} else {
printFailure(comandOutput)

failList = append(failList, commandName)
setPipeBroken()
}
Expand Down Expand Up @@ -337,12 +346,17 @@ func executeScript(hooksGroup, source string, executable os.FileInfo, wg *sync.W
defer func() { ptyOut.Close() }() // Make sure to close the pty at the end.
// Copy stdin to the pty and the pty to stdout.
go func() { io.Copy(ptyOut, os.Stdin) }()
io.Copy(os.Stdout, ptyOut)
comandOutput, _ := ioutil.ReadAll(ptyOut)
ptyOut.Close()
// pty part end

if command.Wait() == nil {
printSuccess(comandOutput)

okList = append(okList, executableName)
} else {
printFailure(comandOutput)

failList = append(failList, executableName)
setPipeBroken()
}
Expand Down Expand Up @@ -374,7 +388,36 @@ func getRunner(hooksGroup, source, executableName string) string {
return runner
}

func printMeta(hooksGroup string) {
if isSkipPrintOutput(outputMeta) {
return
}

log.Println(au.Cyan("Lefthook v" + version))
log.Println(au.Cyan("RUNNING HOOKS GROUP:"), au.Bold(hooksGroup))
}

func printFailure(ptyOut []byte) {
if isSkipPrintOutput(outputFailures) {
return
}

log.Println(string(ptyOut))
}

func printSuccess(ptyOut []byte) {
if isSkipPrintOutput(outputSuccess) {
return
}

log.Println(string(ptyOut))
}

func printSummary(execTime time.Duration) {
if isSkipPrintOutput(outputSummary) {
return
}

if len(okList) == 0 && len(failList) == 0 {
log.Println(au.Cyan("\nSUMMARY:"), au.Brown("(SKIP EMPTY)"))
} else {
Expand Down Expand Up @@ -420,6 +463,24 @@ func isSkipEmptyCommmand(hooksGroup, executableName string) bool {
return true
}

func isSkipPrintOutput(outputDetailValue string) bool {
if !viper.IsSet(outputConfigKey) {
return false
}

if viper.Get(outputConfigKey) == false {
return true
}

for _, elem := range viper.GetStringSlice(outputConfigKey) {
if elem == outputDetailValue {
return false
}
}

return true
}

func getCommands(hooksGroup string) []string {
key := strings.Join([]string{hooksGroup, commandsConfigKey}, ".")
commands := viper.GetStringMap(key)
Expand Down

0 comments on commit 6cbdb29

Please sign in to comment.