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

当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.

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器