Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(573)

Side by Side Diff: deploytool/cmd/param.go

Issue 2182213002: deploytool: Add README.md, migrate docs to it. (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: Rename to "luci_deploy" Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « deploytool/cmd/manage.go ('k') | deploytool/cmd/path.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 }
OLDNEW
« no previous file with comments | « deploytool/cmd/manage.go ('k') | deploytool/cmd/path.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698