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 | "fmt" |
| 8 | "regexp" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | const UnitTagKind = "unit" |
| 13 | |
| 14 | var validUnit = regexp.MustCompile("^(" + ApplicationSnippet + ")/" + NumberSnippet + "$") |
| 15 | |
| 16 | type UnitTag struct { |
| 17 | name string |
| 18 | } |
| 19 | |
| 20 | func (t UnitTag) String() string { return t.Kind() + "-" + t.name } |
| 21 | func (t UnitTag) Kind() string { return UnitTagKind } |
| 22 | func (t UnitTag) Id() string { return unitTagSuffixToId(t.name) } |
| 23 | |
| 24 | // NewUnitTag returns the tag for the unit with the given name. |
| 25 | // It will panic if the given unit name is not valid. |
| 26 | func NewUnitTag(unitName string) UnitTag { |
| 27 | tag, ok := tagFromUnitName(unitName) |
| 28 | if !ok { |
| 29 | panic(fmt.Sprintf("%q is not a valid unit name", unitName)) |
| 30 | } |
| 31 | return tag |
| 32 | } |
| 33 | |
| 34 | // ParseUnitTag parses a unit tag string. |
| 35 | func ParseUnitTag(unitTag string) (UnitTag, error) { |
| 36 | tag, err := ParseTag(unitTag) |
| 37 | if err != nil { |
| 38 | return UnitTag{}, err |
| 39 | } |
| 40 | ut, ok := tag.(UnitTag) |
| 41 | if !ok { |
| 42 | return UnitTag{}, invalidTagError(unitTag, UnitTagKind) |
| 43 | } |
| 44 | return ut, nil |
| 45 | } |
| 46 | |
| 47 | // IsValidUnit returns whether name is a valid unit name. |
| 48 | func IsValidUnit(name string) bool { |
| 49 | return validUnit.MatchString(name) |
| 50 | } |
| 51 | |
| 52 | // UnitApplication returns the name of the application that the unit is |
| 53 | // associated with. It returns an error if unitName is not a valid unit name. |
| 54 | func UnitApplication(unitName string) (string, error) { |
| 55 | s := validUnit.FindStringSubmatch(unitName) |
| 56 | if s == nil { |
| 57 | return "", fmt.Errorf("%q is not a valid unit name", unitName) |
| 58 | } |
| 59 | return s[1], nil |
| 60 | } |
| 61 | |
| 62 | func tagFromUnitName(unitName string) (UnitTag, bool) { |
| 63 | // Replace only the last "/" with "-". |
| 64 | i := strings.LastIndex(unitName, "/") |
| 65 | if i <= 0 || !IsValidUnit(unitName) { |
| 66 | return UnitTag{}, false |
| 67 | } |
| 68 | unitName = unitName[:i] + "-" + unitName[i+1:] |
| 69 | return UnitTag{name: unitName}, true |
| 70 | } |
| 71 | |
| 72 | func unitTagSuffixToId(s string) string { |
| 73 | // Replace only the last "-" with "/", as it is valid for application |
| 74 | // names to contain hyphens. |
| 75 | if i := strings.LastIndex(s, "-"); i > 0 { |
| 76 | s = s[:i] + "/" + s[i+1:] |
| 77 | } |
| 78 | return s |
| 79 | } |