diff --git a/src/Adeotek.Extensions.Containers/ContainersCli.cs b/src/Adeotek.Extensions.Containers/ContainersCli.cs index ac226b2..ccec1a6 100644 --- a/src/Adeotek.Extensions.Containers/ContainersCli.cs +++ b/src/Adeotek.Extensions.Containers/ContainersCli.cs @@ -262,14 +262,7 @@ public int CreateVolume(string volumeName, bool dryRun = false) public int CreateBindVolume(VolumeConfig volume, bool dryRun = false) { - var changes = 0; - LogCommand("mkdir", volume.Source); - if (!dryRun) - { - Directory.CreateDirectory(volume.Source); - changes++; - } - + var changes = CreateVolumeDirectory(volume.Source, dryRun); if (ShellCommand.IsWindowsPlatform) { return changes; @@ -551,6 +544,27 @@ public bool ArchiveVolume(string volumeName, string archiveFile, bool dryRun = f throw new ContainersCliException("run", 1, $"Unable to archive volume '{volumeName}' into '{archiveFile}'!"); } + protected int CreateVolumeDirectory(string volumeSource, bool dryRun = false) + { + var missingPath = volumeSource.EndsWith(Path.DirectorySeparatorChar) + ? volumeSource[..^1] + : Path.GetDirectoryName(volumeSource); + + if (string.IsNullOrEmpty(missingPath) || Directory.Exists(missingPath)) + { + return 0; + } + + LogCommand("mkdir", missingPath); + if (dryRun) + { + return 0; + } + + Directory.CreateDirectory(missingPath); + return 1; + } + protected void LogMessage(string message, string level = "info") { if (OnContainersCliEvent is null)