diff --git a/src/CommunityToolkit.Aspire.Hosting.Golang/GolangAppHostingExtension.cs b/src/CommunityToolkit.Aspire.Hosting.Golang/GolangAppHostingExtension.cs index 4541f9f0..23f3b453 100644 --- a/src/CommunityToolkit.Aspire.Hosting.Golang/GolangAppHostingExtension.cs +++ b/src/CommunityToolkit.Aspire.Hosting.Golang/GolangAppHostingExtension.cs @@ -15,23 +15,35 @@ public static class GolangAppHostingExtension /// The name of the resource. /// The working directory to use for the command. If null, the working directory of the current process is used. /// The optinal arguments to be passed to the executable when it is started. + /// The optional build tags to be used when building the Golang application. /// A reference to the . - public static IResourceBuilder AddGolangApp(this IDistributedApplicationBuilder builder, [ResourceName] string name, string workingDirectory, string[]? args = null) + public static IResourceBuilder AddGolangApp(this IDistributedApplicationBuilder builder, [ResourceName] string name, string workingDirectory, string[]? args = null, string[]? buildTags = null) { ArgumentNullException.ThrowIfNull(builder, nameof(builder)); ArgumentException.ThrowIfNullOrWhiteSpace(name, nameof(name)); ArgumentException.ThrowIfNullOrWhiteSpace(workingDirectory, nameof(workingDirectory)); - string[] allArgs = args is { Length: > 0 } - ? ["run", ".", .. args] - : ["run", ".",]; + var allArgs = new List { "run" }; + + if (buildTags is { Length: > 0 }) + { + allArgs.Add("-tags"); + allArgs.Add(string.Join(",", buildTags)); + } + + allArgs.Add("."); + + if (args is { Length: > 0 }) + { + allArgs.AddRange(args); + } workingDirectory = Path.Combine(builder.AppHostDirectory, workingDirectory).NormalizePathForCurrentPlatform(); var resource = new GolangAppExecutableResource(name, workingDirectory); return builder.AddResource(resource) .WithGolangDefaults() - .WithArgs(allArgs); + .WithArgs(allArgs.ToArray()); } private static IResourceBuilder WithGolangDefaults( diff --git a/tests/CommunityToolkit.Aspire.Hosting.Golang.Tests/ResourceCreationTests.cs b/tests/CommunityToolkit.Aspire.Hosting.Golang.Tests/ResourceCreationTests.cs index c6e0ec1a..b0a0aff3 100644 --- a/tests/CommunityToolkit.Aspire.Hosting.Golang.Tests/ResourceCreationTests.cs +++ b/tests/CommunityToolkit.Aspire.Hosting.Golang.Tests/ResourceCreationTests.cs @@ -21,4 +21,41 @@ public void DefaultGolangApp() Assert.Equal("go", resource.Command); } + + [Fact] + public async Task GolangAppWithBuildTagsAsync() + { + var builder = DistributedApplication.CreateBuilder(); + + builder.AddGolangApp("golang", "../../examples/golang/gin-api", buildTags: ["dev"]); + + using var app = builder.Build(); + + var appModel = app.Services.GetRequiredService(); + + var resource = appModel.Resources.OfType().SingleOrDefault(); + + Assert.NotNull(resource); + + var args = await resource.GetArgumentValuesAsync(); + Assert.Collection( + args, + arg => + { + Assert.Equal("run", arg); + }, + arg => + { + Assert.Equal("-tags", arg); + }, + arg => + { + Assert.Equal("dev", arg); + }, + arg => + { + Assert.Equal(".", arg); + } + ); + } } \ No newline at end of file