revert changes
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
package container // https://github.com/docker/cli/blob/master/cli/command/container/exec.go
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
@ -10,14 +9,14 @@ import (
|
||||
"coopcloud.tech/abra/pkg/log"
|
||||
"github.com/docker/cli/cli"
|
||||
"github.com/docker/cli/cli/command"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types"
|
||||
apiclient "github.com/docker/docker/client"
|
||||
)
|
||||
|
||||
// RunExec runs a command on a remote container. io.Writer corresponds to the
|
||||
// command output.
|
||||
func RunExec(dockerCli command.Cli, client *apiclient.Client, containerID string,
|
||||
execOptions *container.ExecOptions, stdout bool) (string, error) {
|
||||
execConfig *types.ExecConfig) (io.Writer, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
// We need to check the tty _before_ we do the ContainerExecCreate, because
|
||||
@ -25,68 +24,62 @@ func RunExec(dockerCli command.Cli, client *apiclient.Client, containerID string
|
||||
// there's no easy way to clean those up). But also in order to make "not
|
||||
// exist" errors take precedence we do a dummy inspect first.
|
||||
if _, err := client.ContainerInspect(ctx, containerID); err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
if !execOptions.Detach {
|
||||
if err := dockerCli.In().CheckTty(execOptions.AttachStdin, execOptions.Tty); err != nil {
|
||||
return "", err
|
||||
if !execConfig.Detach {
|
||||
if err := dockerCli.In().CheckTty(execConfig.AttachStdin, execConfig.Tty); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
response, err := client.ContainerExecCreate(ctx, containerID, *execOptions)
|
||||
response, err := client.ContainerExecCreate(ctx, containerID, *execConfig)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
execID := response.ID
|
||||
if execID == "" {
|
||||
return "", errors.New("exec ID empty")
|
||||
return nil, errors.New("exec ID empty")
|
||||
}
|
||||
|
||||
if execOptions.Detach {
|
||||
execStartCheck := container.ExecStartOptions{
|
||||
Detach: execOptions.Detach,
|
||||
Tty: execOptions.Tty,
|
||||
if execConfig.Detach {
|
||||
execStartCheck := types.ExecStartCheck{
|
||||
Detach: execConfig.Detach,
|
||||
Tty: execConfig.Tty,
|
||||
}
|
||||
return "", client.ContainerExecStart(ctx, execID, execStartCheck)
|
||||
return nil, client.ContainerExecStart(ctx, execID, execStartCheck)
|
||||
}
|
||||
return interactiveExec(ctx, dockerCli, client, execOptions, execID, stdout)
|
||||
return interactiveExec(ctx, dockerCli, client, execConfig, execID)
|
||||
}
|
||||
|
||||
func interactiveExec(ctx context.Context, dockerCli command.Cli, client *apiclient.Client,
|
||||
execOpts *container.ExecOptions, execID string, stdout bool) (string, error) {
|
||||
execConfig *types.ExecConfig, execID string) (io.Writer, error) {
|
||||
// Interactive exec requested.
|
||||
var (
|
||||
out, stderr io.Writer
|
||||
outBuffer *bytes.Buffer
|
||||
in io.ReadCloser
|
||||
)
|
||||
|
||||
if execOpts.AttachStdin {
|
||||
if execConfig.AttachStdin {
|
||||
in = dockerCli.In()
|
||||
}
|
||||
if execOpts.AttachStdout {
|
||||
if stdout {
|
||||
out = dockerCli.Out()
|
||||
} else {
|
||||
outBuffer = new(bytes.Buffer)
|
||||
out = outBuffer
|
||||
}
|
||||
if execConfig.AttachStdout {
|
||||
out = dockerCli.Out()
|
||||
}
|
||||
if execOpts.AttachStderr {
|
||||
if execOpts.Tty {
|
||||
if execConfig.AttachStderr {
|
||||
if execConfig.Tty {
|
||||
stderr = dockerCli.Out()
|
||||
} else {
|
||||
stderr = dockerCli.Err()
|
||||
}
|
||||
}
|
||||
|
||||
execStartCheck := container.ExecStartOptions{
|
||||
Tty: execOpts.Tty,
|
||||
execStartCheck := types.ExecStartCheck{
|
||||
Tty: execConfig.Tty,
|
||||
}
|
||||
resp, err := client.ContainerExecAttach(ctx, execID, execStartCheck)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return out, err
|
||||
}
|
||||
defer resp.Close()
|
||||
|
||||
@ -101,15 +94,15 @@ func interactiveExec(ctx context.Context, dockerCli command.Cli, client *apiclie
|
||||
outputStream: out,
|
||||
errorStream: stderr,
|
||||
resp: resp,
|
||||
tty: execOpts.Tty,
|
||||
detachKeys: execOpts.DetachKeys,
|
||||
tty: execConfig.Tty,
|
||||
detachKeys: execConfig.DetachKeys,
|
||||
}
|
||||
|
||||
return streamer.stream(ctx)
|
||||
}()
|
||||
}()
|
||||
|
||||
if execOpts.Tty && dockerCli.In().IsTerminal() {
|
||||
if execConfig.Tty && dockerCli.In().IsTerminal() {
|
||||
if err := MonitorTtySize(ctx, client, dockerCli, execID, true); err != nil {
|
||||
fmt.Fprintln(dockerCli.Err(), "Error monitoring TTY size:", err)
|
||||
}
|
||||
@ -117,14 +110,10 @@ func interactiveExec(ctx context.Context, dockerCli command.Cli, client *apiclie
|
||||
|
||||
if err := <-errCh; err != nil {
|
||||
log.Debugf("Error hijack: %s", err)
|
||||
return "", err
|
||||
return out, err
|
||||
}
|
||||
|
||||
if execOpts.AttachStdout {
|
||||
return outBuffer.String(), getExecExitStatus(ctx, client, execID)
|
||||
}
|
||||
|
||||
return "", getExecExitStatus(ctx, client, execID)
|
||||
return out, getExecExitStatus(ctx, client, execID)
|
||||
}
|
||||
|
||||
func getExecExitStatus(ctx context.Context, client apiclient.ContainerAPIClient, execID string) error {
|
||||
|
Reference in New Issue
Block a user