mirror of
https://github.com/skoelle/kctl-tui.git
synced 2026-09-17 20:10:24 +00:00
Add internal/kctl and internal/config packages with unit tests
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package kctl
|
||||
|
||||
import "sort"
|
||||
|
||||
// DistinctLabelValues returns the sorted, unique, non-empty values of the
|
||||
// given label key across a set of namespaces (name -> labels map).
|
||||
func DistinctLabelValues(namespaces map[string]map[string]string, labelKey string) []string {
|
||||
seen := map[string]bool{}
|
||||
for _, labels := range namespaces {
|
||||
if v, ok := labels[labelKey]; ok && v != "" {
|
||||
seen[v] = true
|
||||
}
|
||||
}
|
||||
result := make([]string, 0, len(seen))
|
||||
for v := range seen {
|
||||
result = append(result, v)
|
||||
}
|
||||
sort.Strings(result)
|
||||
return result
|
||||
}
|
||||
|
||||
// NamespacesForLabelValue returns the sorted namespace names whose labelKey
|
||||
// matches the given value exactly.
|
||||
func NamespacesForLabelValue(namespaces map[string]map[string]string, labelKey, value string) []string {
|
||||
result := make([]string, 0)
|
||||
for ns, labels := range namespaces {
|
||||
if labels[labelKey] == value {
|
||||
result = append(result, ns)
|
||||
}
|
||||
}
|
||||
sort.Strings(result)
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user