| 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 main | |
| 6 | |
| 7 import ( | |
| 8 "path/filepath" | |
| 9 "unicode" | |
| 10 | |
| 11 "github.com/luci/luci-go/common/errors" | |
| 12 ) | |
| 13 | |
| 14 type title string | |
| 15 | |
| 16 func (t title) validate() error { | |
| 17 if len(t) == 0 { | |
| 18 return errors.New("cannot be empty") | |
| 19 } | |
| 20 | |
| 21 idx := 0 | |
| 22 for _, r := range t { | |
| 23 if !(unicode.IsLetter(r) || unicode.IsNumber(r) || r == '-') { | |
| 24 return errors.Reason("character at %(pos)d (%(char)c) is
not permitted in a title"). | |
| 25 D("pos", idx).D("char", r).Err() | |
| 26 } | |
| 27 idx++ | |
| 28 } | |
| 29 return nil | |
| 30 } | |
| 31 | |
| 32 // titleFromConfigPath returns the title of a configuration item identified by | |
| 33 // the specified configuration file. | |
| 34 // | |
| 35 // If the file was not a valid config path, or the title was not valid, an error | |
| 36 // will be returned. | |
| 37 func titleFromConfigPath(path string) (title, error) { | |
| 38 path = filepath.Base(path) | |
| 39 if filepath.Ext(path) == configExt { | |
| 40 t := title(path[:len(path)-len(configExt)]) | |
| 41 if err := t.validate(); err != nil { | |
| 42 return "", err | |
| 43 } | |
| 44 return t, nil | |
| 45 } | |
| 46 return "", errors.Reason("missing config extension [" + configExt + "]")
.Err() | |
| 47 } | |
| OLD | NEW |