-
Notifications
You must be signed in to change notification settings - Fork 543
/
Copy pathEnvironmentVariableEvaluator.cs
44 lines (37 loc) · 1.75 KB
/
EnvironmentVariableEvaluator.cs
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
38
39
40
41
42
43
44
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Aspire.Hosting.Tests.Utils;
public static class EnvironmentVariableEvaluator
{
public static async ValueTask<Dictionary<string, string>> GetEnvironmentVariablesAsync(IResource resource,
DistributedApplicationOperation applicationOperation = DistributedApplicationOperation.Run)
{
var environmentVariables = new Dictionary<string, string>();
if (resource.TryGetEnvironmentVariables(out var callbacks))
{
var config = new Dictionary<string, object>();
var executionContext = new DistributedApplicationExecutionContext(applicationOperation);
var context = new EnvironmentCallbackContext(executionContext, config);
foreach (var callback in callbacks)
{
await callback.Callback(context);
}
foreach (var (key, expr) in config)
{
var value = (applicationOperation, expr) switch
{
(_, string s) => s,
(DistributedApplicationOperation.Run, IValueProvider provider) => await provider.GetValueAsync().ConfigureAwait(false),
(DistributedApplicationOperation.Publish, IManifestExpressionProvider provider) => provider.ValueExpression,
(_, null) => null,
_ => throw new InvalidOperationException($"Unsupported expression type: {expr.GetType()}")
};
if (value is not null)
{
environmentVariables[key] = value;
}
}
}
return environmentVariables;
}
}