72 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			72 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using System;
 | |
| using System.CommandLine;
 | |
| using System.Threading;
 | |
| using System.Threading.Tasks;
 | |
| using Microsoft.Extensions.DependencyInjection;
 | |
| using Microsoft.Extensions.Logging;
 | |
| using StellaOps.Cli.Commands;
 | |
| using StellaOps.Cli.Configuration;
 | |
| using StellaOps.Cli.Services;
 | |
| using StellaOps.Cli.Telemetry;
 | |
| 
 | |
| namespace StellaOps.Cli;
 | |
| 
 | |
| internal static class Program
 | |
| {
 | |
|     internal static async Task<int> Main(string[] args)
 | |
|     {
 | |
|         var (options, configuration) = CliBootstrapper.Build(args);
 | |
| 
 | |
|         var services = new ServiceCollection();
 | |
|         services.AddSingleton(configuration);
 | |
|         services.AddSingleton(options);
 | |
| 
 | |
|         var verbosityState = new VerbosityState();
 | |
|         services.AddSingleton(verbosityState);
 | |
| 
 | |
|         services.AddLogging(builder =>
 | |
|         {
 | |
|             builder.ClearProviders();
 | |
|             builder.AddSimpleConsole(logOptions =>
 | |
|             {
 | |
|                 logOptions.TimestampFormat = "HH:mm:ss ";
 | |
|                 logOptions.SingleLine = true;
 | |
|             });
 | |
|             builder.AddFilter((category, level) => level >= verbosityState.MinimumLevel);
 | |
|         });
 | |
| 
 | |
|         services.AddHttpClient<IBackendOperationsClient, BackendOperationsClient>(client =>
 | |
|         {
 | |
|             client.Timeout = TimeSpan.FromMinutes(5);
 | |
|             if (!string.IsNullOrWhiteSpace(options.BackendUrl) &&
 | |
|                 Uri.TryCreate(options.BackendUrl, UriKind.Absolute, out var backendUri))
 | |
|             {
 | |
|                 client.BaseAddress = backendUri;
 | |
|             }
 | |
|         });
 | |
| 
 | |
|         services.AddSingleton<IScannerExecutor, ScannerExecutor>();
 | |
|         services.AddSingleton<IScannerInstaller, ScannerInstaller>();
 | |
| 
 | |
|         await using var serviceProvider = services.BuildServiceProvider();
 | |
|         using var cts = new CancellationTokenSource();
 | |
|         Console.CancelKeyPress += (_, eventArgs) =>
 | |
|         {
 | |
|             eventArgs.Cancel = true;
 | |
|             cts.Cancel();
 | |
|         };
 | |
| 
 | |
|         var rootCommand = CommandFactory.Create(serviceProvider, options, cts.Token);
 | |
|         var commandConfiguration = new CommandLineConfiguration(rootCommand);
 | |
|         var commandExit = await commandConfiguration.InvokeAsync(args, cts.Token).ConfigureAwait(false);
 | |
| 
 | |
|         var finalExit = Environment.ExitCode != 0 ? Environment.ExitCode : commandExit;
 | |
|         if (cts.IsCancellationRequested && finalExit == 0)
 | |
|         {
 | |
|             finalExit = 130; // Typical POSIX cancellation exit code
 | |
|         }
 | |
| 
 | |
|         return finalExit;
 | |
|     }
 | |
| }
 |