我有一个应用程序附加到 docker 容器以使用 containerAttach() 获取其输出docker库提供的功能。 该函数返回 HijackedResponse带有指向 bufio.Reader 的指针的结构。
我正在尝试将文本从 bufio.Reader 流式传输到 stdout,并在写入 stdout 的字符串中获取意外字符。
代码:
_, err := io.Copy(os.Stdout, hijackedResponse.Reader)
预期输出:
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but
will not be persisted to local or remote state storage.
实际输出:
6Refreshing Terraform state in-memory prior to plan...
=The refreshed state will be used to calculate this plan, but
9will not be persisted to local or remote state storage.
我不知道每一行中的第一个字符来自哪里。任何帮助将非常感激。如果需要,我可以提供有关我在代码中使用的 docker 容器和附加选项的更多详细信息,尽管我假设它们没问题,因为我正在通过阅读器获取输出。
最佳答案
我发现了问题 - 我的 containerConfig需要指定 Tty: true
关于golang - 使用 io.Copy 将奇怪的字符从 bufio.Reader 复制到 STDOUT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42475239/