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

Unified Diff: cipd/client/cipd/ensure/item_parsers.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/good_test.go ('k') | cipd/client/cipd/ensure/package_def.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cipd/client/cipd/ensure/item_parsers.go
diff --git a/cipd/client/cipd/ensure/item_parsers.go b/cipd/client/cipd/ensure/item_parsers.go
new file mode 100644
index 0000000000000000000000000000000000000000..7e6c8ac2a190df504d0a2538dd7285865a049876
--- /dev/null
+++ b/cipd/client/cipd/ensure/item_parsers.go
@@ -0,0 +1,47 @@
+// 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"
+ "net/url"
+)
+
+// an itemParser should parse the value from `val`, and update s or
+// f accordingly, returning an error if needed.
+type itemParser func(s *itemParserState, f *File, val string) error
+
+// itemParserState is the state object shared between the item parsers and the
+// main ParseFile implementation.
+type itemParserState struct {
+ curRoot string
+}
+
+func rootParser(s *itemParserState, _ *File, val string) error {
+ if err := ValidateRoot(val); err != nil {
+ return err
+ }
+ s.curRoot = val
+ return nil
+}
+
+func serviceURLParser(_ *itemParserState, f *File, val string) error {
+ if f.ServiceURL != "" {
+ return fmt.Errorf("$ServiceURL may only be set once per file")
+ }
+ if _, err := url.Parse(val); err != nil {
+ return fmt.Errorf("expecting '$ServiceURL <url>' but url is invalid: %s", err)
+ }
+ f.ServiceURL = val
+ return nil
+}
+
+// itemParsers is the main way that the ensure file format is extended. If you
+// need to add a new setting or directive, please add an appropriate function
+// above and then add it to this map.
+var itemParsers = map[string]itemParser{
+ "@root": rootParser,
+ "$serviceurl": serviceURLParser,
+}
« no previous file with comments | « cipd/client/cipd/ensure/good_test.go ('k') | cipd/client/cipd/ensure/package_def.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698