From 68b04f28b30332ffae74e626209a64642407efeb Mon Sep 17 00:00:00 2001 From: Luca Di Maio Date: Wed, 1 May 2024 15:55:26 +0200 Subject: [PATCH] refactor: store labels as map instead of list, Fix https://github.com/89luca89/distrobox/issues/1333 Signed-off-by: Luca Di Maio --- cmd/create.go | 2 +- cmd/inspect.go | 1 + cmd/ps.go | 2 +- cmd/run.go | 2 +- cmd/update.go | 2 +- pkg/fileutils/file_utils.go | 2 +- pkg/utils/utils.go | 70 +++++++++++++++++++++++++------------ 7 files changed, 53 insertions(+), 28 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index 29fce32..98c5f7e 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -217,7 +217,7 @@ func create(cmd *cobra.Command, arguments []string) error { Workdir: "/", Stopsignal: stopsignal, Mounts: append(mount, volume...), - Labels: label, + Labels: utils.ListToMap(label), // entry point related Entrypoint: append(configEntrypoint, args...), } diff --git a/cmd/inspect.go b/cmd/inspect.go index 5c11e80..36dbcd6 100644 --- a/cmd/inspect.go +++ b/cmd/inspect.go @@ -63,6 +63,7 @@ func inspect(cmd *cobra.Command, arguments []string) error { format = strings.ReplaceAll(format, ".State.Status", ".Status") format = strings.ReplaceAll(format, ".Config.Env", ".Env") + format = strings.ReplaceAll(format, ".Config.Labels", ".Labels") var output string diff --git a/cmd/ps.go b/cmd/ps.go index c6ed5a0..2fcaaa2 100644 --- a/cmd/ps.go +++ b/cmd/ps.go @@ -191,7 +191,7 @@ func doContainerRow( } // continue with table - labels := strings.Join(config.Labels, ",") + labels := strings.Join(utils.MapToList(config.Labels), ",") if len(labels) > 16 && !notrunc { labels = labels[:15] + "..." } diff --git a/cmd/run.go b/cmd/run.go index 2f7d4c2..35931cd 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -209,7 +209,7 @@ func run(cmd *cobra.Command, arguments []string) error { Workdir: "/", Stopsignal: stopsignal, Mounts: append(mount, volume...), - Labels: label, + Labels: utils.ListToMap(label), // entry point related Entrypoint: entrypoint, } diff --git a/cmd/update.go b/cmd/update.go index f31a2df..3cf87d9 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -209,7 +209,7 @@ func update(cmd *cobra.Command, arguments []string) error { } if cmd.Flags().Lookup("label").Changed { - config.Labels = label + config.Labels = utils.ListToMap(label) } logging.LogDebug( diff --git a/pkg/fileutils/file_utils.go b/pkg/fileutils/file_utils.go index 534cb33..e430844 100644 --- a/pkg/fileutils/file_utils.go +++ b/pkg/fileutils/file_utils.go @@ -18,7 +18,7 @@ import ( "github.com/89luca89/lilipod/pkg/procutils" ) -// ReadFile will return the content of input file or error. +// eadFile will return the content of input file or error. // This is a linux-only implementation using syscalls for performance benefits. func ReadFile(path string) ([]byte, error) { var stat syscall.Stat_t diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 1111996..d22a002 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -10,6 +10,7 @@ import ( "os" "os/exec" "path/filepath" + "strings" "time" "github.com/89luca89/lilipod/pkg/constants" @@ -27,28 +28,28 @@ import ( // oci-registry and images compliant, but doesn't need // to create oci-compliant containers. type Config struct { - Env []string `json:"env"` - Cgroup string `json:"cgroup"` - Created string `json:"created"` - Gidmap string `json:"gidmap"` - Hostname string `json:"hostname"` - ID string `json:"id"` - Image string `json:"image"` - Ipc string `json:"ipc"` - Names string `json:"names"` - Network string `json:"network"` - Pid string `json:"pid"` - Privileged bool `json:"privileged"` - Size string `json:"size"` - Status string `json:"status"` - Time string `json:"time"` - Uidmap string `json:"uidmap"` - User string `json:"user"` - Userns string `json:"userns"` - Workdir string `json:"workdir"` - Stopsignal string `json:"stopsignal"` - Mounts []string `json:"mounts"` - Labels []string `json:"labels"` + Env []string `json:"env"` + Cgroup string `json:"cgroup"` + Created string `json:"created"` + Gidmap string `json:"gidmap"` + Hostname string `json:"hostname"` + ID string `json:"id"` + Image string `json:"image"` + Ipc string `json:"ipc"` + Names string `json:"names"` + Network string `json:"network"` + Pid string `json:"pid"` + Privileged bool `json:"privileged"` + Size string `json:"size"` + Status string `json:"status"` + Time string `json:"time"` + Uidmap string `json:"uidmap"` + User string `json:"user"` + Userns string `json:"userns"` + Workdir string `json:"workdir"` + Stopsignal string `json:"stopsignal"` + Mounts []string `json:"mounts"` + Labels map[string]string `json:"labels"` // entry point related Entrypoint []string `json:"entrypoint"` } @@ -147,7 +148,7 @@ func GetDefaultConfig() Config { Workdir: "/", Stopsignal: "SIGTERM", Mounts: []string{}, - Labels: []string{}, + Labels: map[string]string{}, Entrypoint: []string{"/bin/sh"}, } } @@ -366,3 +367,26 @@ func setupBusybox(dependencies []string) error { return nil } + +func MapToList(input map[string]string) []string { + result := []string{} + + for k, v := range input { + result = append(result, k+"="+v) + } + + return result +} + +func ListToMap(input []string) map[string]string { + result := map[string]string{} + + for _, v := range input { + key := strings.Split(v, "=")[0] + value := strings.Split(v, "=")[1:] + + result[key] = strings.Join(value, "=") + } + + return result +}