Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add service.withPrivileged #74

Merged
merged 8 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/pr-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
- opened
- synchronize
- reopened
merge_group:

jobs:
unit-test:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>10.0</LangVersion>
<LangVersion>11.0</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
93 changes: 67 additions & 26 deletions src/DockerComposeBuilder.Tests/ComposeBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ public void EmptyComposeBuilderTest()
var result = compose.Serialize();

Assert.Equal(
@"version: ""3.8""
", result
"""
version: "3.8"

""",
result
);
}

Expand All @@ -35,11 +38,41 @@ public void ServiceWithImageTest()
var result = compose.Serialize();

Assert.Equal(
@"version: ""3.8""
services:
a-service:
image: ""aviationexam/a-service""
", result);
"""
version: "3.8"
services:
a-service:
image: "aviationexam/a-service"

""",
result
);
}

[Fact]
public void PrivilegedServiceWithImageTest()
{
var compose = Builder.MakeCompose()
.WithServices(Builder.MakeService("a-service")
.WithImage("aviationexam/a-service")
.WithPrivileged()
.Build()
)
.Build();

var result = compose.Serialize();

Assert.Equal(
"""
version: "3.8"
services:
a-service:
image: "aviationexam/a-service"
privileged: true

""",
result
);
}

[Fact]
Expand All @@ -58,13 +91,17 @@ public void ServiceWithBuildTest()
var result = compose.Serialize();

Assert.Equal(
@"version: ""3.8""
services:
a-service:
image: ""aviationexam/a-service""
build:
dockerfile: ""a.dockerfile""
", result);
"""
version: "3.8"
services:
a-service:
image: "aviationexam/a-service"
build:
dockerfile: "a.dockerfile"

""",
result
);
}

[Fact]
Expand All @@ -89,17 +126,21 @@ public void ServiceWithBuildArgumentsTest()
var result = compose.Serialize();

