CsharpTemplate/ConsoleTemplate/Program.cs

95 lines
3.1 KiB
C#
Raw Permalink Normal View History

2023-10-27 19:00:41 +07:00
using ConsoleTemplate.Contexts;
using ConsoleTemplate.Repositories;
using ConsoleTemplate.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.CommandLine;
using System.Globalization;
namespace ConsoleTemplate
{
internal class Program
{
static int Main(string[] args)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
var serviceProvider = serviceCollection.BuildServiceProvider();
var timerSvc = serviceProvider.GetRequiredService<TimerService>();
// Option
var dateOption = new Option<DateTime>
(
name: "--date",
description: "Tangal Proses",
isDefault: true,
parseArgument: result =>
{
if (result.Tokens.Count == 0)
{
return DateTime.Now;
}
string strtgl = result.Tokens.Single().Value;
if (DateTime.TryParseExact(strtgl, "yyMMdd", null, DateTimeStyles.None, out DateTime tgl))
{
return tgl;
}
else
{
result.ErrorMessage = "Format tanggal salah";
return DateTime.Now;
}
}
);
dateOption.AddAlias("-d");
var threadOption = new Option<int>
(
name: "--thread",
description: "Jumlah Thread",
getDefaultValue: () => 8
);
threadOption.AddAlias("-t");
// Command
// == Timer
var rootCommand = new RootCommand("Proses RO PB");
rootCommand.AddOption(dateOption);
rootCommand.AddOption(threadOption);
rootCommand.SetHandler
(
(ctx) =>
{
timerSvc.Start(
ctx.ParseResult.GetValueForOption<DateTime>(dateOption),
ctx.ParseResult.GetValueForOption<int>(threadOption)
);
}
);
return rootCommand.Invoke(args);
}
private static void ConfigureServices(ServiceCollection service)
{
// Build Configuration
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
//.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("$safeprojectname$.config.json")
.Build();
service.AddSingleton(configuration);
service.AddSingleton<IDBContext, MysqlDBContext>();
service.AddSingleton<LogService>();
service.AddSingleton<TimerService>();
service.AddScoped<IRepository, MysqlRepository>();
}
}
}