| OLD | NEW |
| (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 config | |
| 6 | |
| 7 import ( | |
| 8 "errors" | |
| 9 "flag" | |
| 10 "fmt" | |
| 11 ) | |
| 12 | |
| 13 // ProjectName is a luci-config project name. | |
| 14 // | |
| 15 // A valid project name may only include: | |
| 16 // - Lowercase letters [a-z] | |
| 17 // - Numbers [0-9] | |
| 18 // - Hyphen (-) | |
| 19 // - Underscore (_) | |
| 20 // | |
| 21 // It also must begin with a letter. | |
| 22 // | |
| 23 // See: | |
| 24 // https://github.com/luci/luci-py/blob/8e594074929871a9761d27e814541bc0d7d84744
/appengine/components/components/config/common.py#L41 | |
| 25 type ProjectName string | |
| 26 | |
| 27 var _ flag.Value = (*ProjectName)(nil) | |
| 28 | |
| 29 // Validate returns an error if the supplied ProjectName is not a valid project | |
| 30 // name. | |
| 31 func (p ProjectName) Validate() error { | |
| 32 if len(p) == 0 { | |
| 33 return errors.New("cannot have empty name") | |
| 34 } | |
| 35 | |
| 36 for idx, r := range p { | |
| 37 switch { | |
| 38 case r >= 'a' && r <= 'z': | |
| 39 | |
| 40 case (r >= '0' && r <= '9'), r == '-', r == '_': | |
| 41 if idx == 0 { | |
| 42 return errors.New("must begin with a letter") | |
| 43 } | |
| 44 | |
| 45 default: | |
| 46 return fmt.Errorf("invalid character at %d (%c)", idx, r
) | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 return nil | |
| 51 } | |
| 52 | |
| 53 // String implements flag.Value. | |
| 54 func (p *ProjectName) String() string { | |
| 55 return string(*p) | |
| 56 } | |
| 57 | |
| 58 // Set implements flag.Value. | |
| 59 func (p *ProjectName) Set(v string) error { | |
| 60 vp := ProjectName(v) | |
| 61 if err := vp.Validate(); err != nil { | |
| 62 return err | |
| 63 } | |
| 64 *p = vp | |
| 65 return nil | |
| 66 } | |
| OLD | NEW |