@@ -8,10 +8,16 @@ import (
88 "github.com/Sirupsen/logrus"
99)
1010
11+ var defaultValues = make (map [string ]string )
12+
1113func isNum (c uint8 ) bool {
1214 return c >= '0' && c <= '9'
1315}
1416
17+ func validVariableDefault (c uint8 , line string , pos int ) bool {
18+ return c == ':' && line [pos + 1 ] == '-'
19+ }
20+
1521func validVariableNameChar (c uint8 ) bool {
1622 return c == '_' ||
1723 c >= 'A' && c <= 'Z' ||
@@ -36,6 +42,22 @@ func parseVariable(line string, pos int, mapping func(string) string) (string, i
3642 return mapping (buffer .String ()), pos , true
3743}
3844
45+ func parseDefaultValue (line string , pos int ) (string , int , bool ) {
46+ var buffer bytes.Buffer
47+
48+ for ; pos < len (line ); pos ++ {
49+ c := line [pos ]
50+ if c == '}' {
51+ return buffer .String (), pos - 1 , true
52+ }
53+ err := buffer .WriteByte (c )
54+ if err != nil {
55+ return "" , pos , false
56+ }
57+ }
58+ return "" , 0 , false
59+ }
60+
3961func parseVariableWithBraces (line string , pos int , mapping func (string ) string ) (string , int , bool ) {
4062 var buffer bytes.Buffer
4163
@@ -49,10 +71,13 @@ func parseVariableWithBraces(line string, pos int, mapping func(string) string)
4971 if bufferString == "" {
5072 return "" , 0 , false
5173 }
52-
5374 return mapping (buffer .String ()), pos , true
5475 case validVariableNameChar (c ):
5576 buffer .WriteByte (c )
77+ case validVariableDefault (c , line , pos ):
78+ defaultValue := ""
79+ defaultValue , pos , _ = parseDefaultValue (line , pos + 2 )
80+ defaultValues [buffer .String ()] = defaultValue
5681 default :
5782 return "" , 0 , false
5883 }
@@ -143,10 +168,19 @@ func Interpolate(key string, data *interface{}, environmentLookup EnvironmentLoo
143168 values := environmentLookup .Lookup (s , nil )
144169
145170 if len (values ) == 0 {
171+ if val , ok := defaultValues [s ]; ok {
172+ return val
173+ }
146174 logrus .Warnf ("The %s variable is not set. Substituting a blank string." , s )
147175 return ""
148176 }
149177
178+ if strings .SplitN (values [0 ], "=" , 2 )[1 ] == "" {
179+ if val , ok := defaultValues [s ]; ok {
180+ return val
181+ }
182+ }
183+
150184 // Use first result if many are given
151185 value := values [0 ]
152186
0 commit comments