Build compiles pkg (a Go package path, e.g. "." or "./cmd/app") to out. It uses `go build` in the provided working directory. Context cancellation kills the build.
(ctx context.Context, workDir, pkg, out string)
| 27 | // It uses `go build` in the provided working directory. Context cancellation |
| 28 | // kills the build. |
| 29 | func Build(ctx context.Context, workDir, pkg, out string) error { |
| 30 | if pkg == "" { |
| 31 | pkg = "." |
| 32 | } |
| 33 | var stderr bytes.Buffer |
| 34 | cmd := exec.CommandContext(ctx, "go", "build", "-o", out, pkg) |
| 35 | cmd.Dir = workDir |
| 36 | cmd.Stderr = &stderr |
| 37 | if err := cmd.Run(); err != nil { |
| 38 | return &BuildError{Stderr: stderr.String(), Err: err} |
| 39 | } |
| 40 | return nil |
| 41 | } |