Skip to content

Add Go Build Tags Support #733

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

Merged
merged 2 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,27 @@ public static class GolangAppHostingExtension
/// <param name="name">The name of the resource.</param>
/// <param name="workingDirectory">The working directory to use for the command. If null, the working directory of the current process is used.</param>
/// <param name="args">The optinal arguments to be passed to the executable when it is started.</param>
/// <param name="buildTags">The optional build tags to be used when building the Golang application.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<GolangAppExecutableResource> AddGolangApp(this IDistributedApplicationBuilder builder, [ResourceName] string name, string workingDirectory, string[]? args = null)
public static IResourceBuilder<GolangAppExecutableResource> 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", ".",];
string[] allArgs;
if (buildTags is { Length: > 0 })
{
allArgs = args is { Length: > 0 }
? ["run", "-tags", string.Join(",", buildTags), ".", .. args ?? []]
: ["run", "-tags", string.Join(",", buildTags), "."];
}
else
{
allArgs = args is { Length: > 0 }
? ["run", ".", .. args]
: ["run", ".",];
}

workingDirectory = Path.Combine(builder.AppHostDirectory, workingDirectory).NormalizePathForCurrentPlatform();
var resource = new GolangAppExecutableResource(name, workingDirectory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DistributedApplicationModel>();

var resource = appModel.Resources.OfType<GolangAppExecutableResource>().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);
}
);
}
}
Loading