Skip to content
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

By default, ExecShell will add an io.Discard #20

Merged
merged 2 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,8 @@ func ExecShell(ctx context.Context, client *docker.Client, containerID string, c
Env: opts.Env,
Cmd: []string{"bash", "-c", cmdString},
AttachStdin: opts.Interactive,
AttachStdout: opts.Interactive || opts.CombinedOutput != nil,
AttachStderr: opts.Interactive || opts.CombinedOutput != nil,
AttachStdout: true,
AttachStderr: true,
Tty: opts.Interactive,
Container: containerID,
WorkingDir: opts.Dir,
Expand All @@ -542,6 +542,12 @@ func ExecShell(ctx context.Context, client *docker.Client, containerID string, c
startOpts.OutputStream = opts.CombinedOutput
startOpts.ErrorStream = opts.CombinedOutput
}

if opts.CombinedOutput == nil {
startOpts.OutputStream = ioutil.Discard
startOpts.ErrorStream = ioutil.Discard
}

err = client.StartExec(exec.ID, startOpts)
if err != nil {
return fmt.Errorf("execute shell command in container %s: %w", containerID, err)
Expand Down
26 changes: 8 additions & 18 deletions containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,18 @@ func TestExecExitError(t *testing.T) {
}

cmdStat := "stat /somewhere/strange/that/do/not/exist"
err = ExecShell(ctx, client, containerID, cmdStat, &ExecShellOptions{
CombinedOutput: os.Stdout,
})
if err != nil {
if code, ok := IsExitError(err); !ok || code != 1 {
t.Errorf("error = %v; want exit code 1", err)
}
} else {
t.Error("Expecting an error, but none was returned")
err = ExecShell(ctx, client, containerID, cmdStat, &ExecShellOptions{})
if code, ok := IsExitError(err); !ok || code != 1 {
t.Logf("is it exit error? %v", ok)
t.Errorf("error = %v; want exit code 1, got %d", err, code)
}

// Should have the "-p" flag
cmdMkdir := "mkdir /somewhere/strange/that/do/not/exist"
err = ExecShell(ctx, client, containerID, cmdMkdir, &ExecShellOptions{
CombinedOutput: os.Stdout,
})
if err != nil {
if code, ok := IsExitError(err); !ok || code != 1 {
t.Errorf("error = %v; want exit code 1", err)
}
} else {
t.Error("Expecting an error, but none was returned")
err = ExecShell(ctx, client, containerID, cmdMkdir, &ExecShellOptions{})
if code, ok := IsExitError(err); !ok || code != 1 {
t.Logf("is it exit error? %v", ok)
t.Errorf("error = %v; want exit code 1, got %d", err, code)
}

}
Expand Down