-
Notifications
You must be signed in to change notification settings - Fork 154
Added exec.NewCommandExecutor to execute commands with correct interpreter #1075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d0b152f
Added cmdio.NewCommandExecutor to execute artifact build commands
andrewnester 3d317b0
find correct interpreter per os
andrewnester 09d32b4
execute with temp file
andrewnester 6d880fc
use correct file extension
andrewnester a21ec7c
cleanup test files
andrewnester 9703eaf
removed unnecessary interpreters
andrewnester f1f0497
fixed test
andrewnester 18930f9
added test for cmd on windows
andrewnester a265971
fixed test
andrewnester 67e6f02
addressed feedback
andrewnester 3678049
cleanup in goroutine
andrewnester 2b75ed0
safe async runs
andrewnester 35c718f
fix cmd.Wait
andrewnester a101ef5
removed sync logic from command
andrewnester File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,18 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestArtifactBuild(t *testing.T) { | ||
| artifact := Artifact{ | ||
| BuildCommand: "echo 'Hello from build command'", | ||
| } | ||
| res, err := artifact.Build(context.Background()) | ||
| assert.NoError(t, err) | ||
| assert.NotNil(t, res) | ||
| assert.Equal(t, "Hello from build command\n", string(res)) | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,101 @@ | ||
| package exec | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "os" | ||
| osexec "os/exec" | ||
| ) | ||
|
|
||
| type Command interface { | ||
| // Wait for command to terminate. It must have been previously started. | ||
| Wait() error | ||
|
|
||
| // StdinPipe returns a pipe that will be connected to the command's standard input when the command starts. | ||
| Stdout() io.ReadCloser | ||
|
|
||
| // StderrPipe returns a pipe that will be connected to the command's standard error when the command starts. | ||
| Stderr() io.ReadCloser | ||
| } | ||
|
|
||
| type command struct { | ||
| cmd *osexec.Cmd | ||
| execContext *execContext | ||
| stdout io.ReadCloser | ||
| stderr io.ReadCloser | ||
| } | ||
|
|
||
| func (c *command) Wait() error { | ||
| // After the command has finished (cmd.Wait call), remove the temporary script file | ||
| defer os.Remove(c.execContext.scriptFile) | ||
|
|
||
| err := c.cmd.Wait() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (c *command) Stdout() io.ReadCloser { | ||
| return c.stdout | ||
| } | ||
|
|
||
| func (c *command) Stderr() io.ReadCloser { | ||
| return c.stderr | ||
| } | ||
|
|
||
| type Executor struct { | ||
| interpreter interpreter | ||
| dir string | ||
| } | ||
|
|
||
| func NewCommandExecutor(dir string) (*Executor, error) { | ||
| interpreter, err := findInterpreter() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &Executor{ | ||
| interpreter: interpreter, | ||
| dir: dir, | ||
| }, nil | ||
| } | ||
|
|
||
| func (e *Executor) StartCommand(ctx context.Context, command string) (Command, error) { | ||
| ec, err := e.interpreter.prepare(command) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return e.start(ctx, ec) | ||
| } | ||
|
|
||
| func (e *Executor) start(ctx context.Context, ec *execContext) (Command, error) { | ||
| cmd := osexec.CommandContext(ctx, ec.executable, ec.args...) | ||
| cmd.Dir = e.dir | ||
|
|
||
| stdout, err := cmd.StdoutPipe() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| stderr, err := cmd.StderrPipe() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &command{cmd, ec, stdout, stderr}, cmd.Start() | ||
| } | ||
|
|
||
| func (e *Executor) Exec(ctx context.Context, command string) ([]byte, error) { | ||
| cmd, err := e.StartCommand(ctx, command) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| res, err := io.ReadAll(io.MultiReader(cmd.Stdout(), cmd.Stderr())) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return res, cmd.Wait() | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.