原始需求
只是一个很简单的需求, 使用golang的exec运行一个命令然后获取实时结果, 命令是
trivy image --download-db-only
正常的打印应该是
2023-08-08T17:06:02.929+0800 INFO Need to update DB
2023-08-08T17:06:02.929+0800 INFO DB Repository: ghcr.io/aquasecurity/trivy-db
2023-08-08T17:06:02.929+0800 INFO Downloading DB...
867.62 KiB / 38.79 MiB [->_____________________________________________________________________] 2.18% 804.52 KiB p/s ETA 48s
然后最下面是个进度条, 随着时间会慢慢的加满, 最后是到100结束
2023-08-08T17:36:33.103+0800 INFO Need to update DB
2023-08-08T17:36:33.103+0800 INFO DB Repository: ghcr.io/aquasecurity/trivy-db
2023-08-08T17:36:33.103+0800 INFO Downloading DB...
38.79 MiB / 38.79 MiB [--------------------------------------------------------------------------] 100.00% 835.73 KiB p/s 48s
然后我就使用普通的 exec.Command
运行并监听其 stderr 和 stdout 通道, 结果发现进度条数据获取不到, 我将每一个数据都记录到了日志文件中, 日志文件如下:
{"level":"warn","ts":"2023-08-08T16:29:04.978+0800","caller":"trivy-db/download.go:46","msg":"命令错误输出","command":"trivy image --download-db-only ","output":"2023-08-08T16:29:04.978+0800\t\u001b[34mINFO\u001b[0m\tNeed to update DB"}
{"level":"warn","ts":"2023-08-08T16:29:04.978+0800","caller":"trivy-db/download.go:46","msg":"命令错误输出","command":"trivy image --download-db-only ","output":"2023-08-08T16:29:04.978+0800\t\u001b[34mINFO\u001b[0m\tDB Repository: ghcr.io/aquasecurity/trivy-db"}
{"level":"warn","ts":"2023-08-08T16:29:04.978+0800","caller":"trivy-db/download.go:46","msg":"命令错误输出","command":"trivy image --download-db-only ","output":"2023-08-08T16:29:04.978+0800\t\u001b[34mINFO\u001b[0m\tDownloading DB..."}
请忽略\t
, [34m
这种特殊字符, 可以看到后续的进度条日志完全未捕捉到, 然后开始了找问题之旅, 因为觉得挺有意思所以把过程分享给大家