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

Unified Diff: cipd/client/cipd/ensure/template.go

Issue 2651863002: Add ensure-file parser package. (Closed)
Patch Set: Address comments Created 3 years, 11 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « cipd/client/cipd/ensure/package_def.go ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cipd/client/cipd/ensure/template.go
diff --git a/cipd/client/cipd/ensure/template.go b/cipd/client/cipd/ensure/template.go
new file mode 100644
index 0000000000000000000000000000000000000000..d581095cc8d3b0feaf4acd3855b4546fc9433476
--- /dev/null
+++ b/cipd/client/cipd/ensure/template.go
@@ -0,0 +1,72 @@
+// Copyright 2017 The LUCI Authors. All rights reserved.
+// Use of this source code is governed under the Apache License, Version 2.0
+// that can be found in the LICENSE file.
+
+package ensure
+
+import (
+ "regexp"
+ "strings"
+
+ "github.com/luci/luci-go/cipd/client/cipd/common"
+ "github.com/luci/luci-go/common/errors"
+)
+
+var templateParm = regexp.MustCompile(`\${[^}]*}`)
+
+var errSkipTemplate = errors.New("this template should be skipped")
+
+// expandTemplate applies template expansion rules to the package template,
+// using the provided platform and arch values. If err == errSkipTemplate, that
+// means that this template does not apply to this platform/arch combination and
+// should be skipped.
+func expandTemplate(template, platform, arch string) (pkg string, err error) {
+ skip := false
+
+ expansionLookup := map[string]string{
+ "platform": platform,
+ "arch": arch,
+ }
+
+ pkg = templateParm.ReplaceAllStringFunc(template, func(parm string) string {
+ // ${...}
+ contents := parm[2 : len(parm)-1]
+
+ varNameValues := strings.SplitN(contents, "=", 2)
+ if len(varNameValues) == 1 {
+ // ${varName}
+ if value, ok := expansionLookup[varNameValues[0]]; ok {
+ return value
+ }
+
+ err = errors.Reason("unknown variable in ${%(contents)s}").
+ D("contents", contents).Err()
+ }
+
+ // ${varName=value,value}
+ ourValue, ok := expansionLookup[varNameValues[0]]
+ if !ok {
+ err = errors.Reason("unknown variable %(parm)q").D("parm", parm).Err()
+ return parm
+ }
+
+ for _, val := range strings.Split(varNameValues[1], ",") {
+ if val == ourValue {
+ return ourValue
+ }
+ }
+ skip = true
+ return parm
+ })
+ if skip {
+ err = errSkipTemplate
+ }
+ if err == nil && strings.ContainsRune(pkg, '$') {
+ err = errors.Reason("unable to process some variables in %(template)q").
+ D("template", template).Err()
+ }
+ if err == nil {
+ err = common.ValidatePackageName(pkg)
+ }
+ return
+}
« no previous file with comments | « cipd/client/cipd/ensure/package_def.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698