Skip to content

Commit 68b04f2

Browse files
committed
refactor: store labels as map instead of list, Fix 89luca89/distrobox#1333
Signed-off-by: Luca Di Maio <luca.dimaio1@gmail.com>
1 parent 428c724 commit 68b04f2

File tree

7 files changed

+53
-28
lines changed

7 files changed

+53
-28
lines changed

cmd/create.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func create(cmd *cobra.Command, arguments []string) error {
217217
Workdir: "/",
218218
Stopsignal: stopsignal,
219219
Mounts: append(mount, volume...),
220-
Labels: label,
220+
Labels: utils.ListToMap(label),
221221
// entry point related
222222
Entrypoint: append(configEntrypoint, args...),
223223
}

cmd/inspect.go

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func inspect(cmd *cobra.Command, arguments []string) error {
6363

6464
format = strings.ReplaceAll(format, ".State.Status", ".Status")
6565
format = strings.ReplaceAll(format, ".Config.Env", ".Env")
66+
format = strings.ReplaceAll(format, ".Config.Labels", ".Labels")
6667

6768
var output string
6869

cmd/ps.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func doContainerRow(
191191
}
192192
// continue with table
193193

194-
labels := strings.Join(config.Labels, ",")
194+
labels := strings.Join(utils.MapToList(config.Labels), ",")
195195
if len(labels) > 16 && !notrunc {
196196
labels = labels[:15] + "..."
197197
}

cmd/run.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func run(cmd *cobra.Command, arguments []string) error {
209209
Workdir: "/",
210210
Stopsignal: stopsignal,
211211
Mounts: append(mount, volume...),
212-
Labels: label,
212+
Labels: utils.ListToMap(label),
213213
// entry point related
214214
Entrypoint: entrypoint,
215215
}

cmd/update.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func update(cmd *cobra.Command, arguments []string) error {
209209
}
210210

211211
if cmd.Flags().Lookup("label").Changed {
212-
config.Labels = label
212+
config.Labels = utils.ListToMap(label)
213213
}
214214

215215
logging.LogDebug(

pkg/fileutils/file_utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"github.com/89luca89/lilipod/pkg/procutils"
1919
)
2020

21-
// ReadFile will return the content of input file or error.
21+
// eadFile will return the content of input file or error.
2222
// This is a linux-only implementation using syscalls for performance benefits.
2323
func ReadFile(path string) ([]byte, error) {
2424
var stat syscall.Stat_t

pkg/utils/utils.go

+47-23
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111
"os/exec"
1212
"path/filepath"
13+
"strings"
1314
"time"
1415

1516
"github.com/89luca89/lilipod/pkg/constants"
@@ -27,28 +28,28 @@ import (
2728
// oci-registry and images compliant, but doesn't need
2829
// to create oci-compliant containers.
2930
type Config struct {
30-
Env []string `json:"env"`
31-
Cgroup string `json:"cgroup"`
32-
Created string `json:"created"`
33-
Gidmap string `json:"gidmap"`
34-
Hostname string `json:"hostname"`
35-
ID string `json:"id"`
36-
Image string `json:"image"`
37-
Ipc string `json:"ipc"`
38-
Names string `json:"names"`
39-
Network string `json:"network"`
40-
Pid string `json:"pid"`
41-
Privileged bool `json:"privileged"`
42-
Size string `json:"size"`
43-
Status string `json:"status"`
44-
Time string `json:"time"`
45-
Uidmap string `json:"uidmap"`
46-
User string `json:"user"`
47-
Userns string `json:"userns"`
48-
Workdir string `json:"workdir"`
49-
Stopsignal string `json:"stopsignal"`
50-
Mounts []string `json:"mounts"`
51-
Labels []string `json:"labels"`
31+
Env []string `json:"env"`
32+
Cgroup string `json:"cgroup"`
33+
Created string `json:"created"`
34+
Gidmap string `json:"gidmap"`
35+
Hostname string `json:"hostname"`
36+
ID string `json:"id"`
37+
Image string `json:"image"`
38+
Ipc string `json:"ipc"`
39+
Names string `json:"names"`
40+
Network string `json:"network"`
41+
Pid string `json:"pid"`
42+
Privileged bool `json:"privileged"`
43+
Size string `json:"size"`
44+
Status string `json:"status"`
45+
Time string `json:"time"`
46+
Uidmap string `json:"uidmap"`
47+
User string `json:"user"`
48+
Userns string `json:"userns"`
49+
Workdir string `json:"workdir"`
50+
Stopsignal string `json:"stopsignal"`
51+
Mounts []string `json:"mounts"`
52+
Labels map[string]string `json:"labels"`
5253
// entry point related
5354
Entrypoint []string `json:"entrypoint"`
5455
}
@@ -147,7 +148,7 @@ func GetDefaultConfig() Config {
147148
Workdir: "/",
148149
Stopsignal: "SIGTERM",
149150
Mounts: []string{},
150-
Labels: []string{},
151+
Labels: map[string]string{},
151152
Entrypoint: []string{"/bin/sh"},
152153
}
153154
}
@@ -366,3 +367,26 @@ func setupBusybox(dependencies []string) error {
366367

367368
return nil
368369
}
370+
371+
func MapToList(input map[string]string) []string {
372+
result := []string{}
373+
374+
for k, v := range input {
375+
result = append(result, k+"="+v)
376+
}
377+
378+
return result
379+
}
380+
381+
func ListToMap(input []string) map[string]string {
382+
result := map[string]string{}
383+
384+
for _, v := range input {
385+
key := strings.Split(v, "=")[0]
386+
value := strings.Split(v, "=")[1:]
387+
388+
result[key] = strings.Join(value, "=")
389+
}
390+
391+
return result
392+
}

0 commit comments

Comments
 (0)