go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/config/project_name.go (about) 1 // Copyright 2016 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package config 16 17 import ( 18 "errors" 19 "fmt" 20 ) 21 22 // ValidateProjectName returns an error if the supplied string is not a valid 23 // project name. 24 // 25 // A valid project name may only include: 26 // - Lowercase letters [a-z] 27 // - Numbers [0-9] 28 // - Hyphen (-) 29 // - Underscore (_) 30 // 31 // It also must begin with a letter. 32 // 33 // See: 34 // https://github.com/luci/luci-py/blob/8e594074929871a9761d27e814541bc0d7d84744/appengine/components/components/config/common.py#L41 35 func ValidateProjectName(p string) error { 36 if len(p) == 0 { 37 return errors.New("cannot have empty name") 38 } 39 40 for idx, r := range p { 41 switch { 42 case r >= 'a' && r <= 'z': 43 44 case (r >= '0' && r <= '9'), r == '-', r == '_': 45 if idx == 0 { 46 return errors.New("must begin with a letter") 47 } 48 49 default: 50 return fmt.Errorf("invalid character at %d (%c)", idx, r) 51 } 52 } 53 54 return nil 55 }