From e898b5c8ba56c4d6f29a4d1f433baa1779a0845b Mon Sep 17 00:00:00 2001 From: Alexander Abroskin Date: Sat, 14 Dec 2019 16:07:47 +0300 Subject: [PATCH] return before executing command --- cmd/run.go | 53 +++++++++++++++++++++++----------------------- cmd/run_windows.go | 51 +++++++++++++++++++++++--------------------- 2 files changed, 54 insertions(+), 50 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index 1494e554..b45f70c1 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -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" @@ -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 @@ -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. @@ -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 @@ -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. diff --git a/cmd/run_windows.go b/cmd/run_windows.go index dd384027..72d55220 100644 --- a/cmd/run_windows.go +++ b/cmd/run_windows.go @@ -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 @@ -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) @@ -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 @@ -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)