MCPcopy Create free account
hub / github.com/1Panel-dev/1Panel / runContainerCommandRaw

Function runContainerCommandRaw

agent/app/service/container.go:1513–1554  ·  view source on GitHub ↗
(cli *client.Client, containerID string, command []string)

Source from the content-addressed store, hash-verified

1511}
1512
1513func 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
1556func runContainerCommandStream(cli *client.Client, containerID string, command []string) (io.ReadCloser, error) {
1557 ctx := context.Background()

Callers 2

runContainerCommandFunction · 0.85

Calls 3

CloseMethod · 0.65
StringMethod · 0.45
BytesMethod · 0.45

Tested by

no test coverage detected