~rogpeppe/juju-core/438-local-instance-Addresses

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
package formula

import (
	"fmt"
	"os"
	"strconv"
	"strings"
)

// The Formula interface is implemented by any type that
// may be handled as a formula.
type Formula interface {
	Meta() *Meta
	Config() *Config
}

// ParseId splits a formula identifier into its constituting parts.
func ParseId(id string) (namespace string, name string, rev int, err os.Error) {
	colon := strings.Index(id, ":")
	if colon == -1 {
		err = errorf("Missing formula namespace: %q", id)
		return
	}
	dash := strings.LastIndex(id, "-")
	if dash != -1 {
		rev, err = strconv.Atoi(id[dash+1:])
	}
	if dash == -1 || err != nil {
		err = errorf("Missing formula revision: %q", id)
		return
	}
	namespace = id[:colon]
	name = id[colon+1 : dash]
	return
}

func errorf(format string, args ...interface{}) os.Error {
	return os.NewError(fmt.Sprintf(format, args...))
}