forked from juju/names
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
48 lines (38 loc) · 1.18 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package names
import (
"regexp"
)
const ServiceTagKind = "service"
const (
ServiceSnippet = "(?:[a-z][a-z0-9]*(?:-[a-z0-9]*[a-z][a-z0-9]*)*)"
NumberSnippet = "(?:0|[1-9][0-9]*)"
)
var validService = regexp.MustCompile("^" + ServiceSnippet + "$")
// IsValidService returns whether name is a valid service name.
func IsValidService(name string) bool {
return validService.MatchString(name)
}
type ServiceTag struct {
Name string
}
func (t ServiceTag) String() string { return t.Kind() + "-" + t.Id() }
func (t ServiceTag) Kind() string { return ServiceTagKind }
func (t ServiceTag) Id() string { return t.Name }
// NewServiceTag returns the tag for the service with the given name.
func NewServiceTag(serviceName string) ServiceTag {
return ServiceTag{Name: serviceName}
}
// ParseServiceTag parses a service tag string.
func ParseServiceTag(serviceTag string) (ServiceTag, error) {
tag, err := ParseTag(serviceTag)
if err != nil {
return ServiceTag{}, err
}
st, ok := tag.(ServiceTag)
if !ok {
return ServiceTag{}, invalidTagError(serviceTag, ServiceTagKind)
}
return st, nil
}