| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | |
| 3 // that can be found in the LICENSE file. | |
| 4 | |
| 5 package main | |
| 6 | |
| 7 import ( | |
| 8 "regexp" | |
| 9 "strings" | |
| 10 | |
| 11 "github.com/luci/luci-go/common/errors" | |
| 12 ) | |
| 13 | |
| 14 // paramRE is the regular expression for deployment parameter substitution. | |
| 15 // | |
| 16 // Keys can contain alphanumeric letters plus underscores and periods. | |
| 17 // Substitution is represented as ${KEY}. | |
| 18 var paramRE = regexp.MustCompile(`\${([a-zA-Z0-9_.]+)}`) | |
| 19 | |
| 20 // substitute substitutes any parameter expressions with a value from the | |
| 21 // supplied substitution map. If a parameter is referenced but not defined, | |
| 22 // an error will be returned. | |
| 23 func substitute(vp *string, subs map[string]string) error { | |
| 24 var ( | |
| 25 v = *vp | |
| 26 matches = paramRE.FindAllStringSubmatchIndex(v, -1) | |
| 27 parts = make([]string, 0, (len(matches)*2)+1) | |
| 28 sidx = 0 | |
| 29 ) | |
| 30 for _, m := range matches { | |
| 31 // m will have 4 entries: | |
| 32 // v[m[0]:m[1]] is the variable expression that was matched. | |
| 33 // v[m[2]:m[3]] is the paramter key (first capture group) | |
| 34 parts = append(parts, v[sidx:m[0]]) | |
| 35 sidx = m[1] | |
| 36 | |
| 37 key := v[m[2]:m[3]] | |
| 38 if val, ok := subs[key]; ok { | |
| 39 parts = append(parts, val) | |
| 40 } else { | |
| 41 // panic to immediately stop matching. This will be caug
ht at the top of | |
| 42 // this function. | |
| 43 return errors.Reason("undefined parameter %(key)q").D("k
ey", key).Err() | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 // Finish the string and return. | |
| 48 parts = append(parts, v[sidx:]) | |
| 49 *vp = strings.Join(parts, "") | |
| 50 return nil | |
| 51 } | |
| OLD | NEW |