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

Unified Diff: cipd/client/cipd/ensure/package_def.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/item_parsers.go ('k') | cipd/client/cipd/ensure/template.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cipd/client/cipd/ensure/package_def.go
diff --git a/cipd/client/cipd/ensure/package_def.go b/cipd/client/cipd/ensure/package_def.go
new file mode 100644
index 0000000000000000000000000000000000000000..11f6749e5ea0b3ba554e497d60408d18b806ac07
--- /dev/null
+++ b/cipd/client/cipd/ensure/package_def.go
@@ -0,0 +1,71 @@
+// 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 (
+ "fmt"
+
+ "github.com/luci/luci-go/cipd/client/cipd/common"
+ "github.com/luci/luci-go/common/errors"
+)
+
+// PackageDef defines a package line parsed out of an ensure file.
+type PackageDef struct {
+ PackageTemplate string
+ UnresolvedVersion string
+
+ // LineNo is set while parsing an ensure file by the ParseFile method. It is
+ // used by File.Resolve to give additional context if an error occurs.
+ LineNo int
+}
+
+func (p *PackageDef) String() string {
+ return fmt.Sprintf("%s %s", p.PackageTemplate, p.UnresolvedVersion)
+}
+
+// VersionResolver is expected to tranform a {PackageName, Version} tuple into
+// a resolved instance ID.
+//
+// - `pkg` is guaranteed to pass common.ValidatePackageName
+// - `vers` is guaranteed to pass common.ValidateInstanceVersion
+//
+// The returned `instID` will NOT be checked (so that you
+// can provide a pass-through resolver, if you like).
+type VersionResolver func(pkg, vers string) (common.Pin, error)
+
+// Resolve takes a Package definition containing a possibly templated package
+// name, and a possibly unresolved version string and attempts to resolve them
+// into a Pin.
+func (p *PackageDef) Resolve(plat, arch string, rslv VersionResolver) (pin common.Pin, err error) {
+ pin.PackageName, err = expandTemplate(p.PackageTemplate, plat, arch)
+ if err == errSkipTemplate {
+ return
+ }
+ if err != nil {
+ err = errors.Annotate(err).
+ Reason("failed to resolve package template (line %(line)d)").
+ D("line", p.LineNo).
+ Err()
+ return
+ }
+
+ pin, err = rslv(pin.PackageName, p.UnresolvedVersion)
+ if err != nil {
+ err = errors.Annotate(err).
+ Reason("failed to resolve package version (line %(line)d)").
+ D("line", p.LineNo).
+ Err()
+ return
+ }
+
+ return
+}
+
+// PackageSlice is a sortable slice of PackageDef
+type PackageSlice []PackageDef
+
+func (ps PackageSlice) Len() int { return len(ps) }
+func (ps PackageSlice) Less(i, j int) bool { return ps[i].PackageTemplate < ps[j].PackageTemplate }
+func (ps PackageSlice) Swap(i, j int) { ps[i], ps[j] = ps[j], ps[i] }
« no previous file with comments | « cipd/client/cipd/ensure/item_parsers.go ('k') | cipd/client/cipd/ensure/template.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698