Assert.Equal(
@"version: ""3.8""
services:
a-service:
image: ""aviationexam/a-service""
build:
context: "".""
dockerfile: ""a.dockerfile""
args:
- ""ENV_1""
- ""ENV_2=value""
- ""ENV_3=value""
", result);
"""
version: "3.8"
services:
a-service:
image: "aviationexam/a-service"
build:
context: "."
dockerfile: "a.dockerfile"
args:
- "ENV_1"
- "ENV_2=value"
- "ENV_3=value"

""",
result
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>10</LangVersion>
<LangVersion>11</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/DockerComposeBuilder/Builders/ComposeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public ComposeBuilder WithVolumes(params Volume[] volumes)

foreach (var volume in volumes)
{
if (WorkingObject.Volumes.ContainsKey(volume.Name))
if (WorkingObject.Volumes!.ContainsKey(volume.Name))
{
throw new Exception("Volume name already added to volumes, please pick a unique one!");
}
Expand Down
8 changes: 7 additions & 1 deletion src/DockerComposeBuilder/Builders/ServiceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ServiceBuilder WithDependencies(params string[] services)
{
WorkingObject.DependsOn ??= new List<string>();

var dependsOnList = WorkingObject.DependsOn as List<string>;
var dependsOnList = WorkingObject.DependsOn;

dependsOnList?.AddRange(services);
return this;
Expand Down Expand Up @@ -129,6 +129,12 @@ public ServiceBuilder WithImage(string image)
return this;
}

public ServiceBuilder WithPrivileged(bool? privileged = true)
{
WorkingObject.Privileged = privileged;
return this;
}

public ServiceBuilder WithNetworks(params string[] networks)
{
if (WorkingObject.Networks == null)
Expand Down
2 changes: 1 addition & 1 deletion src/DockerComposeBuilder/Builders/Services/BuildBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace DockerComposeBuilder.Builders.Services;

public class BuildBuilder : BaseBuilder<BuildBuilder, ServiceBuild>
{
private BuildArgumentBuilder? _buildArgumentBuilder = null;
private BuildArgumentBuilder? _buildArgumentBuilder;

public BuildBuilder WithContext(string context)
{
Expand Down
3 changes: 2 additions & 1 deletion src/DockerComposeBuilder/DockerComposeBuilder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
<PackageProjectUrl>https://github.com/aviationexam/docker-compose-builder</PackageProjectUrl>
<RepositoryUrl>https://github.com/aviationexam/docker-compose-builder</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<LangVersion>10.0</LangVersion>
<LangVersion>11.0</LangVersion>
<PackageTags>Docker Docker-Compose Builder</PackageTags>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Nullable>annotations</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="PolySharp" Version="1.13.2" PrivateAssets="all" />
<PackageReference Include="YamlDotNet" Version="13.7.1" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions src/DockerComposeBuilder/Emitters/FlowStringEnumConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ IEventEmitter nextEmitter

public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
{
if (eventInfo.Source.Type.IsEnum && eventInfo.Source.Value is { } value)
if (eventInfo.Source.Type is { IsEnum: true } sourceType && eventInfo.Source.Value is { } value)
{
var enumMember = eventInfo.Source.Type.GetMember(eventInfo.Source.Value.ToString()).FirstOrDefault();
var enumMember = sourceType.GetMember(value.ToString()!).FirstOrDefault();
var yamlValue = enumMember?.GetCustomAttributes<EnumMemberAttribute>(true).Select(ema => ema.Value).FirstOrDefault() ?? value.ToString();

eventInfo = new ScalarEventInfo(new ObjectDescriptor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public override bool EnterMapping(IPropertyDescriptor key, IObjectDescriptor val

if (value.Value == null)
{
return retVal;
return false;
}

if (value.Value is IEnumerable enumerableObject)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace DockerComposeBuilder.Extensions
{
public static class ComposeExtensions
{
public static string Serialize(this Compose serializable)
public static string Serialize(this Compose serializable, string lineEndings = "\n")
{
var serializer = new SerializerBuilder()
.WithTypeConverter(new YamlValueCollectionConverter())
Expand All @@ -16,6 +16,7 @@ public static string Serialize(this Compose serializable)
.WithEventEmitter(nextEmitter => new FlowStringEnumConverter(nextEmitter))
.WithEventEmitter(nextEmitter => new ForceQuotedStringValuesEventEmitter(nextEmitter))
.WithEmissionPhaseObjectGraphVisitor(args => new YamlIEnumerableSkipEmptyObjectGraphVisitor(args.InnerVisitor))
.WithNewLine(lineEndings)
.Build();

return serializer.Serialize(serializable);
Expand Down
11 changes: 8 additions & 3 deletions src/DockerComposeBuilder/Model/Base/ObjectBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using DockerComposeBuilder.Interfaces;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using YamlDotNet.Serialization;

namespace DockerComposeBuilder.Model.Base
Expand All @@ -9,7 +10,7 @@ namespace DockerComposeBuilder.Model.Base
public class ObjectBase : Dictionary<string, object>, IObject
{
[YamlIgnore]
public string Name { get; set; }
public string Name { get; set; } = null!;

public T SetProperty<T>(string property, T value)
{
Expand All @@ -23,7 +24,11 @@ public T SetProperty<T>(string property, T value)
return value;
}

public bool TryGetProperty<T>(string property, out T result) where T : class
public bool TryGetProperty<T>(
string property,
[NotNullWhen(true)]
out T? result
) where T : class
{
if (ContainsKey(property))
{
Expand All @@ -35,7 +40,7 @@ public bool TryGetProperty<T>(string property, out T result) where T : class
return false;
}

public T GetProperty<T>(string property) where T : class
public T? GetProperty<T>(string property) where T : class
{
TryGetProperty<T>(property, out var result);
return result;
Expand Down
2 changes: 1 addition & 1 deletion src/DockerComposeBuilder/Model/PlacementPreferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class PlacementPreferences : ObjectBase
{
public string Spread
{
get => GetProperty<string>("spread");
get => GetProperty<string>("spread")!;
set => SetProperty("spread", value);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/DockerComposeBuilder/Model/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class Service
[YamlMember(Alias = "image")]
public string? Image { get; set; }

[YamlMember(Alias = "privileged")]
public bool? Privileged { get; set; }

[YamlMember(Alias = "build")]
public ServiceBuild? Build { get; set; }

Expand Down