Skip to content

Commit

Permalink
Added several dockerfile commands for Issue #177
Browse files Browse the repository at this point in the history
ENV, ARG, ENTRYPOINT, LABEL, USER, VOLUME
  • Loading branch information
mariotoffia committed Mar 17, 2021
1 parent 0074eac commit 276065e
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 0 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"cSpell.words": [
"Ductus",
"dont"
]
}
33 changes: 33 additions & 0 deletions Ductus.FluentDocker/Extensions/EnvironmentExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Ductus.FluentDocker.Common;
using Ductus.FluentDocker.Executors;
using Ductus.FluentDocker.Model.Common;

namespace Ductus.FluentDocker.Extensions
{
Expand Down Expand Up @@ -33,5 +36,35 @@ public static Tuple<string, string> Extract(this string envExpression)

return executor;
}

/// <summary>
/// This function will extract the name value and verify that is is valid. It will then
/// make sure that the value is wrapped inside double quotes if not yet wrapped.
/// </summary>
/// <param name="nameValue">The name=value strings</param>
/// <returns>A list of name=value string with the value wrapped inside double quotes.</returns>
public static IList<string> WrapValue(this TemplateString[] nameValue)
{

var list = new List<string>();
foreach (var s in nameValue.Select(s => s.Rendered))
{

var index = s.IndexOf('=');
if (-1 == index)
{
throw new FluentDockerException(
$"Expected format name=value, missing equal sign in the name value string: '{s}'"
);
}

var name = s.Substring(0, index);
var value = s.Substring(index + 1, s.Length - index - 1).WrapWithChar("\"");

list.Add($"{name}={value}");
}

return list;
}
}
}
26 changes: 26 additions & 0 deletions Ductus.FluentDocker/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

namespace Ductus.FluentDocker.Extensions {
public static class StringExtensions {

/// <summary>
/// This function will wrap the string s with string c (start and end) if
/// not already existant. If already exist, it will leave it, hence it
/// do not double wrap.
/// </summary>
/// <param name="s">The string to wrap.</param>
/// <param name="c">The string to check and wrap with if not existing.</param>
/// <returns>The wrapped string.</returns>
public static string WrapWithChar(this string s, string c) {

if (!s.StartsWith(c)) {
s = c + s;
}

if (!s.EndsWith(c)) {
s = s + c;
}

return s;
}
}
}
41 changes: 41 additions & 0 deletions Ductus.FluentDocker/Model/Builders/FileBuilder/ArgCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Ductus.FluentDocker.Common;
using Ductus.FluentDocker.Extensions;
using Ductus.FluentDocker.Model.Common;

namespace Ductus.FluentDocker.Model.Builders.FileBuilder
{
public sealed class ArgCommand : ICommand
{
public ArgCommand(TemplateString name, TemplateString defaultValue = null)
{
if (null == name || string.IsNullOrEmpty(name.Rendered))
{
throw new FluentDockerException("Must, at least, specify the argument name in a ARG");
}


Name = name.Rendered;

if (null != defaultValue && !string.IsNullOrEmpty(defaultValue.Rendered))
{
DefaultValue = defaultValue.Rendered;
}
}

public string Name { get; }
public string DefaultValue { get; }

public override string ToString()
{
if (string.IsNullOrEmpty(DefaultValue))
{
return $"ARG {Name}";
}

return $"ARG {Name}={DefaultValue}";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace Ductus.FluentDocker.Model.Builders.FileBuilder
{
public sealed class EntrypointCommand : ICommand
{
public EntrypointCommand(string executable, params string[] args)
{
Executable = executable;
Arguments = args ?? Array.Empty<string>();
}

public string Executable { get; }
public string[] Arguments { get; }

public override string ToString()
{
return $"ENTRYPOINT [\"{Executable}{string.Join("\",\"", Arguments)}\"]";
}
}
}
29 changes: 29 additions & 0 deletions Ductus.FluentDocker/Model/Builders/FileBuilder/EnvCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Ductus.FluentDocker.Extensions;
using Ductus.FluentDocker.Model.Common;
using System.Linq;

namespace Ductus.FluentDocker.Model.Builders.FileBuilder
{
public sealed class EnvCommand : ICommand
{
public EnvCommand(params TemplateString[] nameValue)
{
if (nameValue == null || 0 == nameValue.Length) {
NameValue = new string[0];
} else {
NameValue = NameValue = nameValue.WrapValue().ToArray();
}
}

public string[] NameValue { get; internal set; }

public override string ToString()
{
if (0 == NameValue.Length) {
return "";
}

return $"ENV {string.Join(" ", NameValue)}";
}
}
}
33 changes: 33 additions & 0 deletions Ductus.FluentDocker/Model/Builders/FileBuilder/LabelCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Ductus.FluentDocker.Extensions;
using Ductus.FluentDocker.Model.Common;
using System.Linq;

namespace Ductus.FluentDocker.Model.Builders.FileBuilder
{
public sealed class LabelCommand : ICommand
{
public LabelCommand(params TemplateString[] nameValue)
{
if (nameValue == null || 0 == nameValue.Length)
{
NameValue = new string[0];
}
else
{
NameValue = nameValue.WrapValue().ToArray();
}
}

public string[] NameValue { get; internal set; }

public override string ToString()
{
if (0 == NameValue.Length)
{
return "";
}

return $"LABEL {string.Join(" ", NameValue)}";
}
}
}
41 changes: 41 additions & 0 deletions Ductus.FluentDocker/Model/Builders/FileBuilder/UserCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Ductus.FluentDocker.Common;
using Ductus.FluentDocker.Extensions;
using Ductus.FluentDocker.Model.Common;

namespace Ductus.FluentDocker.Model.Builders.FileBuilder
{
public sealed class UserCommand : ICommand
{
public UserCommand(TemplateString user, TemplateString group = null)
{
if (null == user || string.IsNullOrEmpty(user.Rendered))
{
throw new FluentDockerException("Must specify username or user id");
}


User = user.Rendered;

if (null != group && !string.IsNullOrEmpty(group.Rendered))
{
Group = group.Rendered;
}
}

public string User { get; }
public string Group { get; }

public override string ToString()
{
if (string.IsNullOrEmpty(Group))
{
return $"USER {User}";
}

return $"USER {User}:{Group}";
}
}
}
30 changes: 30 additions & 0 deletions Ductus.FluentDocker/Model/Builders/FileBuilder/VolumeCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Ductus.FluentDocker.Extensions;
using Ductus.FluentDocker.Model.Common;

namespace Ductus.FluentDocker.Model.Builders.FileBuilder
{
public sealed class VolumeCommand : ICommand
{
public VolumeCommand(params TemplateString[] mountpoints)
{
var list = new List<string>();

foreach (var s in mountpoints.Select(s => s.Rendered))
{
list.Add(s.WrapWithChar("\""));
}

Mountpoints = list.ToArray();
}

public string[] Mountpoints { get; }

public override string ToString()
{
return $"VOLUME [\"{string.Join(",", Mountpoints)}\"]";
}
}
}

0 comments on commit 276065e

Please sign in to comment.