-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from syncromatics/feature/ps-command
Added support for ps command
- Loading branch information
Showing
8 changed files
with
206 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace LclDckr.Commands.Ps | ||
{ | ||
public class ContainerInfo | ||
{ | ||
public string ContainerId { get; set; } | ||
public string Image { get; set; } | ||
public string Command { get; set; } | ||
public string Created { get; set; } | ||
public string Status { get; set; } | ||
public string Ports { get; set; } | ||
public List<string> Names { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace LclDckr.Commands.Ps | ||
{ | ||
/// <summary> | ||
/// Parses output of Ps command | ||
/// </summary> | ||
internal class ContainerInfoParser | ||
{ | ||
private readonly string[] _expectedHeaders = | ||
{ | ||
"CONTAINER ID", | ||
"IMAGE", | ||
"COMMAND", | ||
"CREATED", | ||
"STATUS", | ||
"PORTS", | ||
"NAMES" | ||
}; | ||
|
||
private readonly Tuple<int, int>[] _fieldLocations; | ||
|
||
public ContainerInfoParser(string headers) | ||
{ | ||
_fieldLocations = new Tuple<int, int>[_expectedHeaders.Length]; | ||
|
||
for (int i = 0; i < _expectedHeaders.Length; i++) | ||
{ | ||
var idRegex = new Regex($"({_expectedHeaders[i]}) *"); | ||
var match = idRegex.Match(headers); | ||
if (!match.Success) | ||
{ | ||
throw new Exception($"Returned headers from ps command did not match expected headers {headers}"); | ||
} | ||
|
||
_fieldLocations[i] = Tuple.Create(match.Index, match.Length); | ||
} | ||
} | ||
|
||
public ContainerInfo Parse(string fields) | ||
{ | ||
Func<int, string> getField = i => fields.Substring(_fieldLocations[i].Item1, i < _fieldLocations.Length - 1 ? _fieldLocations[i].Item2 : fields.Length - _fieldLocations[i].Item1); | ||
|
||
return new ContainerInfo | ||
{ | ||
ContainerId = getField(0).Trim(), | ||
Image = getField(1).Trim(), | ||
Command = getField(2).Trim(), | ||
Created = getField(3).Trim(), | ||
Status = getField(4).Trim(), | ||
Ports = getField(5).Trim(), | ||
Names = getField(6).Trim().Split(',').ToList() | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace LclDckr.Commands.Ps.Filters | ||
{ | ||
public interface IFilter | ||
{ | ||
string Value { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace LclDckr.Commands.Ps.Filters | ||
{ | ||
public class NameFilter : IFilter | ||
{ | ||
public string Name { get; set; } | ||
public string Value => $"name={Name}"; | ||
|
||
public NameFilter() | ||
{ | ||
|
||
} | ||
|
||
public NameFilter(string name) | ||
{ | ||
Name = name; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters