Skip to content

Commit

Permalink
refactor: store labels as map instead of list, Fix 89luca89/distrobox…
Browse files Browse the repository at this point in the history
…#1333

Signed-off-by: Luca Di Maio <[email protected]>
  • Loading branch information
89luca89 committed May 1, 2024
1 parent 428c724 commit 68b04f2
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 28 deletions.
2 changes: 1 addition & 1 deletion cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...),
}
Expand Down
1 change: 1 addition & 0 deletions cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion cmd/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] + "..."
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion pkg/fileutils/file_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 47 additions & 23 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/89luca89/lilipod/pkg/constants"
Expand All @@ -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"`
}
Expand Down Expand Up @@ -147,7 +148,7 @@ func GetDefaultConfig() Config {
Workdir: "/",
Stopsignal: "SIGTERM",
Mounts: []string{},
Labels: []string{},
Labels: map[string]string{},
Entrypoint: []string{"/bin/sh"},
}
}
Expand Down Expand Up @@ -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
}

0 comments on commit 68b04f2

Please sign in to comment.