-
Notifications
You must be signed in to change notification settings - Fork 0
/
variables.go
78 lines (68 loc) · 1.56 KB
/
variables.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
package common
import (
"errors"
"fmt"
"os"
"strings"
)
type BuildVariable struct {
Key string `json:"key"`
Value string `json:"value"`
Public bool `json:"public"`
Internal bool `json:"-"`
File bool `json:"-"`
}
type BuildVariables []BuildVariable
func (b BuildVariable) String() string {
return fmt.Sprintf("%s=%s", b.Key, b.Value)
}
func (b BuildVariables) PublicOrInternal() (variables BuildVariables) {
for _, variable := range b {
if variable.Public || variable.Internal {
variables = append(variables, variable)
}
}
return variables
}
func (b BuildVariables) StringList() (variables []string) {
for _, variable := range b {
variables = append(variables, variable.String())
}
return variables
}
func (b BuildVariables) Get(key string) string {
switch key {
case "$":
return key
case "*", "#", "@", "!", "?", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9":
return ""
}
for i := len(b) - 1; i >= 0; i-- {
if b[i].Key == key {
return b[i].Value
}
}
return ""
}
func (b BuildVariables) ExpandValue(value string) string {
return os.Expand(value, b.Get)
}
func (b BuildVariables) Expand() (variables BuildVariables) {
for _, variable := range b {
variable.Value = b.ExpandValue(variable.Value)
variables = append(variables, variable)
}
return variables
}
func ParseVariable(text string) (variable BuildVariable, err error) {
keyValue := strings.SplitN(text, "=", 2)
if len(keyValue) != 2 {
err = errors.New("missing =")
return
}
variable = BuildVariable{
Key: keyValue[0],
Value: keyValue[1],
}
return
}