(cli *client.Client, containerID string, command []string)
| 1511 | } |
| 1512 | |
| 1513 | func runContainerCommandRaw(cli *client.Client, containerID string, command []string) ([]byte, error) { |
| 1514 | ctx := context.Background() |
| 1515 | resp, err := cli.ContainerExecCreate(ctx, containerID, container.ExecOptions{ |
| 1516 | Cmd: command, |
| 1517 | AttachStdout: true, |
| 1518 | AttachStderr: true, |
| 1519 | }) |
| 1520 | if err != nil { |
| 1521 | return nil, err |
| 1522 | } |
| 1523 | hijack, err := cli.ContainerExecAttach(ctx, resp.ID, container.ExecAttachOptions{}) |
| 1524 | if err != nil { |
| 1525 | return nil, err |
| 1526 | } |
| 1527 | defer hijack.Close() |
| 1528 | |
| 1529 | raw, err := io.ReadAll(hijack.Reader) |
| 1530 | if err != nil { |
| 1531 | return nil, err |
| 1532 | } |
| 1533 | var stdout bytes.Buffer |
| 1534 | var stderr bytes.Buffer |
| 1535 | if _, err := stdcopy.StdCopy(&stdout, &stderr, bytes.NewReader(raw)); err != nil { |
| 1536 | return nil, err |
| 1537 | } |
| 1538 | output := strings.TrimSpace(stdout.String()) |
| 1539 | errorOutput := strings.TrimSpace(stderr.String()) |
| 1540 | info, err := cli.ContainerExecInspect(ctx, resp.ID) |
| 1541 | if err != nil { |
| 1542 | return nil, err |
| 1543 | } |
| 1544 | if info.ExitCode != 0 { |
| 1545 | if len(errorOutput) != 0 { |
| 1546 | return nil, fmt.Errorf("%s", errorOutput) |
| 1547 | } |
| 1548 | if len(output) == 0 { |
| 1549 | return nil, fmt.Errorf("container command failed with exit code %d", info.ExitCode) |
| 1550 | } |
| 1551 | return nil, fmt.Errorf("%s", output) |
| 1552 | } |
| 1553 | return stdout.Bytes(), nil |
| 1554 | } |
| 1555 | |
| 1556 | func runContainerCommandStream(cli *client.Client, containerID string, command []string) (io.ReadCloser, error) { |
| 1557 | ctx := context.Background() |
no test coverage detected