-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: split command with file templates into chunks (#541)
* set the maximum line length for Windows, macOS and Linux * split the command if it uses file templates in run string * execute the split command sequentially
- Loading branch information
Showing
19 changed files
with
765 additions
and
379 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,6 @@ | ||
.vscode/ | ||
.idea/ | ||
.lefthook-local/ | ||
.lefthook/ | ||
/lefthook-local.yml | ||
/lefthook.yml | ||
/lefthook | ||
|
||
tmp/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[pre-commit] | ||
parallel = true | ||
|
||
[pre-commit.commands.lint] | ||
run = "make lint" | ||
glob = "*.go" | ||
stage_fixed = true | ||
|
||
[pre-commit.commands.test] | ||
run = "make test" | ||
glob = "*.go" | ||
|
||
[pre-commit.commands.lychee] | ||
glob = "*.md" | ||
run = "lychee {staged_files}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package exec | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
"syscall" | ||
) | ||
|
||
type CommandExecutor struct{} | ||
|
||
func (e CommandExecutor) Execute(opts Options, out io.Writer) error { | ||
root, _ := filepath.Abs(opts.Root) | ||
envs := make([]string, len(opts.Env)) | ||
for name, value := range opts.Env { | ||
envs = append( | ||
envs, | ||
fmt.Sprintf("%s=%s", strings.ToUpper(name), os.ExpandEnv(value)), | ||
) | ||
} | ||
|
||
for _, args := range opts.Commands { | ||
if err := e.executeOne(args, root, envs, opts.Interactive, os.Stdin, out); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (e CommandExecutor) RawExecute(command []string, out io.Writer) error { | ||
cmd := exec.Command(command[0], command[1:]...) | ||
|
||
cmd.Stdout = out | ||
cmd.Stderr = os.Stderr | ||
|
||
return cmd.Run() | ||
} | ||
|
||
func (e CommandExecutor) executeOne(args []string, root string, envs []string, interactive bool, in io.Reader, out io.Writer) error { | ||
command := exec.Command(args[0]) | ||
command.SysProcAttr = &syscall.SysProcAttr{ | ||
CmdLine: strings.Join(args, " "), | ||
} | ||
command.Dir = root | ||
command.Env = append(os.Environ(), envs...) | ||
|
||
if interactive { | ||
command.Stdout = os.Stdout | ||
} else { | ||
command.Stdout = out | ||
} | ||
|
||
command.Stdin = in | ||
command.Stderr = os.Stderr | ||
err := command.Start() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer func() { _ = command.Process.Kill() }() | ||
|
||
return command.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package exec | ||
|
||
import ( | ||
"io" | ||
) | ||
|
||
// Options contains the data that controls the execution. | ||
type Options struct { | ||
Name, Root, FailText string | ||
Commands [][]string | ||
Env map[string]string | ||
Interactive bool | ||
} | ||
|
||
// Executor provides an interface for command execution. | ||
// It is used here for testing purpose mostly. | ||
type Executor interface { | ||
Execute(opts Options, out io.Writer) error | ||
RawExecute(command []string, out io.Writer) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.