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 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测