dqsw7529 2018-08-28 15:18
浏览 308
已采纳

如何使用多个参数将-ldflags传递给exec.Command

I have a fairly simple go file which builds several internal tools written in go. It worked well until I decided to add -ldflags to my build command. The following is the snippet which formats the go build command.

var cmd *exec.Cmd
file := fmt.Sprintf("%s.%s.%s", p.Bin, e.OS, e.Arch)
if len(p.Flags) > 0 {
    ldflags := ""
    for _, f := range p.Flags {
        if len(ldflags) > 0 {
            ldflags = ldflags + " "
        }
        ldflags = ldflags + f.Flag
    }
    ldflags = "\"" + ldflags + "\""
    fmt.Println("go", "build", "-v", "-a", "-ldflags", ldflags, "-o", fmt.Sprintf("/tmp/bin/%s", file), ".")
    cmd = exec.Command("go", "build", "-a", "-ldflags", ldflags, "-o", fmt.Sprintf("/tmp/bin/%s", file), ".")
} else {
    fmt.Println("go", "build", "-v", "-a", "-o", fmt.Sprintf("/tmp/bin/%s", file), ".")
    cmd = exec.Command("go", "build", "-a", "-o", fmt.Sprintf("/tmp/bin/%s", file), ".")
        }
cmd.Dir = p.Pkg
cmd.Stdout = ioutil.Discard
cmd.Stderr = os.Stdout
cmd.Env = append(cleanEnv(),
    fmt.Sprintf("GOOS=%s", e.OS),
    fmt.Sprintf("GOARCH=%s", e.Arch),
)

if err := cmd.Run(); err != nil {
    return err
}

My flags are defined as simple strings like so

[]flagarg{
    {Flag:"-X main.buildstamp=`date -u '+%Y-%m-%d_%I:%M:%S%p'`"},
    {Flag:"-X main.githash=`git rev-parse --short HEAD`"},
}

The following is the output when I print

go build -v -a -ldflags "-X main.buildstamp=`date -u
'+%Y-%m-%d_%I:%M:%S%p'` -X main.githash=`git rev-parse --short HEAD`"
-o /tmp/bin/bro.linux.amd64 .

The above command works when I paste it into my CLI but fails when I run this go build via my go script. When I say fail I mean that it's not setting my variables githash and buildstamp. Copying and pasting the command does set these variables as expected.

I figured it must be something with the quotes and I have tried changing them around but I am unable to get things to work. I am starting to think I am heading down the wrong path and that it must be a better way to get this working.

  • 写回答

1条回答 默认 最新

  • dongxun6690 2018-08-28 15:25
    关注

    The issue is likely with the command substitution (backticks); it's not doing what you expect because it's a feature of the shell, not the "go build" command.

    []flagarg{
        {Flag:"-X main.buildstamp=`date -u '+%Y-%m-%d_%I:%M:%S%p'`"},
        // Bash command subst ----^------------------------------^
    

    Try executing your printed command line as a single string argument to "bash -c", e.g.:

    cmd = exec.Command("bash", "-c", "go build -v -a ...")
    

    You also might want to consider using the $(...) form of bash command substitution (I think it's easier to read), here's a simple example to demonstrate:

    cmdline := "echo \"The time is now '$(date)'!\""
    out, err := exec.Command("bash", "-c", cmdline).Output()
    if err != nil {
        panic(err)
    }
    fmt.Println(string(out))
    // The time is now 'Tue Aug 28 09:33:34 MDT 2018'!
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中mjs用不了
  • ¥15 Ios抖音直播的时候如何添加自定义图片在直播间!
  • ¥60 riscv-pulpino总线上挂载axi从机
  • ¥15 ssh登录页面的问题
  • ¥50 关于在matlab上对曲柄摇杆机构上一点的运动学仿真
  • ¥15 jetson nano
  • ¥15 :app:debugCompileClasspath'.
  • ¥15 windows c++内嵌qt出现数据转换问题。
  • ¥15 stm32 串口通讯过程中的问题
  • ¥20 公众号如何实现点击超链接后自动发送文字