file-folder-helper/Program.cs
2022-05-26 08:57:57 -07:00

151 lines
6.6 KiB
C#

using File_Folder_Helper.Models;
using Microsoft.Extensions.Configuration;
using Phares.Shared.Models;
using Phares.Shared.Models.Stateless.Methods;
using Serilog;
using System.Diagnostics;
using System.Reflection;
namespace File_Folder_Helper;
internal class Program
{
public static void Secondary(List<string> args)
{
LoggerConfiguration loggerConfiguration = new();
Assembly assembly = Assembly.GetExecutingAssembly();
bool debuggerWasAttachedAtLineZero = Debugger.IsAttached || assembly.Location.Contains(@"\bin\Debug");
IsEnvironment isEnvironment = new(processesCount: null, nullASPNetCoreEnvironmentIsDevelopment: debuggerWasAttachedAtLineZero, nullASPNetCoreEnvironmentIsProduction: !debuggerWasAttachedAtLineZero);
IConfigurationBuilder configurationBuilder = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddJsonFile(isEnvironment.AppSettingsFileName, optional: false, reloadOnChange: true);
IConfigurationRoot configurationRoot = configurationBuilder.Build();
AppSettings appSettings = Models.Stateless.AppSettings.Get(configurationRoot);
if (string.IsNullOrEmpty(appSettings.WorkingDirectoryName))
throw new Exception("Working directory name must have a value!");
string workingDirectory = IWorkingDirectory.GetWorkingDirectory(assembly.GetName().Name, appSettings.WorkingDirectoryName);
Environment.SetEnvironmentVariable(nameof(workingDirectory), workingDirectory);
_ = ConfigurationLoggerConfigurationExtensions.Configuration(loggerConfiguration.ReadFrom, configurationRoot);
Log.Logger = loggerConfiguration.CreateLogger();
ILogger log = Log.ForContext<Program>();
int silentIndex = args.IndexOf("s");
if (silentIndex > -1)
args.RemoveAt(silentIndex);
try
{
int? singleCharIndex = null;
ConsoleKey consoleKey = ConsoleKey.End;
ConsoleKey[] consoleKeys = new ConsoleKey[]
{
ConsoleKey.A,
ConsoleKey.B,
ConsoleKey.C,
ConsoleKey.D,
ConsoleKey.E,
ConsoleKey.F,
ConsoleKey.L,
ConsoleKey.R,
ConsoleKey.T,
ConsoleKey.Z,
ConsoleKey.Delete
};
for (int i = 0; i < args.Count; i++)
{
if (args[i].Length == 1 && Enum.TryParse(args[i], out consoleKey) && consoleKeys.Contains(consoleKey))
{
singleCharIndex = i;
break;
}
}
if (consoleKey is not ConsoleKey.End && !consoleKeys.Contains(consoleKey))
consoleKey = ConsoleKey.End;
if (singleCharIndex is not null)
args.RemoveAt(singleCharIndex.Value);
if (!args.Any())
log.Information("Must pass a argument!");
else if (Directory.Exists(args[0]) && File.Exists(Path.Combine(args[0], string.Concat(Path.GetFileName(args[0]), ".dll"))))
Helpers.HelperILMerge.ILMerge(args[0]);
else if (Directory.Exists(args[0]))
{
for (int i = 0; i < int.MaxValue; i++)
{
if (consoleKeys.Contains(consoleKey))
break;
log.Information("A) Save (Top Directory Only),");
log.Information("B) Save (All Directories),");
log.Information("C) Clipboard (Top Directory Only),");
log.Information("D) Clipboard (All Directories),");
log.Information("E) Clipboard (Top Directory Only and File Name Without Extension),");
log.Information("F) Clipboard (All Directories and File Name Without Extension),");
log.Information("L) Log Merge (APC Log [0-9{8}]_*.log),");
log.Information("R) Rename to old, copy, delete old");
log.Information("T) Too long rename");
log.Information("Z) Zip file(s) by date,");
log.Information("Delete) Delete empty directories,");
consoleKey = Console.ReadKey().Key;
log.Information(string.Empty);
}
switch (consoleKey)
{
case ConsoleKey.A:
case ConsoleKey.B:
case ConsoleKey.C:
case ConsoleKey.E:
case ConsoleKey.F:
Helpers.HelperSaveOrCopyContents.SaveOrCopyContents(log, args[0], consoleKey);
break;
case ConsoleKey.L:
Helpers.HelperLogMerge.LogMerge(args[0]);
break;
case ConsoleKey.R:
Helpers.HelperRenameToOldMoveDeleteOldMerge.RenameToOldMoveDeleteOld(log, args[0]);
break;
case ConsoleKey.T:
Helpers.HelperTooLong.TooLong(args[0], delete: false);
Helpers.HelperTooLong.TooLong(args[0], delete: true);
break;
case ConsoleKey.Z:
Helpers.HelperZipFilesByDate.ZipFilesByDate(log, args[0]);
break;
default:
switch (consoleKey)
{
case ConsoleKey.Delete:
for (int j = 1; j < 6; j++)
{
if (!Helpers.HelperDeleteEmptyDirectories.DeleteEmptyDirectories(args[0]))
break;
}
break;
default:
throw new Exception();
}
break;
}
}
else
throw new Exception(args[0]);
}
catch (Exception ex)
{
Console.WriteLine(string.Concat(ex.Message, Environment.NewLine, ex.StackTrace));
}
if (silentIndex > -1)
Console.WriteLine("Done. Bye");
else
{
Console.WriteLine("Done. Press 'Enter' to end");
_ = Console.ReadLine();
}
}
public static void Main(string[] args)
{
if (args is not null)
Secondary(args.ToList());
else
Secondary(new List<string>());
}
}