-
Notifications
You must be signed in to change notification settings - Fork 84
/
resource.go
39 lines (32 loc) · 888 Bytes
/
resource.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
package kubernetes
import (
"fmt"
"strings"
)
// ResourceLocation defines the location of Kubernetes resource in a particular
// namespace.
type ResourceLocation struct {
Name string
Namespace string
}
// String implements fmt.Stringer.
func (r *ResourceLocation) String() string {
if r == nil {
return ""
}
return fmt.Sprintf("%s/%s", r.Namespace, r.Name)
}
// ParseResourceLocation parses a Kubernetes resource location from string.
// Returns an error if the string does not match the expected format of
// `namespace/name`.
func ParseResourceLocation(s string) (*ResourceLocation, error) {
parts := strings.Split(strings.Trim(s, "/"), "/")
if len(parts) != 2 {
return nil, fmt.Errorf(`invalid resource location, expected format "namespace/name" but got %q`, s)
}
ref := &ResourceLocation{
Namespace: parts[0],
Name: parts[1],
}
return ref, nil
}