/
helpers.go
90 lines (79 loc) · 1.69 KB
/
helpers.go
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"errors"
"fmt"
"github.com/juju/deputy"
"log"
"os"
"os/exec"
)
func validation(str ...interface{}) {
log.SetPrefix("docker-build: ")
log.Println(str...)
os.Exit(1)
}
func internalError(err error) {
log.Println(err)
os.Exit(1)
}
func done(str ...interface{}) {
if len(str) > 0 {
fmt.Println(str...)
}
os.Exit(0)
}
type Variable struct {
Cmd string `yaml:"cmd,omitempty"`
Value string `yaml:"value,omitempty"`
}
var (
ErrCmdAndValue = errors.New("A variable can not contain both a cmd and value entry")
ErrNoCmdOrValue = errors.New("A variable must have a command or a value")
ErrNoCommand = errors.New("No command to execute")
)
func parseVariables(variables map[string]*Variable) (map[string]string, error) {
parsed := map[string]string{}
for name, v := range variables {
if len(v.Cmd) > 0 && len(v.Value) > 0 {
return nil, ErrCmdAndValue
}
switch {
case len(v.Cmd) > 0:
value, err := execute(v.Cmd)
if err != nil {
return nil, err
}
parsed[name] = value
case len(v.Value) > 0:
parsed[name] = v.Value
default:
return nil, ErrNoCmdOrValue
}
}
return parsed, nil
}
func execute(cmd string) (string, error) {
if len(cmd) == 0 {
return "", ErrNoCommand
}
var value string
d := deputy.Deputy{
Errors: deputy.FromStderr,
StdoutLog: func(b []byte) {
value = string(b)
},
}
if err := d.Run(exec.Command("sh", "-c", cmd)); err != nil {
return "", err
}
return value, nil
}
func nodeFile(buildDir, node string) string {
var nodeFile string
if node == "" {
nodeFile = fmt.Sprintf("%s/docker-build.yml", buildDir)
} else {
nodeFile = fmt.Sprintf("%s/docker-build-%s.yml", buildDir, node)
}
return nodeFile
}