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

Function runContainerCommandStream

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

Source from the content-addressed store, hash-verified

1554}
1555
1556func runContainerCommandStream(cli *client.Client, containerID string, command []string) (io.ReadCloser, error) {
1557 ctx := context.Background()
1558 resp, err := cli.ContainerExecCreate(ctx, containerID, container.ExecOptions{
1559 Cmd: command,
1560 AttachStdout: true,
1561 AttachStderr: true,
1562 })
1563 if err != nil {
1564 return nil, err
1565 }
1566 hijack, err := cli.ContainerExecAttach(ctx, resp.ID, container.ExecAttachOptions{})
1567 if err != nil {
1568 return nil, err
1569 }
1570
1571 pipeReader, pipeWriter := io.Pipe()
1572 go func() {
1573 defer hijack.Close()
1574 var stderr bytes.Buffer
1575 _, copyErr := stdcopy.StdCopy(pipeWriter, &stderr, hijack.Reader)
1576 if copyErr != nil {
1577 _ = pipeWriter.CloseWithError(copyErr)
1578 return
1579 }
1580 info, inspectErr := cli.ContainerExecInspect(ctx, resp.ID)
1581 if inspectErr != nil {
1582 _ = pipeWriter.CloseWithError(inspectErr)
1583 return
1584 }
1585 if info.ExitCode != 0 {
1586 msg := strings.TrimSpace(stderr.String())
1587 if len(msg) == 0 {
1588 msg = fmt.Sprintf("container command failed with exit code %d", info.ExitCode)
1589 }
1590 _ = pipeWriter.CloseWithError(fmt.Errorf("%s", msg))
1591 return
1592 }
1593 _ = pipeWriter.Close()
1594 }()
1595 return pipeReader, nil
1596}
1597
1598func toContainerFileInfo(filePath string, stat container.PathStat, isDir bool) dto.ContainerFileInfo {
1599 name := stat.Name

Callers 1

DownloadContainerFileMethod · 0.85

Calls 2

CloseMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected