blob: 3f3a02625e3aa986c15130f7c0ce225c158e0b34 [file] [log] [blame]
David K. Bainbridge528b3182017-01-23 08:51:59 -08001// Copyright 2013 Canonical Ltd.
2// Licensed under the LGPLv3, see LICENCE file for details.
3
4package names
5
6import (
7 "fmt"
8 "regexp"
9 "strings"
10)
11
12const UnitTagKind = "unit"
13
14var validUnit = regexp.MustCompile("^(" + ApplicationSnippet + ")/" + NumberSnippet + "$")
15
16type UnitTag struct {
17 name string
18}
19
20func (t UnitTag) String() string { return t.Kind() + "-" + t.name }
21func (t UnitTag) Kind() string { return UnitTagKind }
22func (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.
26func 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.
35func 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.
48func 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.
54func 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
62func 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
72func 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}