-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
89 lines (71 loc) · 1.99 KB
/
helpers.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package client
import (
"os/user"
"path"
"regexp"
"strings"
"github.com/rs/zerolog/log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var toFileName = regexp.MustCompile(`[^(\w/\.)]`)
// IsClusterWide returns true if ns designates cluster scope, false otherwise.
func IsClusterWide(ns string) bool {
return ns == NamespaceAll || ns == AllNamespaces || ns == ClusterScope
}
// CleanseNamespace ensures all ns maps to blank.
func CleanseNamespace(ns string) string {
if IsAllNamespace(ns) {
return AllNamespaces
}
return ns
}
// IsAllNamespace returns true if ns == all.
func IsAllNamespace(ns string) bool {
return ns == NamespaceAll
}
// IsAllNamespaces returns true if all namespaces, false otherwise.
func IsAllNamespaces(ns string) bool {
return ns == NamespaceAll || ns == AllNamespaces
}
// IsNamespaced returns true if a specific ns is given.
func IsNamespaced(ns string) bool {
return !IsAllNamespaces(ns)
}
// IsClusterScoped returns true if resource is not namespaced.
func IsClusterScoped(ns string) bool {
return ns == ClusterScope
}
// Namespaced converts a resource path to namespace and resource name.
func Namespaced(p string) (string, string) {
ns, n := path.Split(p)
return strings.Trim(ns, "/"), n
}
// CoFQN returns a fully qualified container name.
func CoFQN(m metav1.ObjectMeta, co string) string {
return MetaFQN(m) + ":" + co
}
// FQN returns a fully qualified resource name.
func FQN(ns, n string) string {
if ns == "" {
return n
}
return ns + "/" + n
}
// MetaFQN returns a fully qualified resource name.
func MetaFQN(m metav1.ObjectMeta) string {
if m.Namespace == "" {
return FQN(ClusterScope, m.Name)
}
return FQN(m.Namespace, m.Name)
}
func mustHomeDir() string {
usr, err := user.Current()
if err != nil {
log.Fatal().Err(err).Msg("Die getting user home directory")
}
return usr.HomeDir
}
func toHostDir(host string) string {
h := strings.Replace(strings.Replace(host, "https://", "", 1), "http://", "", 1)
return toFileName.ReplaceAllString(h, "_")
}