From ad1abe1ea90642b3343f03350b9f67644083a069 Mon Sep 17 00:00:00 2001 From: Dario Tranchitella Date: Mon, 18 Jul 2022 10:06:19 +0200 Subject: [PATCH] feat: utilities for lookups and mangling --- internal/utilities/args.go | 67 +++++++++++++++++++++++++++++++++ internal/utilities/container.go | 17 +++++++++ internal/utilities/volumes.go | 28 ++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 internal/utilities/args.go create mode 100644 internal/utilities/container.go create mode 100644 internal/utilities/volumes.go diff --git a/internal/utilities/args.go b/internal/utilities/args.go new file mode 100644 index 0000000..e5e7db3 --- /dev/null +++ b/internal/utilities/args.go @@ -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 +} diff --git a/internal/utilities/container.go b/internal/utilities/container.go new file mode 100644 index 0000000..89a8cc4 --- /dev/null +++ b/internal/utilities/container.go @@ -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 +} diff --git a/internal/utilities/volumes.go b/internal/utilities/volumes.go new file mode 100644 index 0000000..9cca630 --- /dev/null +++ b/internal/utilities/volumes.go @@ -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 +}