-
Notifications
You must be signed in to change notification settings - Fork 874
Expand file tree
/
Copy pathEnvironmentVariablesConfigurationGatherer.cs
More file actions
37 lines (32 loc) · 1.5 KB
/
EnvironmentVariablesConfigurationGatherer.cs
File metadata and controls
37 lines (32 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Extensions.Logging;
namespace Aspire.Hosting.ApplicationModel;
/// <summary>
/// Gathers environment variables for resources.
/// </summary>
internal class EnvironmentVariablesExecutionConfigurationGatherer : IExecutionConfigurationGatherer
{
/// <inheritdoc/>
public async ValueTask GatherAsync(IExecutionConfigurationGathererContext context, IResource resource, ILogger resourceLogger, DistributedApplicationExecutionContext executionContext, CancellationToken cancellationToken = default)
{
if (resource.TryGetEnvironmentVariables(out var envVarAnnotations))
{
var envVars = new Dictionary<string, object>(context.EnvironmentVariables);
var callbackContext = new EnvironmentCallbackContext(executionContext, resource, envVars, cancellationToken: cancellationToken)
{
Logger = resourceLogger,
};
foreach (var ann in envVarAnnotations)
{
// Each annotation operates on a shared context.
envVars = await ann.AsCallbackAnnotation().EvaluateOnceAsync(callbackContext).ConfigureAwait(false);
}
// Take the final result and apply to the gatherer context.
foreach (var kvp in envVars)
{
context.EnvironmentVariables[kvp.Key] = kvp.Value;
}
}
}
}