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