David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame^] | 1 | // Copyright 2013 Canonical Ltd. |
| 2 | // Licensed under the LGPLv3, see LICENCE file for details. |
| 3 | |
| 4 | package names |
| 5 | |
| 6 | import ( |
| 7 | "regexp" |
| 8 | ) |
| 9 | |
| 10 | const ApplicationTagKind = "application" |
| 11 | |
| 12 | const ( |
| 13 | ApplicationSnippet = "(?:[a-z][a-z0-9]*(?:-[a-z0-9]*[a-z][a-z0-9]*)*)" |
| 14 | NumberSnippet = "(?:0|[1-9][0-9]*)" |
| 15 | ) |
| 16 | |
| 17 | var validApplication = regexp.MustCompile("^" + ApplicationSnippet + "$") |
| 18 | |
| 19 | // IsValidApplication returns whether name is a valid application name. |
| 20 | func IsValidApplication(name string) bool { |
| 21 | return validApplication.MatchString(name) |
| 22 | } |
| 23 | |
| 24 | type ApplicationTag struct { |
| 25 | Name string |
| 26 | } |
| 27 | |
| 28 | func (t ApplicationTag) String() string { return t.Kind() + "-" + t.Id() } |
| 29 | func (t ApplicationTag) Kind() string { return ApplicationTagKind } |
| 30 | func (t ApplicationTag) Id() string { return t.Name } |
| 31 | |
| 32 | // NewApplicationTag returns the tag for the application with the given name. |
| 33 | func NewApplicationTag(applicationName string) ApplicationTag { |
| 34 | return ApplicationTag{Name: applicationName} |
| 35 | } |
| 36 | |
| 37 | // ParseApplicationTag parses a application tag string. |
| 38 | func ParseApplicationTag(applicationTag string) (ApplicationTag, error) { |
| 39 | tag, err := ParseTag(applicationTag) |
| 40 | if err != nil { |
| 41 | return ApplicationTag{}, err |
| 42 | } |
| 43 | st, ok := tag.(ApplicationTag) |
| 44 | if !ok { |
| 45 | return ApplicationTag{}, invalidTagError(applicationTag, ApplicationTagKind) |
| 46 | } |
| 47 | return st, nil |
| 48 | } |