-
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 4 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package process | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "strings" | ||
|
|
||
| "github.com/databricks/cli/libs/log" | ||
| ) | ||
|
|
||
| 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:]...) | ||
| stdout := &bytes.Buffer{} | ||
| cmd.Stdin = os.Stdin | ||
nfx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| cmd.Stdout = stdout | ||
| cmd.Stderr = stdout | ||
nfx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for _, o := range opts { | ||
| err := o(cmd) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| } | ||
| if err := cmd.Run(); err != nil { | ||
| return "", fmt.Errorf("%s: %s %w", commandStr, stdout.String(), err) | ||
nfx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| return strings.TrimSpace(stdout.String()), nil | ||
nfx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
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,31 @@ | ||
| package process | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os/exec" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| 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 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(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,48 @@ | ||
| 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() | ||
| 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
|
||
|
|
||
| // apply common options | ||
| for _, o := range opts { | ||
| err := o(cmd) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| // pipe standard input to the child process, so that we can allow terminal UX | ||
| // see the PoC at https://github.com/databricks/cli/pull/637 | ||
| stdin, err := cmd.StdinPipe() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| go io.CopyBuffer(stdin, src, make([]byte, 128)) | ||
| defer stdin.Close() | ||
nfx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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() | ||
| buf := bytes.NewBufferString("") | ||
nfx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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() | ||
| buf := bytes.NewBufferString("") | ||
| err := Forwarded(ctx, []string{ | ||
| "_non_existent_", | ||
| }, strings.NewReader("abc\n"), buf) | ||
| assert.NotNil(t, err) | ||
| } | ||
|
|
||
| func TestForwardedFailsOnStdinPipe(t *testing.T) { | ||
| ctx := context.Background() | ||
| buf := bytes.NewBufferString("") | ||
| err := Forwarded(ctx, []string{ | ||
| "_non_existent_", | ||
| }, strings.NewReader("abc\n"), buf, func(c *exec.Cmd) error { | ||
| c.Stdin = strings.NewReader("x") | ||
| return nil | ||
| }) | ||
| assert.NotNil(t, err) | ||
| } | ||
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,51 @@ | ||
| package process | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "os/exec" | ||
| ) | ||
|
|
||
| type execOption func(*exec.Cmd) error | ||
|
|
||
| func WithEnv(key, value string) execOption { | ||
| return func(c *exec.Cmd) error { | ||
| if c.Env == nil { | ||
| c.Env = os.Environ() | ||
nfx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
nfx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| v := fmt.Sprintf("%s=%s", key, value) | ||
| c.Env = append(c.Env, v) | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func WithEnvs(envs map[string]string) execOption { | ||
| return func(c *exec.Cmd) error { | ||
| for k, v := range envs { | ||
| err := WithEnv(k, v)(c) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func WithDir(dir string) execOption { | ||
| return func(c *exec.Cmd) error { | ||
| c.Dir = dir | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func WithStdoutPipe(dst *io.ReadCloser) execOption { | ||
| return func(c *exec.Cmd) error { | ||
| outPipe, err := c.StdoutPipe() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| *dst = outPipe | ||
| return 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,24 @@ | ||
| package process | ||
|
|
||
| import ( | ||
| "context" | ||
| "runtime" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestWithEnvs(t *testing.T) { | ||
| if runtime.GOOS == "windows" { | ||
| // Skipping test on windows for now because of the following error: | ||
| // /bin/sh -c echo $FOO $BAR: exec: "/bin/sh": file does not exist | ||
| t.SkipNow() | ||
| } | ||
| ctx := context.Background() | ||
| res, err := Background(ctx, []string{"/bin/sh", "-c", "echo $FOO $BAR"}, WithEnvs(map[string]string{ | ||
| "FOO": "foo", | ||
| "BAR": "delirium", | ||
| })) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, "foo delirium", 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
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.