-
Notifications
You must be signed in to change notification settings - Fork 112
Added process.Background() and process.Forwarded()
#804
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 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8d5bf91
Added `process.Background()` and `process.Forwarded()`
nfx e42b9e2
fix test
nfx 0fd00f2
fix issues with windows
nfx a1238a2
fix wrong copy-paste
nfx 2e0d41a
allow access to stderr
nfx f0b4477
robust
nfx feafc5f
will it make it less flaky?
nfx cf068d0
address comments
nfx 5467165
more tests
nfx ed03035
make it work on windows
nfx 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
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,52 @@ | ||
| package process | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "os/exec" | ||
| "strings" | ||
|
|
||
| "github.com/databricks/cli/libs/log" | ||
| ) | ||
|
|
||
| type ProcessError struct { | ||
| Command string | ||
| Err error | ||
| Stdout string | ||
| Stderr string | ||
| } | ||
|
|
||
| func (perr *ProcessError) Unwrap() error { | ||
| return perr.Err | ||
| } | ||
|
|
||
| func (perr *ProcessError) Error() string { | ||
| return fmt.Sprintf("%s: %s %s", perr.Command, perr.Stderr, perr.Err) | ||
nfx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func Background(ctx context.Context, args []string, opts ...execOption) (string, error) { | ||
| commandStr := strings.Join(args, " ") | ||
| log.Debugf(ctx, "running: %s", commandStr) | ||
| cmd := exec.CommandContext(ctx, args[0], args[1:]...) | ||
| var stdout, stderr bytes.Buffer | ||
| // For background processes, there's no standard input | ||
| cmd.Stdin = nil | ||
| cmd.Stdout = &stdout | ||
| cmd.Stderr = &stderr | ||
| for _, o := range opts { | ||
| err := o(ctx, cmd) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| } | ||
| if err := cmd.Run(); err != nil { | ||
| return stdout.String(), &ProcessError{ | ||
| Err: err, | ||
| Command: commandStr, | ||
| Stdout: stdout.String(), | ||
| Stderr: stderr.String(), | ||
| } | ||
| } | ||
| return stdout.String(), nil | ||
| } | ||
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,81 @@ | ||
| package process | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestBackgroundUnwrapsNotFound(t *testing.T) { | ||
| ctx := context.Background() | ||
| _, err := Background(ctx, []string{"/bin/meeecho", "1"}) | ||
| assert.ErrorIs(t, err, os.ErrNotExist) | ||
| } | ||
|
|
||
| func TestBackground(t *testing.T) { | ||
| ctx := context.Background() | ||
| res, err := Background(ctx, []string{"echo", "1"}, WithDir("/")) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, "1", res) | ||
| } | ||
nfx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func TestBackgroundOnlyStdoutGetsoutOnSuccess(t *testing.T) { | ||
| ctx := context.Background() | ||
| res, err := Background(ctx, []string{ | ||
| "python3", "-c", "import sys; sys.stderr.write('1'); sys.stdout.write('2')", | ||
| }) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, "2", res) | ||
| } | ||
|
|
||
| func TestBackgroundCombinedOutput(t *testing.T) { | ||
| ctx := context.Background() | ||
| var buf bytes.Buffer | ||
| res, err := Background(ctx, []string{ | ||
| "python3", "-c", "import sys; sys.stderr.write('1'); sys.stdout.write('2')", | ||
| }, WithCombinedOutput(&buf)) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, "2", res) | ||
| assert.Equal(t, "12", buf.String()) | ||
| } | ||
|
|
||
| func TestBackgroundCombinedOutputFailure(t *testing.T) { | ||
| ctx := context.Background() | ||
| var buf bytes.Buffer | ||
| res, err := Background(ctx, []string{ | ||
| "python3", "-c", "import sys; sys.stderr.write('1'); sys.stdout.write('2'); sys.exit(42)", | ||
| }, WithCombinedOutput(&buf)) | ||
| var processErr *ProcessError | ||
| if assert.ErrorAs(t, err, &processErr) { | ||
| assert.Equal(t, "1", processErr.Stderr) | ||
| assert.Equal(t, "2", processErr.Stdout) | ||
| } | ||
| assert.Equal(t, "2", res) | ||
| assert.Equal(t, "12", buf.String()) | ||
| } | ||
|
|
||
| func TestBackgroundNoStdin(t *testing.T) { | ||
| ctx := context.Background() | ||
| res, err := Background(ctx, []string{"cat"}) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, "", res) | ||
| } | ||
|
|
||
| func TestBackgroundFails(t *testing.T) { | ||
| ctx := context.Background() | ||
| _, err := Background(ctx, []string{"ls", "/dev/null/x"}) | ||
| assert.NotNil(t, err) | ||
| } | ||
|
|
||
| func TestBackgroundFailsOnOption(t *testing.T) { | ||
| ctx := context.Background() | ||
| _, err := Background(ctx, []string{"ls", "/dev/null/x"}, func(_ context.Context, c *exec.Cmd) error { | ||
| return fmt.Errorf("nope") | ||
| }) | ||
| assert.EqualError(t, err, "nope") | ||
| } | ||
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,42 @@ | ||
| package process | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "os/exec" | ||
| "strings" | ||
|
|
||
| "github.com/databricks/cli/libs/log" | ||
| ) | ||
|
|
||
| func Forwarded(ctx context.Context, args []string, src io.Reader, dst io.Writer, opts ...execOption) error { | ||
| commandStr := strings.Join(args, " ") | ||
| log.Debugf(ctx, "starting: %s", commandStr) | ||
| cmd := exec.CommandContext(ctx, args[0], args[1:]...) | ||
|
|
||
| // make sure to sync on writing to stdout | ||
| reader, writer := io.Pipe() | ||
|
|
||
| // empirical tests showed buffered copies being more responsive | ||
| go io.CopyBuffer(dst, reader, make([]byte, 128)) | ||
nfx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| defer reader.Close() | ||
| defer writer.Close() | ||
| cmd.Stdout = writer | ||
| cmd.Stderr = writer | ||
nfx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
nfx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| cmd.Stdin = src | ||
|
|
||
| // apply common options | ||
| for _, o := range opts { | ||
| err := o(ctx, cmd) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| err := cmd.Start() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return cmd.Wait() | ||
| } | ||
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,43 @@ | ||
| package process | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "os/exec" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestForwarded(t *testing.T) { | ||
| ctx := context.Background() | ||
| var buf bytes.Buffer | ||
| err := Forwarded(ctx, []string{ | ||
| "python3", "-c", "print(input('input: '))", | ||
| }, strings.NewReader("abc\n"), &buf) | ||
| assert.NoError(t, err) | ||
|
|
||
| assert.Equal(t, "input: abc", strings.TrimSpace(buf.String())) | ||
| } | ||
|
|
||
| func TestForwardedFails(t *testing.T) { | ||
| ctx := context.Background() | ||
| var buf bytes.Buffer | ||
| err := Forwarded(ctx, []string{ | ||
| "_non_existent_", | ||
| }, strings.NewReader("abc\n"), &buf) | ||
| assert.NotNil(t, err) | ||
| } | ||
|
|
||
| func TestForwardedFailsOnStdinPipe(t *testing.T) { | ||
| ctx := context.Background() | ||
| var buf bytes.Buffer | ||
| err := Forwarded(ctx, []string{ | ||
| "_non_existent_", | ||
| }, strings.NewReader("abc\n"), &buf, func(_ context.Context, c *exec.Cmd) error { | ||
| c.Stdin = strings.NewReader("x") | ||
| return nil | ||
| }) | ||
| assert.NotNil(t, err) | ||
| } |
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.