-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDockerFile.cs
69 lines (57 loc) · 1.57 KB
/
DockerFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Non-nullable member is uninitialized
#pragma warning disable CS8618
// ReSharper disable All
// Example from https://mitesh1612.github.io/blog/2021/08/11/how-to-design-fluent-api.
using System.Text;
using M31.FluentApi.Attributes;
namespace ExampleProject;
[FluentApi]
public class DockerFile
{
private readonly StringBuilder content;
private DockerFile()
{
content = new StringBuilder();
}
[FluentMethod(0)]
private void FromImage(string imageName)
{
content.AppendLine($"FROM {imageName}");
}
[FluentMethod(1)]
[FluentContinueWith(1)]
private void CopyFiles(string source, string destination)
{
content.AppendLine($"COPY {source} {destination}");
}
[FluentMethod(1)]
[FluentContinueWith(1)]
private void RunCommand(string command)
{
content.AppendLine($"RUN {command}");
}
[FluentMethod(1)]
[FluentContinueWith(1)]
private void ExposePort(int port)
{
content.AppendLine($"EXPOSE {port}");
}
[FluentMethod(1)]
[FluentContinueWith(1)]
private void WithEnvironmentVariable(string variableName, string variableValue)
{
content.AppendLine($"ENV {variableName}={variableValue}");
}
[FluentMethod(1)]
private void WithCommand(string command)
{
var commandList = command.Split(" ");
content.Append("CMD [ ");
content.Append(string.Join(", ", commandList.Select(c => $"\"{c}\"")));
content.Append(']');
}
public override string ToString()
{
return content.ToString();
}
}