68 lines
2.5 KiB
C#

using Microsoft.Extensions.Configuration;
using System.Text.Json;
namespace View_by_Distance.Delete.By.Distinct.Models.Binder;
public class AppSettings
{
#nullable disable
public string Company { get; set; }
public string CompareRootDirectory { get; set; }
public int? MaxDegreeOfParallelism { get; set; }
public string OutputExtension { get; set; }
public bool? RecycleOption { get; set; }
public bool? RenameToMatch { get; set; }
public string SearchPattern { get; set; }
public bool? SizeForLong { get; set; }
public bool? TicksForLong { get; set; }
public string WorkingDirectoryName { get; set; }
#nullable restore
public override string ToString()
{
string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });
return result;
}
private static Models.AppSettings Get(AppSettings? appSettings)
{
Models.AppSettings result;
if (appSettings?.MaxDegreeOfParallelism is null)
throw new NullReferenceException(nameof(appSettings.MaxDegreeOfParallelism));
if (appSettings?.RecycleOption is null)
throw new NullReferenceException(nameof(appSettings.RecycleOption));
if (appSettings?.RenameToMatch is null)
throw new NullReferenceException(nameof(appSettings.RenameToMatch));
if (appSettings?.SizeForLong is null)
throw new NullReferenceException(nameof(appSettings.SizeForLong));
if (appSettings?.TicksForLong is null)
throw new NullReferenceException(nameof(appSettings.TicksForLong));
if (appSettings.TicksForLong.Value == appSettings.SizeForLong.Value)
throw new Exception("Check appSettings file!");
result = new(
appSettings.Company,
appSettings.CompareRootDirectory,
appSettings.MaxDegreeOfParallelism.Value,
appSettings.OutputExtension,
appSettings.RecycleOption.Value,
appSettings.RenameToMatch.Value,
appSettings.SearchPattern,
appSettings.SizeForLong.Value,
appSettings.TicksForLong.Value,
appSettings.WorkingDirectoryName
);
return result;
}
public static Models.AppSettings Get(IConfigurationRoot configurationRoot)
{
Models.AppSettings result;
AppSettings? appSettings = configurationRoot.Get<AppSettings>();
result = Get(appSettings);
return result;
}
}