Skip to content

Commit

Permalink
return before executing command
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Abroskin committed Dec 14, 2019
1 parent 2465c02 commit e898b5c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 50 deletions.
53 changes: 27 additions & 26 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"github.com/Arkweid/lefthook/context"

arrop "github.com/adam-hanna/arrayOperations"
"github.com/gobwas/glob"
"github.com/creack/pty"
"github.com/gobwas/glob"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -210,6 +210,19 @@ func executeCommand(hooksGroup, commandName string, wg *sync.WaitGroup) {
runner = strings.Replace(runner, subAllFiles, strings.Join(files, " "), -1)
runner = strings.Replace(runner, subFiles, strings.Join(files, " "), -1)

if isSkipCommmand(hooksGroup, commandName) {
log.Println(au.Bold(commandName), au.Brown("(SKIP BY SETTINGS)"))
return
}
if result, _ := arrop.Intersect(getExcludeTags(hooksGroup), getTags(hooksGroup, commandsConfigKey, commandName)); len(result.Interface().([]string)) > 0 {
log.Println(au.Bold(commandName), au.Brown("(SKIP BY TAGS)"))
return
}
if len(files) < 1 && isSkipEmptyCommmand(hooksGroup, commandName) {
log.Println(au.Bold(commandName), au.Brown("(SKIP. NO FILES FOR INSPECTING)"))
return
}

command := exec.Command("sh", "-c", runner)
command.Stdin = os.Stdin

Expand All @@ -225,19 +238,6 @@ func executeCommand(hooksGroup, commandName string, wg *sync.WaitGroup) {
return
}

if isSkipCommmand(hooksGroup, commandName) {
log.Println(au.Brown("(SKIP BY SETTINGS)"))
return
}
if result, _ := arrop.Intersect(getExcludeTags(hooksGroup), getTags(hooksGroup, commandsConfigKey, commandName)); len(result.Interface().([]string)) > 0 {
log.Println(au.Brown("(SKIP BY TAGS)"))
return
}
if len(files) < 1 && isSkipEmptyCommmand(hooksGroup, commandName) {
log.Println(au.Brown("(SKIP. NO FILES FOR INSPECTING)"))
return
}

// pty part start
defer func() { ptyOut.Close() }() // Make sure to close the pty at the end.
// Copy stdin to the pty and the pty to stdout.
Expand Down Expand Up @@ -281,15 +281,24 @@ func executeScript(hooksGroup, source string, executable os.FileInfo, wg *sync.W
command = exec.Command(runnerArg[0], runnerArg[1:]...)
}

if !isScriptExist(hooksGroup, executableName) {
log.Println(au.Bold(executableName), au.Brown("(SKIP BY NOT EXIST IN CONFIG)"))
return
}
if isSkipScript(hooksGroup, executableName) {
log.Println(au.Bold(executableName), au.Brown("(SKIP BY SETTINGS)"))
return
}
if result, _ := arrop.Intersect(getExcludeTags(hooksGroup), getTags(hooksGroup, scriptsConfigKey, executableName)); len(result.Interface().([]string)) > 0 {
log.Println(au.Bold(executableName), au.Brown("(SKIP BY TAGS)"))
return
}

ptyOut, err := pty.Start(command)
mutex.Lock()
defer mutex.Unlock()

log.Println(au.Cyan("\n EXECUTE >"), au.Bold(executableName))
if !isScriptExist(hooksGroup, executableName) {
log.Println(au.Bold(executableName), au.Brown("(SKIP BY NOT EXIST IN CONFIG)"))
return
}
if os.IsPermission(err) {
log.Println(au.Brown("(SKIP NOT EXECUTABLE FILE)"))
return
Expand All @@ -301,14 +310,6 @@ func executeScript(hooksGroup, source string, executable os.FileInfo, wg *sync.W
setPipeBroken()
return
}
if isSkipScript(hooksGroup, executableName) {
log.Println(au.Brown("(SKIP BY SETTINGS)"))
return
}
if result, _ := arrop.Intersect(getExcludeTags(hooksGroup), getTags(hooksGroup, scriptsConfigKey, executableName)); len(result.Interface().([]string)) > 0 {
log.Println(au.Brown("(SKIP BY TAGS)"))
return
}

// pty part start
defer func() { ptyOut.Close() }() // Make sure to close the pty at the end.
Expand Down
51 changes: 27 additions & 24 deletions cmd/run_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,19 @@ func executeCommand(hooksGroup, commandName string, wg *sync.WaitGroup) {
runner = strings.Replace(runner, subAllFiles, strings.Join(files, " "), -1)
runner = strings.Replace(runner, subFiles, strings.Join(files, " "), -1)

if isSkipCommmand(hooksGroup, commandName) {
log.Println(au.Bold(commandName), au.Brown("(SKIP BY SETTINGS)"))
return
}
if result, _ := arrop.Intersect(getExcludeTags(hooksGroup), getTags(hooksGroup, commandsConfigKey, commandName)); len(result.Interface().([]string)) > 0 {
log.Println(au.Bold(commandName), au.Brown("(SKIP BY TAGS)"))
return
}
if len(files) < 1 && isSkipEmptyCommmand(hooksGroup, commandName) {
log.Println(au.Bold(commandName), au.Brown("(SKIP. NO FILES FOR INSPECTING)"))
return
}

runnerArg := strings.Split(runner, " ")
command := exec.Command(runnerArg[0], runnerArg[1:]...)
command.Stdin = os.Stdin
Expand All @@ -223,18 +236,6 @@ func executeCommand(hooksGroup, commandName string, wg *sync.WaitGroup) {
return
}

if isSkipCommmand(hooksGroup, commandName) {
log.Println(au.Brown("(SKIP BY SETTINGS)"))
return
}
if result, _ := arrop.Intersect(getExcludeTags(hooksGroup), getTags(hooksGroup, commandsConfigKey, commandName)); len(result.Interface().([]string)) > 0 {
log.Println(au.Brown("(SKIP BY TAGS)"))
return
}
if len(files) < 1 && isSkipEmptyCommmand(hooksGroup, commandName) {
log.Println(au.Brown("(SKIP. NO FILES FOR INSPECTING)"))
return
}
// io.Copy(os.Stdout, ptyOut) // win specific
if command.Wait() == nil {
okList = append(okList, commandName)
Expand Down Expand Up @@ -275,15 +276,24 @@ func executeScript(hooksGroup, source string, executable os.FileInfo, wg *sync.W
command.Stdout = os.Stdout // win specific
command.Stderr = os.Stderr // win specific

if !isScriptExist(hooksGroup, executableName) {
log.Println(au.Bold(executableName), au.Brown("(SKIP BY NOT EXIST IN CONFIG)"))
return
}
if isSkipScript(hooksGroup, executableName) {
log.Println(au.Bold(executableName), au.Brown("(SKIP BY SETTINGS)"))
return
}
if result, _ := arrop.Intersect(getExcludeTags(hooksGroup), getTags(hooksGroup, scriptsConfigKey, executableName)); len(result.Interface().([]string)) > 0 {
log.Println(au.Bold(executableName), au.Brown("(SKIP BY TAGS)"))
return
}

err := command.Start() // ptyOut, err := pty.Start(command) // win specific
mutex.Lock()
defer mutex.Unlock()

log.Println(au.Cyan("\n EXECUTE >"), au.Bold(executableName))
if !isScriptExist(hooksGroup, executableName) {
log.Println(au.Bold(executableName), au.Brown("(SKIP BY NOT EXIST IN CONFIG)"))
return
}
if os.IsPermission(err) {
log.Println(au.Brown("(SKIP NOT EXECUTABLE FILE)"))
return
Expand All @@ -295,14 +305,7 @@ func executeScript(hooksGroup, source string, executable os.FileInfo, wg *sync.W
setPipeBroken()
return
}
if isSkipScript(hooksGroup, executableName) {
log.Println(au.Brown("(SKIP BY SETTINGS)"))
return
}
if result, _ := arrop.Intersect(getExcludeTags(hooksGroup), getTags(hooksGroup, scriptsConfigKey, executableName)); len(result.Interface().([]string)) > 0 {
log.Println(au.Brown("(SKIP BY TAGS)"))
return
}

// io.Copy(os.Stdout, ptyOut) // win specific
if command.Wait() == nil {
okList = append(okList, executableName)
Expand Down

0 comments on commit e898b5c

Please sign in to comment.