2018-06-16 19:22:21 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Builder ...
|
|
|
|
type Builder interface {
|
|
|
|
Build() error
|
|
|
|
Binary() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type builder struct {
|
|
|
|
dir string
|
|
|
|
binary string
|
|
|
|
errors string
|
|
|
|
wd string
|
|
|
|
buildArgs []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBuilder ...
|
|
|
|
func NewBuilder(dir string, bin string, wd string, buildArgs []string) Builder {
|
2018-06-20 20:40:09 -05:00
|
|
|
// resolve bin name by current folder name
|
|
|
|
if bin == "" {
|
|
|
|
bin = filepath.Base(wd)
|
2018-06-16 19:22:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// does not work on Windows without the ".exe" extension
|
|
|
|
if runtime.GOOS == OSWindows {
|
|
|
|
// check if it already has the .exe extension
|
|
|
|
if !strings.HasSuffix(bin, ".exe") {
|
|
|
|
bin += ".exe"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &builder{dir: dir, binary: bin, wd: wd, buildArgs: buildArgs}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Binary ...
|
|
|
|
func (b *builder) Binary() string {
|
|
|
|
return b.binary
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build ...
|
|
|
|
func (b *builder) Build() error {
|
|
|
|
logger.Info("Building program")
|
|
|
|
args := append([]string{"go", "build", "-o", filepath.Join(b.wd, b.binary)}, b.buildArgs...)
|
|
|
|
logger.Debug("Build command", args)
|
|
|
|
|
|
|
|
command := exec.Command(args[0], args[1:]...) // nolint gas
|
|
|
|
command.Dir = b.dir
|
|
|
|
|
|
|
|
output, err := command.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-20 20:40:09 -05:00
|
|
|
if !command.ProcessState.Success() {
|
|
|
|
return fmt.Errorf("error building: %s", output)
|
2018-06-16 19:22:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|