~themue/juju-core/053-env-more-script-friendly

« back to all changes in this revision

Viewing changes to names/unit.go

  • Committer: Dimiter Naydenov
  • Date: 2013-07-29 15:15:41 UTC
  • mto: This revision was merged to the branch mainline in revision 1565.
  • Revision ID: dimiter.naydenov@canonical.com-20130729151541-zm8murwo9u7mtsdu
names: new package

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package names
 
5
 
 
6
import (
 
7
        "fmt"
 
8
        "regexp"
 
9
        "strings"
 
10
)
 
11
 
 
12
var validUnit = regexp.MustCompile("^" + ServiceSnippet + "/" + NumberSnippet + "$")
 
13
 
 
14
// UnitTag returns the tag for the unit with the given name.
 
15
func UnitTag(unitName string) string {
 
16
        return UnitTagPrefix + strings.Replace(unitName, "/", "-", -1)
 
17
}
 
18
 
 
19
// UnitNameFromTag returns the unit name that was used to create the tag.
 
20
func UnitNameFromTag(tag string) (string, error) {
 
21
        if !strings.HasPrefix(tag, UnitTagPrefix) {
 
22
                return "", fmt.Errorf("invalid unit tag format: %v", tag)
 
23
        }
 
24
        // Strip off the "unit-" prefix.
 
25
        name := tag[len(UnitTagPrefix):]
 
26
        // Put the slashes back.
 
27
        name = strings.Replace(name, "-", "/", -1)
 
28
        return name, nil
 
29
}
 
30
 
 
31
// IsUnitName returns whether name is a valid unit name.
 
32
func IsUnitName(name string) bool {
 
33
        return validUnit.MatchString(name)
 
34
}