mirror of
https://github.com/outbackdingo/kamaji.git
synced 2026-01-27 10:19:29 +00:00
feat: utilities for lookups and mangling
This commit is contained in:
67
internal/utilities/args.go
Normal file
67
internal/utilities/args.go
Normal file
@@ -0,0 +1,67 @@
|
||||
// Copyright 2022 Clastix Labs
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package utilities
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ArgsFromSliceToMap transforms a slice of string into a map, simplifying the subsequent mangling.
|
||||
func ArgsFromSliceToMap(args []string) (m map[string]string) {
|
||||
m = make(map[string]string)
|
||||
|
||||
for _, arg := range args {
|
||||
parts := strings.SplitN(arg, "=", 2)
|
||||
|
||||
flag, value := parts[0], ""
|
||||
|
||||
if len(parts) > 1 {
|
||||
value = parts[1]
|
||||
}
|
||||
|
||||
m[flag] = value
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// ArgsFromMapToSlice create the slice of args, and sorting the resulting output in order to make it idempotent.
|
||||
// Along with that, if a flag doesn't have a value, it's presented barely without a value assignment.
|
||||
func ArgsFromMapToSlice(args map[string]string) (slice []string) {
|
||||
for flag, value := range args {
|
||||
if len(value) == 0 {
|
||||
slice = append(slice, flag)
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
slice = append(slice, fmt.Sprintf("%s=%s", flag, value))
|
||||
}
|
||||
|
||||
sort.Strings(slice)
|
||||
|
||||
return slice
|
||||
}
|
||||
|
||||
// ArgsRemoveFlag removes a flag from the arguments map, returning true if found and removed.
|
||||
func ArgsRemoveFlag(args map[string]string, flag string) bool {
|
||||
if _, found := args[flag]; found {
|
||||
delete(args, flag)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// ArgsAddFlagValue performs upsert of a flag in the arguments map, returning true if created.
|
||||
func ArgsAddFlagValue(args map[string]string, flag, value string) bool {
|
||||
_, ok := args[flag]
|
||||
|
||||
args[flag] = value
|
||||
|
||||
return ok == false
|
||||
}
|
||||
17
internal/utilities/container.go
Normal file
17
internal/utilities/container.go
Normal file
@@ -0,0 +1,17 @@
|
||||
// Copyright 2022 Clastix Labs
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package utilities
|
||||
|
||||
import corev1 "k8s.io/api/core/v1"
|
||||
|
||||
// HasNamedContainer finds the Container in the provided slice by its name, returning a boolean if found, and its index.
|
||||
func HasNamedContainer(container []corev1.Container, name string) (found bool, index int) {
|
||||
for i, volume := range container {
|
||||
if volume.Name == name {
|
||||
return true, i
|
||||
}
|
||||
}
|
||||
|
||||
return false, 0
|
||||
}
|
||||
28
internal/utilities/volumes.go
Normal file
28
internal/utilities/volumes.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// Copyright 2022 Clastix Labs
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package utilities
|
||||
|
||||
import corev1 "k8s.io/api/core/v1"
|
||||
|
||||
// HasNamedVolume finds the Volume in the provided slice by its name, returning a boolean if found, and its index.
|
||||
func HasNamedVolume(volumes []corev1.Volume, name string) (found bool, index int) {
|
||||
for i, volume := range volumes {
|
||||
if volume.Name == name {
|
||||
return true, i
|
||||
}
|
||||
}
|
||||
|
||||
return false, 0
|
||||
}
|
||||
|
||||
// HasNamedVolumeMount finds the VolumeMount in the provided slice by its name, returning a boolean if found, and its index.
|
||||
func HasNamedVolumeMount(volumeMounts []corev1.VolumeMount, name string) (found bool, index int) {
|
||||
for i, volume := range volumeMounts {
|
||||
if volume.Name == name {
|
||||
return true, i
|
||||
}
|
||||
}
|
||||
|
||||
return false, 0
|
||||
}
|
||||
Reference in New Issue
Block a user