-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
106 lines (91 loc) · 3.52 KB
/
Copy pathProgram.cs
File metadata and controls
106 lines (91 loc) · 3.52 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using AzureDbLoginHelper;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
internal static class Program
{
private const string LocalSettingsPath = "appsettings.local.json";
#if USE_AVALONIA_TRAY
// Avalonia macOS native backend must initialize on the process main thread (not thread pool / worker).
public static void Main(string[] args) => RunMacOs(args);
private static void RunMacOs(string[] args)
{
var host = CreateHost(args);
host.StartAsync().GetAwaiter().GetResult();
WriteStartupMessage();
try
{
AvaloniaEntry.Run(host, args);
}
finally
{
host.StopAsync().GetAwaiter().GetResult();
}
}
#else
public static async Task Main(string[] args)
{
var host = CreateHost(args);
await host.StartAsync();
WriteStartupMessage();
try
{
var tray = host.Services.GetRequiredService<WindowsTrayHost>();
await tray.RunAsync(host.Services.GetRequiredService<IHostApplicationLifetime>().ApplicationStopping);
}
finally
{
await host.StopAsync();
}
}
#endif
private static IHost CreateHost(string[] args)
{
var builder = Host.CreateApplicationBuilder(args);
UseLocalAppSettings(builder);
builder.Services
.AddOptions<AzureDbLoginOptions>()
.Bind(builder.Configuration.GetSection(AzureDbLoginOptions.SectionName))
.Validate(o => !o.HasRoles || string.IsNullOrWhiteSpace(o.DefaultRole) || o.FindRole(o.DefaultRole) != null,
"AzureDbLogin:DefaultRole must match a configured Roles[].Key when set.")
.Validate(o => !o.HasRoles || o.Roles.TrueForAll(r =>
!string.IsNullOrWhiteSpace(r.PgUser) && !string.IsNullOrWhiteSpace(r.GroupObjectId)),
"When roles are configured, each role must have PgUser and GroupObjectId.")
.ValidateOnStart();
builder.Services.AddSingleton<TokenService>();
#if USE_WINFORMS_TRAY
builder.Services.AddSingleton<WindowsTrayHost>();
#elif USE_AVALONIA_TRAY
builder.Services.AddSingleton<TrayController>();
#endif
return builder.Build();
}
private static void UseLocalAppSettings(HostApplicationBuilder builder)
{
for (var i = builder.Configuration.Sources.Count - 1; i >= 0; i--)
{
if (builder.Configuration.Sources[i] is FileConfigurationSource { Path: var path }
&& path.Equals("appsettings.json", StringComparison.OrdinalIgnoreCase))
{
builder.Configuration.Sources.RemoveAt(i);
}
}
builder.Configuration.AddJsonFile(LocalSettingsPath, optional: false, reloadOnChange: true);
}
private static void WriteStartupMessage()
{
Console.WriteLine("Azure DB Login Helper is running.");
if (OperatingSystem.IsMacOS())
{
Console.WriteLine(" Look for the icon in the menu bar (top right).");
Console.WriteLine(" Click the icon for roles and tokens.");
}
else
{
Console.WriteLine(" Look for the tray icon (click ^ in the taskbar if hidden).");
Console.WriteLine(" Right-click the tray icon for the menu (double-click copies cached token).");
}
Console.WriteLine(" Press Ctrl+C here, or choose Exit from the menu, to quit.");
Console.WriteLine();
}
}