dtpxi88884 2019-05-03 10:12
浏览 823
已采纳

当gofmt建议更改时,使gofmt以退出状态1退出。

I would like to add gofmt to the CI/CD pipeline. If it produces changes, I want gofmt to exit with status 1.

For example if I run gofmt -s -l . and there are some files listed. I want it to exit with status 1. Right now when I run echo $? gives me 0, even if there are some files listed with gofmt's changes.

I checked the docs and couldn't find a corresponding command line option. Is there a bash hack to do it?

  • 写回答

1条回答 默认 最新

  • doushi9780 2019-05-03 10:20
    关注

    You just need to ensure if the gofmt lists one more lines of output by checking the output of wc -l is non-empty and then run the binary false which sets the exit code to 1 to the shell invoked or use exit 1 to exit out of the script explicitly.

    if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then
        exit 1
    fi
    

    When you say gofmt actually returns $? as 0 even if there are files listed?, in that case you could also simply do

    if gofmt -s -l . > /dev/null; then
        exit 1
    fi
    

    The above if condition relies on the exit code returned by gofmt as 0 would indicate a success of the command, the conditional would assert a true.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 绿盟安全扫描--检测到目标站点存在javascript框架库漏洞
  • ¥30 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包