Skip to content

Commit 59b17eb

Browse files
authored
Add Go Build Tags Support (#733)
* 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 * Revise Command-Line Building I revised the way that I was building the command line for the `go run` command based on review recommendations. The new approach should improve readability of the code.
1 parent c0d5078 commit 59b17eb

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,35 @@ 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+
var allArgs = new List<string> { "run" };
27+
28+
if (buildTags is { Length: > 0 })
29+
{
30+
allArgs.Add("-tags");
31+
allArgs.Add(string.Join(",", buildTags));
32+
}
33+
34+
allArgs.Add(".");
35+
36+
if (args is { Length: > 0 })
37+
{
38+
allArgs.AddRange(args);
39+
}
2840

2941
workingDirectory = Path.Combine(builder.AppHostDirectory, workingDirectory).NormalizePathForCurrentPlatform();
3042
var resource = new GolangAppExecutableResource(name, workingDirectory);
3143

3244
return builder.AddResource(resource)
3345
.WithGolangDefaults()
34-
.WithArgs(allArgs);
46+
.WithArgs(allArgs.ToArray());
3547
}
3648

3749
private static IResourceBuilder<GolangAppExecutableResource> WithGolangDefaults(

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)