blob: aa8985e102678054d4a7bb27dfd69bc2dded72d9 [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 "regexp"
8)
9
10const ApplicationTagKind = "application"
11
12const (
13 ApplicationSnippet = "(?:[a-z][a-z0-9]*(?:-[a-z0-9]*[a-z][a-z0-9]*)*)"
14 NumberSnippet = "(?:0|[1-9][0-9]*)"
15)
16
17var validApplication = regexp.MustCompile("^" + ApplicationSnippet + "$")
18
19// IsValidApplication returns whether name is a valid application name.
20func IsValidApplication(name string) bool {
21 return validApplication.MatchString(name)
22}
23
24type ApplicationTag struct {
25 Name string
26}
27
28func (t ApplicationTag) String() string { return t.Kind() + "-" + t.Id() }
29func (t ApplicationTag) Kind() string { return ApplicationTagKind }
30func (t ApplicationTag) Id() string { return t.Name }
31
32// NewApplicationTag returns the tag for the application with the given name.
33func NewApplicationTag(applicationName string) ApplicationTag {
34 return ApplicationTag{Name: applicationName}
35}
36
37// ParseApplicationTag parses a application tag string.
38func 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}