Skip to content

Commit

Permalink
Merge pull request #250 from devedse/master
Browse files Browse the repository at this point in the history
Fixed issue: 249 + implemented multiple filters
  • Loading branch information
mariotoffia authored May 30, 2022
2 parents 2e94090 + ba08d12 commit 462153d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -406,5 +406,41 @@ public void GetContainersShallWork()
&& JsonConvert.SerializeObject(c) == JsonConvert.SerializeObject(container2)));
}
}

[TestMethod]
public void GetContainersShallWorkWithFilterWhenThereIsNoResults()
{
// Arrange
using (var container = _host.Create("postgres:9.6-alpine"))
using (var container2 = _host.Create("postgres:14.2-alpine"))
{
container.Start();

// Act
var result = _host.GetContainers(true, "ancestor==somethingnotexisting");

// Assert
Assert.AreEqual(0, result.Count);
}
}

[TestMethod]
public void GetContainersShallWorkWithFilterWhenThereIsResults()
{
// Arrange
using (var container = _host.Create("postgres:9.6-alpine"))
using (var container2 = _host.Create("postgres:14.2-alpine"))
{
container.Start();

// Act
var result = _host.GetContainers(true, "ancestor==postgres:14.2-alpine");

// Assert
Assert.AreEqual(1, result.Count);
Assert.IsNotNull(result.SingleOrDefault(c => c.Id == container2.Id
&& JsonConvert.SerializeObject(c) == JsonConvert.SerializeObject(container2)));
}
}
}
}
14 changes: 9 additions & 5 deletions Ductus.FluentDocker/Commands/Client.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using Ductus.FluentDocker.Executors;
Expand Down Expand Up @@ -99,13 +99,17 @@ public static CommandResponse<IList<string>> Build(this DockerUri host, string n
$"{host.RenderBaseArgs(certificates)} build {options} {workdir}").Execute();
}

public static CommandResponse<IList<DockerImageRowResponse>> Images(this DockerUri host, string filter = null,
ICertificatePaths certificates = null)
public static CommandResponse<IList<DockerImageRowResponse>> Images(this DockerUri host, ICertificatePaths certificates = null,
params string[] filters)
{
var options = "--quiet --no-trunc --format \"{{.ID}};{{.Repository}};{{.Tag}}\"";
if (!string.IsNullOrEmpty(filter))

foreach (var filter in filters)
{
options += $" --filter=\"{filter}\"";
if (!string.IsNullOrEmpty(filter))
{
options += $" --filter=\"{filter}\"";
}
}

return
Expand Down
6 changes: 3 additions & 3 deletions Ductus.FluentDocker/Services/IHostService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Net;
using Ductus.FluentDocker.Common;
Expand Down Expand Up @@ -43,9 +43,9 @@ public interface IHostService : IService
/// </remarks>
IList<IContainerService> GetRunningContainers();

IList<IContainerService> GetContainers(bool all = true, string filter = null);
IList<IContainerService> GetContainers(bool all = true, params string[] filters);

IList<IContainerImageService> GetImages(bool all = true, string filer = null);
IList<IContainerImageService> GetImages(bool all = true, params string[] filters);

/// <summary>
/// Creates a new container (not started).
Expand Down
17 changes: 10 additions & 7 deletions Ductus.FluentDocker/Services/Impl/DockerHostService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -129,17 +129,20 @@ public IList<IContainerService> GetRunningContainers()
return GetContainers(false);
}

public IList<IContainerService> GetContainers(bool all = true, string filter = null)
public IList<IContainerService> GetContainers(bool all = true, params string[] filters)
{
var options = string.Empty;
if (all)
options += " --all";

if (!string.IsNullOrEmpty(filter))
options += $" --filter {filter}";
foreach (var filter in filters)
{
if (!string.IsNullOrEmpty(filter))
options += $" --filter {filter}";
}

var psResult = Host.Ps(options, Certificates);
if (!psResult.Success)
if (!psResult.Success || psResult.Data.Count == 0)
return new List<IContainerService>();

var ids = psResult.Data.ToArray();
Expand All @@ -155,9 +158,9 @@ public IList<IContainerService> GetContainers(bool all = true, string filter = n
.Cast<IContainerService>().ToList();
}

public IList<IContainerImageService> GetImages(bool all = true, string filter = null)
public IList<IContainerImageService> GetImages(bool all = true, params string[] filters)
{
var images = Host.Images(filter, Certificates);
var images = Host.Images(Certificates, filters);
if (!images.Success)
return new List<IContainerImageService>();

Expand Down

0 comments on commit 462153d

Please sign in to comment.