Hello Gophers,
I try to write an automatic builder that embeds the build time and version in the program with the -ldflags -X option, but I have a problem running go install with the arguments.
My program:
package main import "fmt" var ( BuildTime string BuildVersion string ) func main() { fmt.Println("Hello, World :)") fmt.Println("Build time:", BuildTime) fmt.Println("Build version:", BuildVersion) }
My builder:
package main import ( "fmt" "os" "os/exec" "path/filepath" "time" ) var version = "v1.2.3" func main() { Cmd("go", "install", "program") Cmd("program") err := os.Remove(filepath.Join(os.ExpandEnv("$GOPATH"), "bin", "program")) if err != nil { fmt.Println("Remove error:", err) } Cmd("go", "install", "-ldflags", "'-X \"main.BuildTime="+time.Now().Format(time.UnixDate)+ "\" -X \"main.BuildVersion="+version+"\"'", "program") Cmd("program") } func Cmd(args ...string) { fmt.Println("Running:", args) cmd := exec.Command(args[0], args[1:]...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr err := cmd.Run() if err != nil { fmt.Println("Error:", err, args) } }
But when I run my builder I only get the following output (emphasis mine):
Running: [go install program]
Running: [program]
Hello, World :)
Build time:
Build version:
Running: [go install -ldflags '-X "main.BuildTime=Thu Sep 3 23:06:44 CEST 2015" -X "main.BuildVersion=v1.2.3"' program]
# program
flag provided but not defined: -X "main.BuildTime
usage: link [options] main.o
flags and options...
Error: exit status 2 [go install -ldflags '-X "main.BuildTime=Thu Sep 3 23:06:44 CEST 2015" -X "main.BuildVersion=v1.2.3"' program]
Running: [program]
Error: exec: "program": executable file not found in $PATH [program]
I can run this command without problems on my terminal go install -ldflags '-X "main.BuildTime=Thu Sep 3 23:06:44 CEST 2015" -X "main.BuildVersion=v1.2.3"' program but I obviously have some problems in the argument list in my builder.
Does anyone have an idea how this could be fixed?
Thank you
-Martin
$ go install -ldflags -X "main.BuildTime=Fri Sep 4 02:09:39 CEST 2015" -X "main.BuildVersion=v1.2.3" exp/program
can't load package: package main.BuildTime=Fri Sep 4 02:09:39 CEST 2015: cannot find package "main.BuildTime=Fri Sep 4 02:09:39 CEST 2015" in any of: