2023-01-04 17:53:48 +00:00
|
|
|
namespace Configuration
|
|
|
|
{
|
2023-01-06 15:08:26 +00:00
|
|
|
public sealed class ApplicationConfigurationService
|
2023-01-04 17:53:48 +00:00
|
|
|
{
|
|
|
|
private FileInfo[] jsonFiles;
|
|
|
|
public FileInfo[] JsonFilesInfo
|
|
|
|
{
|
|
|
|
get { return jsonFiles; }
|
|
|
|
private set
|
|
|
|
{
|
|
|
|
jsonFiles = value.Reverse().ToArray();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private FileInfo outputFile;
|
|
|
|
public FileInfo OutputFile{
|
|
|
|
get {return outputFile;}
|
|
|
|
private set
|
|
|
|
{
|
|
|
|
if (!canCreateFile(value.FullName))
|
|
|
|
throw new DemoApplicationConfigurationException($"Given output name '{value.FullName}' cannot be used");
|
|
|
|
outputFile = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-06 15:08:26 +00:00
|
|
|
public static ApplicationConfigurationService InitializeFrom(string[] args)
|
2023-01-04 17:53:48 +00:00
|
|
|
{
|
|
|
|
if (args.Length < 3)
|
|
|
|
throw new DemoApplicationConfigurationException("Must have atleast: 1 flag, 1 path, 1 output filename");
|
|
|
|
|
|
|
|
var outputFileName = new FileInfo(args.Last());
|
|
|
|
|
|
|
|
// Remove last file-name arg, to get a more consistent array
|
2023-01-04 18:47:37 +00:00
|
|
|
var strippedArgs = args.SkipLast(1).ToArray();
|
2023-01-04 17:53:48 +00:00
|
|
|
|
|
|
|
|
2023-01-04 18:47:37 +00:00
|
|
|
string[] givenFlags = strippedArgs.Where((value, index) => index % 2 == 0).ToArray();
|
|
|
|
string[] givenFilePaths = strippedArgs.Where((value, index) => index % 2 != 0).ToArray();
|
2023-01-04 17:53:48 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2023-01-04 18:47:37 +00:00
|
|
|
// Assert only a single 'd' was passed
|
2023-01-04 17:53:48 +00:00
|
|
|
if ( givenFlags.Length == 1 && givenFlags[0].Equals("-d") ) {
|
|
|
|
string[] filesInDirectory = Directory.GetFiles(givenFilePaths[0]);
|
2023-01-06 15:08:26 +00:00
|
|
|
return new ApplicationConfigurationService {
|
2023-01-04 17:53:48 +00:00
|
|
|
OutputFile = outputFileName,
|
|
|
|
JsonFilesInfo = filesInDirectory.Select(name => new FileInfo(name)).ToArray(),
|
|
|
|
};
|
|
|
|
}
|
2023-01-04 18:47:37 +00:00
|
|
|
// Assert only '-f' were passed
|
2023-01-04 17:53:48 +00:00
|
|
|
else if (givenFlags.All((string flag) => flag.Equals("-f"))) {
|
2023-01-06 15:08:26 +00:00
|
|
|
return new ApplicationConfigurationService {
|
2023-01-04 17:53:48 +00:00
|
|
|
OutputFile = outputFileName,
|
|
|
|
JsonFilesInfo = givenFilePaths.Select(name => new FileInfo(name)).ToArray(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
2023-01-04 18:47:37 +00:00
|
|
|
throw new DemoApplicationConfigurationException("Must have either 1 flag that is '-d', or all flags must be '-f");
|
2023-01-04 17:53:48 +00:00
|
|
|
}
|
2023-01-04 18:47:37 +00:00
|
|
|
} catch (Exception)
|
2023-01-04 17:53:48 +00:00
|
|
|
{
|
2023-01-04 18:47:37 +00:00
|
|
|
Console.WriteLine("Something went wrong");
|
2023-01-04 17:53:48 +00:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static bool canCreateFile(string filename) {
|
|
|
|
try
|
|
|
|
{
|
|
|
|
using (File.Create(filename)) {
|
|
|
|
File.Delete(filename);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Serializable]
|
|
|
|
internal sealed class DemoApplicationConfigurationException : Exception
|
|
|
|
{
|
|
|
|
public DemoApplicationConfigurationException()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public DemoApplicationConfigurationException(string? message) : base(message)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|