wabbajack/Wabbajack.CLI/Program.cs
2022-10-07 14:53:55 -06:00

84 lines
3.1 KiB
C#

using System;
using System.CommandLine;
using System.CommandLine.IO;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using NLog.Targets;
using Octokit;
using Wabbajack.DTOs.Interventions;
using Wabbajack.Networking.Http;
using Wabbajack.Networking.Http.Interfaces;
using Wabbajack.Paths.IO;
using Wabbajack.Server.Lib;
using Wabbajack.Services.OSIntegrated;
using Wabbajack.VFS;
using Client = Wabbajack.Networking.GitHub.Client;
namespace Wabbajack.CLI;
internal class Program
{
private static async Task<int> Main(string[] args)
{
var host = Host.CreateDefaultBuilder(Array.Empty<string>())
.ConfigureLogging(AddLogging)
.ConfigureServices((host, services) =>
{
services.AddSingleton(new JsonSerializerOptions());
services.AddSingleton<HttpClient, HttpClient>();
services.AddSingleton<IHttpDownloader, SingleThreadedDownloader>();
services.AddSingleton<IConsole, SystemConsole>();
services.AddSingleton<CommandLineBuilder, CommandLineBuilder>();
services.AddSingleton<TemporaryFileManager>();
services.AddSingleton<FileExtractor.FileExtractor>();
services.AddSingleton(new ParallelOptions {MaxDegreeOfParallelism = Environment.ProcessorCount});
services.AddSingleton<Client>();
services.AddSingleton<Networking.WabbajackClientApi.Client>();
services.AddSingleton(s => new GitHubClient(new ProductHeaderValue("wabbajack")));
services.AddOSIntegrated();
services.AddServerLib();
services.AddTransient<Context>();
services.AddCommands();
services.AddSingleton<IUserInterventionHandler, UserInterventionHandler>();
}).Build();
var service = host.Services.GetService<CommandLineBuilder>();
return await service!.Run(args);
}
private static void AddLogging(ILoggingBuilder loggingBuilder)
{
var config = new NLog.Config.LoggingConfiguration();
var fileTarget = new FileTarget("file")
{
FileName = "logs/wabbajack-cli.current.log",
ArchiveFileName = "logs/wabbajack-cli.{##}.log",
ArchiveOldFileOnStartup = true,
MaxArchiveFiles = 10,
Layout = "${processtime} [${level:uppercase=true}] (${logger}) ${message:withexception=true}",
Header = "############ Wabbajack log file - ${longdate} ############"
};
var consoleTarget = new ConsoleTarget("console")
{
Layout = "${processtime} [${level:uppercase=true}] ${message:withexception=true}",
};
config.AddRuleForAllLevels(fileTarget);
config.AddRuleForAllLevels(consoleTarget);
loggingBuilder.ClearProviders();
loggingBuilder.SetMinimumLevel(LogLevel.Trace);
loggingBuilder.AddNLog(config);
}
}