Skip to content

Commit ef17213

Browse files
committed
Add Go Build Tags Support
I updated GolangAppHostingExtension to support specifying Go build tags to be used when building and running Go applications. Go build tags can be used to include or exclude files from the build process based On specific conditions. For example, when using Aspire for development, a Go developer may use a build tag to enable special code for OpenTelemetry instrumentation or enable debugging features. Closes #730
1 parent c0d5078 commit ef17213

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

src/CommunityToolkit.Aspire.Hosting.Golang/GolangAppHostingExtension.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,27 @@ public static class GolangAppHostingExtension
1515
/// <param name="name">The name of the resource.</param>
1616
/// <param name="workingDirectory">The working directory to use for the command. If null, the working directory of the current process is used.</param>
1717
/// <param name="args">The optinal arguments to be passed to the executable when it is started.</param>
18+
/// <param name="buildTags">The optional build tags to be used when building the Golang application.</param>
1819
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
19-
public static IResourceBuilder<GolangAppExecutableResource> AddGolangApp(this IDistributedApplicationBuilder builder, [ResourceName] string name, string workingDirectory, string[]? args = null)
20+
public static IResourceBuilder<GolangAppExecutableResource> AddGolangApp(this IDistributedApplicationBuilder builder, [ResourceName] string name, string workingDirectory, string[]? args = null, string[]? buildTags = null)
2021
{
2122
ArgumentNullException.ThrowIfNull(builder, nameof(builder));
2223
ArgumentException.ThrowIfNullOrWhiteSpace(name, nameof(name));
2324
ArgumentException.ThrowIfNullOrWhiteSpace(workingDirectory, nameof(workingDirectory));
2425

25-
string[] allArgs = args is { Length: > 0 }
26-
? ["run", ".", .. args]
27-
: ["run", ".",];
26+
string[] allArgs;
27+
if (buildTags is { Length: > 0 })
28+
{
29+
allArgs = args is { Length: > 0 }
30+
? ["run", "-tags", string.Join(",", buildTags), ".", .. args ?? []]
31+
: ["run", "-tags", string.Join(",", buildTags), "."];
32+
}
33+
else
34+
{
35+
allArgs = args is { Length: > 0 }
36+
? ["run", ".", .. args]
37+
: ["run", ".",];
38+
}
2839

2940
workingDirectory = Path.Combine(builder.AppHostDirectory, workingDirectory).NormalizePathForCurrentPlatform();
3041
var resource = new GolangAppExecutableResource(name, workingDirectory);

tests/CommunityToolkit.Aspire.Hosting.Golang.Tests/ResourceCreationTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,41 @@ public void DefaultGolangApp()
2121

2222
Assert.Equal("go", resource.Command);
2323
}
24+
25+
[Fact]
26+
public async Task GolangAppWithBuildTagsAsync()
27+
{
28+
var builder = DistributedApplication.CreateBuilder();
29+
30+
builder.AddGolangApp("golang", "../../examples/golang/gin-api", buildTags: ["dev"]);
31+
32+
using var app = builder.Build();
33+
34+
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();
35+
36+
var resource = appModel.Resources.OfType<GolangAppExecutableResource>().SingleOrDefault();
37+
38+
Assert.NotNull(resource);
39+
40+
var args = await resource.GetArgumentValuesAsync();
41+
Assert.Collection(
42+
args,
43+
arg =>
44+
{
45+
Assert.Equal("run", arg);
46+
},
47+
arg =>
48+
{
49+
Assert.Equal("-tags", arg);
50+
},
51+
arg =>
52+
{
53+
Assert.Equal("dev", arg);
54+
},
55+
arg =>
56+
{
57+
Assert.Equal(".", arg);
58+
}
59+
);
60+
}
2461
}

0 commit comments

Comments
 (0)