| OLD | NEW |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. | 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package ensure | 5 package ensure |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "net/url" | 9 "net/url" |
| 10 |
| 11 "github.com/luci/luci-go/cipd/client/cipd/common" |
| 10 ) | 12 ) |
| 11 | 13 |
| 12 // an itemParser should parse the value from `val`, and update s or | 14 // an itemParser should parse the value from `val`, and update s or |
| 13 // f accordingly, returning an error if needed. | 15 // f accordingly, returning an error if needed. |
| 14 type itemParser func(s *itemParserState, f *File, val string) error | 16 type itemParser func(s *itemParserState, f *File, val string) error |
| 15 | 17 |
| 16 // itemParserState is the state object shared between the item parsers and the | 18 // itemParserState is the state object shared between the item parsers and the |
| 17 // main ParseFile implementation. | 19 // main ParseFile implementation. |
| 18 type itemParserState struct { | 20 type itemParserState struct { |
| 19 curRoot string | 21 curRoot string |
| 20 } | 22 } |
| 21 | 23 |
| 22 func rootParser(s *itemParserState, _ *File, val string) error { | 24 func rootParser(s *itemParserState, _ *File, val string) error { |
| 23 » if err := ValidateRoot(val); err != nil { | 25 » if err := common.ValidateRoot(val); err != nil { |
| 24 return err | 26 return err |
| 25 } | 27 } |
| 26 s.curRoot = val | 28 s.curRoot = val |
| 27 return nil | 29 return nil |
| 28 } | 30 } |
| 29 | 31 |
| 30 func serviceURLParser(_ *itemParserState, f *File, val string) error { | 32 func serviceURLParser(_ *itemParserState, f *File, val string) error { |
| 31 if f.ServiceURL != "" { | 33 if f.ServiceURL != "" { |
| 32 return fmt.Errorf("$ServiceURL may only be set once per file") | 34 return fmt.Errorf("$ServiceURL may only be set once per file") |
| 33 } | 35 } |
| 34 if _, err := url.Parse(val); err != nil { | 36 if _, err := url.Parse(val); err != nil { |
| 35 return fmt.Errorf("expecting '$ServiceURL <url>' but url is inva
lid: %s", err) | 37 return fmt.Errorf("expecting '$ServiceURL <url>' but url is inva
lid: %s", err) |
| 36 } | 38 } |
| 37 f.ServiceURL = val | 39 f.ServiceURL = val |
| 38 return nil | 40 return nil |
| 39 } | 41 } |
| 40 | 42 |
| 41 // itemParsers is the main way that the ensure file format is extended. If you | 43 // itemParsers is the main way that the ensure file format is extended. If you |
| 42 // need to add a new setting or directive, please add an appropriate function | 44 // need to add a new setting or directive, please add an appropriate function |
| 43 // above and then add it to this map. | 45 // above and then add it to this map. |
| 44 var itemParsers = map[string]itemParser{ | 46 var itemParsers = map[string]itemParser{ |
| 45 "@root": rootParser, | 47 "@root": rootParser, |
| 46 "$serviceurl": serviceURLParser, | 48 "$serviceurl": serviceURLParser, |
| 47 } | 49 } |
| OLD | NEW |