Skip to content

Commit

Permalink
fix volume opts flags
Browse files Browse the repository at this point in the history
  • Loading branch information
aksiksi committed Nov 15, 2023
1 parent 3e07a96 commit e6afe9f
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 15 deletions.
9 changes: 9 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) == "" {
Expand Down
1 change: 1 addition & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
6 changes: 3 additions & 3 deletions templates/volume.nix.tmpl
Original file line number Diff line number Diff line change
@@ -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}} ];
Expand All @@ -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}}
Expand Down
6 changes: 3 additions & 3 deletions testdata/TestDocker_RemoveVolumes_out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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" ];
Expand All @@ -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" ];
Expand All @@ -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" ];
Expand Down
6 changes: 3 additions & 3 deletions testdata/TestDocker_SystemdMount_out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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" ];
Expand All @@ -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" ];
Expand All @@ -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" ];
Expand Down
6 changes: 3 additions & 3 deletions testdata/TestDocker_WithProject_out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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" ];
Expand All @@ -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" ];
Expand All @@ -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" ];
Expand Down
6 changes: 3 additions & 3 deletions testdata/TestDocker_out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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" ];
Expand All @@ -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" ];
Expand All @@ -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" ];
Expand Down

0 comments on commit e6afe9f

Please sign in to comment.