Skip to content

Commit 4e8419f

Browse files
Specify the Path of the Golang Executable (#755)
I enhanced GolangAppHostingExtension to define a new override of AddGolangApp that allows specifying the path of the Golang executable relative to the working directory. This is useful for scenarios where the Golang executable is in a `cmd` subdirectory, but the working directory needs to be the root of the project for accessing resources relative to the working directory at runtime. It is also useful when there are more than one Golang executable in a Go module. Resolves #754 Co-authored-by: Tommaso Stocchi <tstocchi@microsoft.com>
1 parent 8b5e7e0 commit 4e8419f

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ public static IResourceBuilder<GolangAppExecutableResource> AddGolangApp(this ID
3030
/// <param name="buildTags">The optional build tags to be used when building the Golang application.</param>
3131
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
3232
public static IResourceBuilder<GolangAppExecutableResource> AddGolangApp(this IDistributedApplicationBuilder builder, [ResourceName] string name, string workingDirectory, string[]? args = null, string[]? buildTags = null)
33+
=> AddGolangApp(builder, name, workingDirectory, ".", args, buildTags);
34+
35+
/// <summary>
36+
/// Adds a Golang application to the application model. Executes the executable Golang app.
37+
/// </summary>
38+
/// <param name="builder">The <see cref="IDistributedApplicationBuilder"/> to add the resource to.</param>
39+
/// <param name="name">The name of the resource.</param>
40+
/// <param name="workingDirectory">The working directory to use for the command. If null, the working directory of the current process is used.</param>
41+
/// <param name="executable">The path to the Golang package directory or source file to be executed. Use "." to execute the program in the current directory. For example, "./cmd/server".</param>
42+
/// <param name="args">The optinal arguments to be passed to the executable when it is started.</param>
43+
/// <param name="buildTags">The optional build tags to be used when building the Golang application.</param>
44+
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
45+
public static IResourceBuilder<GolangAppExecutableResource> AddGolangApp(this IDistributedApplicationBuilder builder, [ResourceName] string name, string workingDirectory, string executable, string[]? args = null, string[]? buildTags = null)
3346
{
3447
ArgumentNullException.ThrowIfNull(builder, nameof(builder));
3548
ArgumentException.ThrowIfNullOrWhiteSpace(name, nameof(name));
@@ -43,7 +56,7 @@ public static IResourceBuilder<GolangAppExecutableResource> AddGolangApp(this ID
4356
allArgs.Add(string.Join(",", buildTags));
4457
}
4558

46-
allArgs.Add(".");
59+
allArgs.Add(executable);
4760

4861
if (args is { Length: > 0 })
4962
{

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,34 @@ public async Task GolangAppWithBuildTagsAsync()
5858
}
5959
);
6060
}
61+
62+
63+
[Fact]
64+
public async Task GolangAppWithExecutableAsync()
65+
{
66+
var builder = DistributedApplication.CreateBuilder();
67+
68+
builder.AddGolangApp("golang", "../../examples/golang/gin-api", "./cmd/server");
69+
70+
using var app = builder.Build();
71+
72+
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();
73+
74+
var resource = appModel.Resources.OfType<GolangAppExecutableResource>().SingleOrDefault();
75+
76+
Assert.NotNull(resource);
77+
78+
var args = await resource.GetArgumentValuesAsync();
79+
Assert.Collection(
80+
args,
81+
arg =>
82+
{
83+
Assert.Equal("run", arg);
84+
},
85+
arg =>
86+
{
87+
Assert.Equal("./cmd/server", arg);
88+
}
89+
);
90+
}
6191
}

0 commit comments

Comments
 (0)