From e6afe9fcdd0e18dbb866a62706d013ccf666b51c Mon Sep 17 00:00:00 2001 From: Assil Ksiksi Date: Wed, 15 Nov 2023 18:33:39 -0500 Subject: [PATCH] fix volume opts flags --- helpers.go | 9 +++++++++ template.go | 1 + templates/volume.nix.tmpl | 6 +++--- testdata/TestDocker_RemoveVolumes_out.nix | 6 +++--- testdata/TestDocker_SystemdMount_out.nix | 6 +++--- testdata/TestDocker_WithProject_out.nix | 6 +++--- testdata/TestDocker_out.nix | 6 +++--- 7 files changed, 25 insertions(+), 15 deletions(-) diff --git a/helpers.go b/helpers.go index 6a54d48..2dc187c 100644 --- a/helpers.go +++ b/helpers.go @@ -26,6 +26,15 @@ func mapToRepeatedKeyValFlag(flagName string, m map[string]string) []string { return arr } +func mapToRepeatedFlag(flagName string, m map[string]string) []string { + var flags []string + for k, v := range m { + flags = append(flags, fmt.Sprintf("%s=%s=%s", flagName, k, v)) + } + slices.Sort(flags) + return flags +} + func ReadEnvFiles(envFiles []string, mergeWithEnv bool) (env []string, _ error) { for _, p := range envFiles { if strings.TrimSpace(p) == "" { diff --git a/template.go b/template.go index 47d3e16..98cea75 100644 --- a/template.go +++ b/template.go @@ -37,6 +37,7 @@ func toNixValue(v any) any { var funcMap template.FuncMap = template.FuncMap{ "derefInt": derefInt, "mapToKeyValArray": mapToKeyValArray, + "mapToRepeatedFlag": mapToRepeatedFlag, "mapToRepeatedKeyValFlag": mapToRepeatedKeyValFlag, "toNixValue": toNixValue, } diff --git a/templates/volume.nix.tmpl b/templates/volume.nix.tmpl index 78ad49f..d0770a6 100644 --- a/templates/volume.nix.tmpl +++ b/templates/volume.nix.tmpl @@ -1,4 +1,4 @@ -{{- $driverOptsString := mapToKeyValArray .DriverOpts | join "," -}} +{{- $driverOpts := mapToRepeatedFlag "--opt" .DriverOpts | join " " -}} {{- $labels := mapToRepeatedKeyValFlag "--label" .Labels -}} systemd.services."{{.Runtime}}-volume-{{.Name}}" = { path = [ pkgs.{{.Runtime}} ]; @@ -11,9 +11,9 @@ systemd.services."{{.Runtime}}-volume-{{.Name}}" = { }; script = '' {{- if eq .Driver ""}} - {{.Runtime}} volume inspect {{.Name}} || {{.Runtime}} volume create {{.Name}} --opt {{$driverOptsString}}{{- if $labels}} {{ $labels | join " "}}{{- end}} + {{.Runtime}} volume inspect {{.Name}} || {{.Runtime}} volume create {{.Name}}{{- if $driverOpts}} {{$driverOpts}}{{- end}}{{- if $labels}} {{ $labels | join " "}}{{- end}} {{- else}} - {{.Runtime}} volume inspect {{.Name}} || {{.Runtime}} volume create {{.Name}} --driver {{.Driver}} --opt {{$driverOptsString}}{{- if $labels}} {{ $labels | join " "}}{{- end}} + {{.Runtime}} volume inspect {{.Name}} || {{.Runtime}} volume create {{.Name}} --driver {{.Driver}}{{- if $driverOpts}} {{$driverOpts}}{{- end}}{{- if $labels}} {{ $labels | join " "}}{{- end}} {{- end}} ''; {{- if rootTarget}} diff --git a/testdata/TestDocker_RemoveVolumes_out.nix b/testdata/TestDocker_RemoveVolumes_out.nix index 00e4393..69648a8 100644 --- a/testdata/TestDocker_RemoveVolumes_out.nix +++ b/testdata/TestDocker_RemoveVolumes_out.nix @@ -319,7 +319,7 @@ ExecStop = "${pkgs.docker}/bin/docker volume rm -f books"; }; script = '' - docker volume inspect books || docker volume create books --opt device=/mnt/media/Books,o=bind,type=none + docker volume inspect books || docker volume create books --opt=device=/mnt/media/Books --opt=o=bind --opt=type=none ''; partOf = [ "docker-compose-myproject-root.target" ]; wantedBy = [ "docker-compose-myproject-root.target" ]; @@ -332,7 +332,7 @@ ExecStop = "${pkgs.docker}/bin/docker volume rm -f photos"; }; script = '' - docker volume inspect photos || docker volume create photos --opt device=/mnt/photos,o=bind,type=none --label=test-label=okay + docker volume inspect photos || docker volume create photos --opt=device=/mnt/photos --opt=o=bind --opt=type=none --label=test-label=okay ''; partOf = [ "docker-compose-myproject-root.target" ]; wantedBy = [ "docker-compose-myproject-root.target" ]; @@ -345,7 +345,7 @@ ExecStop = "${pkgs.docker}/bin/docker volume rm -f storage"; }; script = '' - docker volume inspect storage || docker volume create storage --opt device=/mnt/media,o=bind,type=none + docker volume inspect storage || docker volume create storage --opt=device=/mnt/media --opt=o=bind --opt=type=none ''; partOf = [ "docker-compose-myproject-root.target" ]; wantedBy = [ "docker-compose-myproject-root.target" ]; diff --git a/testdata/TestDocker_SystemdMount_out.nix b/testdata/TestDocker_SystemdMount_out.nix index 6e4c693..0d04786 100644 --- a/testdata/TestDocker_SystemdMount_out.nix +++ b/testdata/TestDocker_SystemdMount_out.nix @@ -326,7 +326,7 @@ RemainAfterExit = true; }; script = '' - docker volume inspect books || docker volume create books --opt device=/mnt/media/Books,o=bind,type=none + docker volume inspect books || docker volume create books --opt=device=/mnt/media/Books --opt=o=bind --opt=type=none ''; partOf = [ "docker-compose-myproject-root.target" ]; wantedBy = [ "docker-compose-myproject-root.target" ]; @@ -338,7 +338,7 @@ RemainAfterExit = true; }; script = '' - docker volume inspect photos || docker volume create photos --opt device=/mnt/photos,o=bind,type=none --label=test-label=okay + docker volume inspect photos || docker volume create photos --opt=device=/mnt/photos --opt=o=bind --opt=type=none --label=test-label=okay ''; partOf = [ "docker-compose-myproject-root.target" ]; wantedBy = [ "docker-compose-myproject-root.target" ]; @@ -350,7 +350,7 @@ RemainAfterExit = true; }; script = '' - docker volume inspect storage || docker volume create storage --opt device=/mnt/media,o=bind,type=none + docker volume inspect storage || docker volume create storage --opt=device=/mnt/media --opt=o=bind --opt=type=none ''; partOf = [ "docker-compose-myproject-root.target" ]; wantedBy = [ "docker-compose-myproject-root.target" ]; diff --git a/testdata/TestDocker_WithProject_out.nix b/testdata/TestDocker_WithProject_out.nix index 55b94e2..ac8f1dd 100644 --- a/testdata/TestDocker_WithProject_out.nix +++ b/testdata/TestDocker_WithProject_out.nix @@ -318,7 +318,7 @@ RemainAfterExit = true; }; script = '' - docker volume inspect books || docker volume create books --opt device=/mnt/media/Books,o=bind,type=none + docker volume inspect books || docker volume create books --opt=device=/mnt/media/Books --opt=o=bind --opt=type=none ''; partOf = [ "docker-compose-myproject-root.target" ]; wantedBy = [ "docker-compose-myproject-root.target" ]; @@ -330,7 +330,7 @@ RemainAfterExit = true; }; script = '' - docker volume inspect photos || docker volume create photos --opt device=/mnt/photos,o=bind,type=none --label=test-label=okay + docker volume inspect photos || docker volume create photos --opt=device=/mnt/photos --opt=o=bind --opt=type=none --label=test-label=okay ''; partOf = [ "docker-compose-myproject-root.target" ]; wantedBy = [ "docker-compose-myproject-root.target" ]; @@ -342,7 +342,7 @@ RemainAfterExit = true; }; script = '' - docker volume inspect storage || docker volume create storage --opt device=/mnt/media,o=bind,type=none + docker volume inspect storage || docker volume create storage --opt=device=/mnt/media --opt=o=bind --opt=type=none ''; partOf = [ "docker-compose-myproject-root.target" ]; wantedBy = [ "docker-compose-myproject-root.target" ]; diff --git a/testdata/TestDocker_out.nix b/testdata/TestDocker_out.nix index 9bd36cf..c6f8b31 100644 --- a/testdata/TestDocker_out.nix +++ b/testdata/TestDocker_out.nix @@ -313,7 +313,7 @@ RemainAfterExit = true; }; script = '' - docker volume inspect books || docker volume create books --opt device=/mnt/media/Books,o=bind,type=none + docker volume inspect books || docker volume create books --opt=device=/mnt/media/Books --opt=o=bind --opt=type=none ''; partOf = [ "docker-compose-myproject-root.target" ]; wantedBy = [ "docker-compose-myproject-root.target" ]; @@ -325,7 +325,7 @@ RemainAfterExit = true; }; script = '' - docker volume inspect photos || docker volume create photos --opt device=/mnt/photos,o=bind,type=none --label=test-label=okay + docker volume inspect photos || docker volume create photos --opt=device=/mnt/photos --opt=o=bind --opt=type=none --label=test-label=okay ''; partOf = [ "docker-compose-myproject-root.target" ]; wantedBy = [ "docker-compose-myproject-root.target" ]; @@ -337,7 +337,7 @@ RemainAfterExit = true; }; script = '' - docker volume inspect storage || docker volume create storage --opt device=/mnt/media,o=bind,type=none + docker volume inspect storage || docker volume create storage --opt=device=/mnt/media --opt=o=bind --opt=type=none ''; partOf = [ "docker-compose-myproject-root.target" ]; wantedBy = [ "docker-compose-myproject-root.target" ];