-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.go
More file actions
68 lines (55 loc) · 1.28 KB
/
Copy pathbuilder.go
File metadata and controls
68 lines (55 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"fmt"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
type Builder struct {
dir string
binary string
errors string
useGodep bool
wd string
buildArgs []string
}
// NewBuilder creates new builder
func NewBuilder(dir string, bin string, useGodep bool, wd string, buildArgs []string) *Builder {
if len(bin) == 0 {
bin = "bin"
}
// We need to append .exe in Windows
if runtime.GOOS == "windows" {
if !strings.HasSuffix(bin, ".exe") { // check if it already has the .exe extension
bin += ".exe"
}
}
return &Builder{dir: dir, binary: bin, useGodep: useGodep, wd: wd, buildArgs: buildArgs}
}
func (b *Builder) Binary() string {
return b.binary
}
func (b *Builder) Errors() string {
return b.errors
}
func (b *Builder) Build() error {
args := append([]string{"go", "build", "-o", filepath.Join(b.wd, b.binary)}, b.buildArgs...)
var command *exec.Cmd
if b.useGodep {
args = append([]string{"godep"}, args...)
}
command = exec.Command(args[0], args[1:]...)
output, err := command.CombinedOutput()
if err != nil {
b.errors = err.Error() + ": " + string(output)
} else if command.ProcessState.Success() {
b.errors = ""
} else {
b.errors = string(output)
}
if len(b.errors) > 0 {
return fmt.Errorf(b.errors)
}
return err
}