From 140074208f8fa35621e0a2e4d7565613c92b3ebb Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 31 Jul 2023 00:31:19 +0200 Subject: [PATCH] Merged API and CLI into one. --- API/API.csproj | 21 -- API/Program.cs | 52 ---- API/Dockerfile => Dockerfile | 6 +- Logging/Logger.cs | 2 +- Tranga-CLI/Tranga-CLI.csproj | 16 -- Tranga.sln | 12 - {API => Tranga/API}/RequestHandler.cs | 2 +- {API => Tranga/API}/Server.cs | 9 +- Tranga/TaskManager.cs | 2 +- Tranga-CLI/Tranga_Cli.cs => Tranga/Tranga.cs | 254 +++++++------------ Tranga/Tranga.csproj | 8 + 11 files changed, 108 insertions(+), 276 deletions(-) delete mode 100644 API/API.csproj delete mode 100644 API/Program.cs rename API/Dockerfile => Dockerfile (71%) delete mode 100644 Tranga-CLI/Tranga-CLI.csproj rename {API => Tranga/API}/RequestHandler.cs (99%) rename {API => Tranga/API}/Server.cs (91%) rename Tranga-CLI/Tranga_Cli.cs => Tranga/Tranga.cs (70%) diff --git a/API/API.csproj b/API/API.csproj deleted file mode 100644 index 0622e9c..0000000 --- a/API/API.csproj +++ /dev/null @@ -1,21 +0,0 @@ - - - - Exe - net7.0 - enable - enable - Linux - - - - - .dockerignore - - - - - - - - diff --git a/API/Program.cs b/API/Program.cs deleted file mode 100644 index c2e0237..0000000 --- a/API/Program.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System.Runtime.InteropServices; -using Logging; -using Tranga; -using Tranga.NotificationManagers; -using Tranga.LibraryManagers; - -namespace API; - -public static class Program -{ - public static void Main(string[] args) - { - string applicationFolderPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Tranga-API"); - string downloadFolderPath = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/Manga" : Path.Join(applicationFolderPath, "Manga"); - string logsFolderPath = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/var/logs/Tranga" : Path.Join(applicationFolderPath, "logs"); - string logFilePath = Path.Join(logsFolderPath, $"log-{DateTime.Now:dd-M-yyyy-HH-mm-ss}.txt"); - string settingsFilePath = Path.Join(applicationFolderPath, "settings.json"); - - Directory.CreateDirectory(logsFolderPath); - Logger logger = new(new[] { Logger.LoggerType.FileLogger, Logger.LoggerType.ConsoleLogger }, Console.Out, Console.Out.Encoding, logFilePath); - - logger.WriteLine("Tranga",value: "\n"+ - "-------------------------------------------\n"+ - " Starting Tranga-API\n"+ - "-------------------------------------------"); - logger.WriteLine("Tranga", "Loading settings."); - - TrangaSettings settings; - if (File.Exists(settingsFilePath)) - settings = TrangaSettings.LoadSettings(settingsFilePath); - else - settings = new TrangaSettings(downloadFolderPath, applicationFolderPath); - - Directory.CreateDirectory(settings.workingDirectory); - Directory.CreateDirectory(settings.downloadLocation); - Directory.CreateDirectory(settings.coverImageCache); - - logger.WriteLine("Tranga",$"Application-Folder: {settings.workingDirectory}"); - logger.WriteLine("Tranga",$"Settings-File-Path: {settings.settingsFilePath}"); - logger.WriteLine("Tranga",$"Download-Folder-Path: {settings.downloadLocation}"); - logger.WriteLine("Tranga",$"Logfile-Path: {logFilePath}"); - logger.WriteLine("Tranga",$"Image-Cache-Path: {settings.coverImageCache}"); - - logger.WriteLine("Tranga", "Loading Taskmanager."); - TaskManager taskManager = new (settings, logger); - - Server server = new (6531, taskManager); - foreach(NotificationManager nm in taskManager.commonObjects.notificationManagers) - nm.SendNotification("Tranga-API", "Started Tranga-API"); - } -} - diff --git a/API/Dockerfile b/Dockerfile similarity index 71% rename from API/Dockerfile rename to Dockerfile index 23e3804..1379b93 100644 --- a/API/Dockerfile +++ b/Dockerfile @@ -2,8 +2,10 @@ FROM mcr.microsoft.com/dotnet/sdk:7.0 as build-env WORKDIR /src -COPY . /src/ -RUN dotnet restore /src/API/API.csproj +COPY Tranga /src/Tranga +COPY Logging /src/Logging +COPY Tranga.sln /src +RUN dotnet restore /src/Tranga/Tranga.csproj RUN dotnet publish -c Release -o /publish FROM glax/tranga-base:latest as runtime diff --git a/Logging/Logger.cs b/Logging/Logger.cs index 19baa2a..9ca3d28 100644 --- a/Logging/Logger.cs +++ b/Logging/Logger.cs @@ -30,7 +30,7 @@ public class Logger : TextWriter { _formattedConsoleLogger = new FormattedConsoleLogger(stdOut, encoding); } - else + else if (enabledLoggers.Contains(LoggerType.ConsoleLogger) && stdOut is null) { _formattedConsoleLogger = null; throw new ArgumentException($"stdOut can not be null for LoggerType {LoggerType.ConsoleLogger}"); diff --git a/Tranga-CLI/Tranga-CLI.csproj b/Tranga-CLI/Tranga-CLI.csproj deleted file mode 100644 index 4d6dbf4..0000000 --- a/Tranga-CLI/Tranga-CLI.csproj +++ /dev/null @@ -1,16 +0,0 @@ - - - - Exe - net7.0 - Tranga_CLI - enable - enable - Linux - - - - - - - diff --git a/Tranga.sln b/Tranga.sln index 97a12c6..78a7590 100644 --- a/Tranga.sln +++ b/Tranga.sln @@ -2,12 +2,8 @@ Microsoft Visual Studio Solution File, Format Version 12.00 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tranga", ".\Tranga\Tranga.csproj", "{545E81B9-D96B-4C8F-A97F-2C02414DE566}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tranga-CLI", "Tranga-CLI\Tranga-CLI.csproj", "{4899E3B2-B259-479A-B43E-042D043E9501}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Logging", "Logging\Logging.csproj", "{415BE889-BB7D-426F-976F-8D977876A462}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API", "API\API.csproj", "{A8AB1F5F-D174-49DC-AED2-0909B93BA7B6}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -18,17 +14,9 @@ Global {545E81B9-D96B-4C8F-A97F-2C02414DE566}.Debug|Any CPU.Build.0 = Debug|Any CPU {545E81B9-D96B-4C8F-A97F-2C02414DE566}.Release|Any CPU.ActiveCfg = Release|Any CPU {545E81B9-D96B-4C8F-A97F-2C02414DE566}.Release|Any CPU.Build.0 = Release|Any CPU - {4899E3B2-B259-479A-B43E-042D043E9501}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {4899E3B2-B259-479A-B43E-042D043E9501}.Debug|Any CPU.Build.0 = Debug|Any CPU - {4899E3B2-B259-479A-B43E-042D043E9501}.Release|Any CPU.ActiveCfg = Release|Any CPU - {4899E3B2-B259-479A-B43E-042D043E9501}.Release|Any CPU.Build.0 = Release|Any CPU {415BE889-BB7D-426F-976F-8D977876A462}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {415BE889-BB7D-426F-976F-8D977876A462}.Debug|Any CPU.Build.0 = Debug|Any CPU {415BE889-BB7D-426F-976F-8D977876A462}.Release|Any CPU.ActiveCfg = Release|Any CPU {415BE889-BB7D-426F-976F-8D977876A462}.Release|Any CPU.Build.0 = Release|Any CPU - {A8AB1F5F-D174-49DC-AED2-0909B93BA7B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A8AB1F5F-D174-49DC-AED2-0909B93BA7B6}.Debug|Any CPU.Build.0 = Debug|Any CPU - {A8AB1F5F-D174-49DC-AED2-0909B93BA7B6}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A8AB1F5F-D174-49DC-AED2-0909B93BA7B6}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/API/RequestHandler.cs b/Tranga/API/RequestHandler.cs similarity index 99% rename from API/RequestHandler.cs rename to Tranga/API/RequestHandler.cs index 118e3f2..62f7f5c 100644 --- a/API/RequestHandler.cs +++ b/Tranga/API/RequestHandler.cs @@ -5,7 +5,7 @@ using Tranga; using Tranga.Connectors; using Tranga.TrangaTasks; -namespace API; +namespace Tranga.API; public class RequestHandler { diff --git a/API/Server.cs b/Tranga/API/Server.cs similarity index 91% rename from API/Server.cs rename to Tranga/API/Server.cs index c4dd5f3..ae913ed 100644 --- a/API/Server.cs +++ b/Tranga/API/Server.cs @@ -6,12 +6,13 @@ using Logging; using Newtonsoft.Json; using Tranga; -namespace API; +namespace Tranga.API; public class Server { private readonly HttpListener _listener = new (); private readonly RequestHandler _requestHandler; + private readonly TaskManager _taskManager; internal readonly Logger? logger; private readonly Regex _validUrl = @@ -19,12 +20,14 @@ public class Server public Server(int port, TaskManager taskManager, Logger? logger = null) { this.logger = logger; + this._taskManager = taskManager; if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) this._listener.Prefixes.Add($"http://*:{port}/"); else this._listener.Prefixes.Add($"http://localhost:{port}/"); this._requestHandler = new RequestHandler(taskManager, this); - Listen(); + Thread listenThread = new Thread(Listen); + listenThread.Start(); } private void Listen() @@ -32,7 +35,7 @@ public class Server this._listener.Start(); foreach (string prefix in this._listener.Prefixes) this.logger?.WriteLine(this.GetType().ToString(), $"Listening on {prefix}"); - while (this._listener.IsListening) + while (this._listener.IsListening && _taskManager._continueRunning) { HttpListenerContext context = this._listener.GetContextAsync().Result; Task t = new (() => diff --git a/Tranga/TaskManager.cs b/Tranga/TaskManager.cs index 132f929..3ab8ba2 100644 --- a/Tranga/TaskManager.cs +++ b/Tranga/TaskManager.cs @@ -13,7 +13,7 @@ public class TaskManager public HashSet collection = new(); private HashSet _allTasks = new(); private readonly Dictionary _runningTasks = new (); - private bool _continueRunning = true; + public bool _continueRunning = true; private readonly Connector[] _connectors; public TrangaSettings settings { get; } public CommonObjects commonObjects { get; init; } diff --git a/Tranga-CLI/Tranga_Cli.cs b/Tranga/Tranga.cs similarity index 70% rename from Tranga-CLI/Tranga_Cli.cs rename to Tranga/Tranga.cs index a131b5e..5757d9c 100644 --- a/Tranga-CLI/Tranga_Cli.cs +++ b/Tranga/Tranga.cs @@ -1,139 +1,59 @@ using System.Globalization; +using System.Runtime.InteropServices; using Logging; -using Tranga; +using Tranga.API; using Tranga.Connectors; -using Tranga.LibraryManagers; using Tranga.NotificationManagers; using Tranga.TrangaTasks; -namespace Tranga_CLI; +namespace Tranga; -/* - * This is written with pure hatred for readability. - * At some point do this properly. - * Read at own risk. - */ - -public static class Tranga_Cli +public static class Tranga { public static void Main(string[] args) { - string applicationFolderPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Tranga"); - string logsFolderPath = Path.Join(applicationFolderPath, "logs"); + string applicationFolderPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Tranga-API"); + string downloadFolderPath = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/Manga" : Path.Join(applicationFolderPath, "Manga"); + string logsFolderPath = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "/var/log/Tranga" : Path.Join(applicationFolderPath, "log"); string logFilePath = Path.Join(logsFolderPath, $"log-{DateTime.Now:dd-M-yyyy-HH-mm-ss}.txt"); string settingsFilePath = Path.Join(applicationFolderPath, "settings.json"); - - Directory.CreateDirectory(applicationFolderPath); + + Directory.CreateDirectory(logsFolderPath); + Logger logger = new(new[] { Logger.LoggerType.FileLogger }, Console.Out, Console.Out.Encoding, logFilePath); - Console.WriteLine($"Logfile-Path: {logFilePath}"); - Console.WriteLine($"Settings-File-Path: {settingsFilePath}"); + logger.WriteLine("Tranga",value: "\n"+ + "-------------------------------------------\n"+ + " Starting Tranga-API\n"+ + "-------------------------------------------"); + logger.WriteLine("Tranga", "Loading settings."); - Logger logger = new(new[] { Logger.LoggerType.FileLogger }, null, Console.Out.Encoding, logFilePath); + TrangaSettings settings = File.Exists(settingsFilePath) + ? TrangaSettings.LoadSettings(settingsFilePath) + : new TrangaSettings(downloadFolderPath, applicationFolderPath); + + Directory.CreateDirectory(settings.workingDirectory); + Directory.CreateDirectory(settings.downloadLocation); + Directory.CreateDirectory(settings.coverImageCache); + + logger.WriteLine("Tranga",$"Application-Folder: {settings.workingDirectory}"); + logger.WriteLine("Tranga",$"Settings-File-Path: {settings.settingsFilePath}"); + logger.WriteLine("Tranga",$"Download-Folder-Path: {settings.downloadLocation}"); + logger.WriteLine("Tranga",$"Logfile-Path: {logFilePath}"); + logger.WriteLine("Tranga",$"Image-Cache-Path: {settings.coverImageCache}"); + + logger.WriteLine("Tranga", "Loading Taskmanager."); + TaskManager taskManager = new (settings, logger); - logger.WriteLine("Tranga_CLI", "Loading Taskmanager."); - TrangaSettings settings = File.Exists(settingsFilePath) ? TrangaSettings.LoadSettings(settingsFilePath, logger) : new TrangaSettings(Directory.GetCurrentDirectory(), applicationFolderPath, new HashSet(), new HashSet(), logger); - - - settings.logger?.WriteLine("Tranga_CLI", "User Input"); - Console.WriteLine($"Output folder path [{settings.downloadLocation}]:"); - string? tmpPath = Console.ReadLine(); - while(tmpPath is null) - tmpPath = Console.ReadLine(); - if (tmpPath.Length > 0) - settings.UpdateSettings(TrangaSettings.UpdateField.DownloadLocation, tmpPath); - - Console.WriteLine($"Komga BaseURL [{settings.libraryManagers.FirstOrDefault(lm => lm.GetType() == typeof(Komga))?.baseUrl}]:"); - string? tmpUrlKomga = Console.ReadLine(); - while (tmpUrlKomga is null) - tmpUrlKomga = Console.ReadLine(); - if (tmpUrlKomga.Length > 0) - { - Console.WriteLine("Username:"); - string? tmpKomgaUser = Console.ReadLine(); - while (tmpKomgaUser is null || tmpKomgaUser.Length < 1) - tmpKomgaUser = Console.ReadLine(); - - Console.WriteLine("Password:"); - string tmpKomgaPass = string.Empty; - ConsoleKey key; - do - { - var keyInfo = Console.ReadKey(intercept: true); - key = keyInfo.Key; - - if (key == ConsoleKey.Backspace && tmpKomgaPass.Length > 0) - { - Console.Write("\b \b"); - tmpKomgaPass = tmpKomgaPass[0..^1]; - } - else if (!char.IsControl(keyInfo.KeyChar)) - { - Console.Write("*"); - tmpKomgaPass += keyInfo.KeyChar; - } - } while (key != ConsoleKey.Enter); - - settings.UpdateSettings(TrangaSettings.UpdateField.Komga, tmpUrlKomga, tmpKomgaUser, tmpKomgaPass); - } + Server _ = new (6531, taskManager); + foreach(NotificationManager nm in taskManager.commonObjects.notificationManagers) + nm.SendNotification("Tranga-API", "Started Tranga-API"); - Console.WriteLine($"Kavita BaseURL [{settings.libraryManagers.FirstOrDefault(lm => lm.GetType() == typeof(Kavita))?.baseUrl}]:"); - string? tmpUrlKavita = Console.ReadLine(); - while (tmpUrlKavita is null) - tmpUrlKavita = Console.ReadLine(); - if (tmpUrlKavita.Length > 0) - { - Console.WriteLine("Username:"); - string? tmpKavitaUser = Console.ReadLine(); - while (tmpKavitaUser is null || tmpKavitaUser.Length < 1) - tmpKavitaUser = Console.ReadLine(); - - Console.WriteLine("Password:"); - string tmpKavitaPass = string.Empty; - ConsoleKey key; - do - { - var keyInfo = Console.ReadKey(intercept: true); - key = keyInfo.Key; - - if (key == ConsoleKey.Backspace && tmpKavitaPass.Length > 0) - { - Console.Write("\b \b"); - tmpKavitaPass = tmpKavitaPass[0..^1]; - } - else if (!char.IsControl(keyInfo.KeyChar)) - { - Console.Write("*"); - tmpKavitaPass += keyInfo.KeyChar; - } - } while (key != ConsoleKey.Enter); - - settings.UpdateSettings(TrangaSettings.UpdateField.Kavita, tmpUrlKavita, tmpKavitaUser, tmpKavitaPass); - } - - Console.WriteLine($"Gotify BaseURL [{((Gotify?)settings.notificationManagers.FirstOrDefault(lm => lm.GetType() == typeof(Gotify)))?.endpoint}]:"); - string? tmpGotifyUrl = Console.ReadLine(); - while (tmpGotifyUrl is null) - tmpGotifyUrl = Console.ReadLine(); - if (tmpGotifyUrl.Length > 0) - { - Console.WriteLine("AppToken:"); - string? tmpGotifyAppToken = Console.ReadLine(); - while (tmpGotifyAppToken is null || tmpGotifyAppToken.Length < 1) - tmpGotifyAppToken = Console.ReadLine(); - - settings.UpdateSettings(TrangaSettings.UpdateField.Gotify, tmpGotifyUrl, tmpGotifyAppToken); - } - - logger.WriteLine("Tranga_CLI", "Loaded."); - foreach(NotificationManager nm in settings.notificationManagers) - nm.SendNotification("Tranga", "Loaded."); - TaskMode(settings, logger); + TaskMode(taskManager, logger); } - - private static void TaskMode(TrangaSettings settings, Logger logger) + + private static void TaskMode(TaskManager taskManager, Logger logger) { - TaskManager taskManager = new (settings); ConsoleKey selection = ConsoleKey.EraseEndOfFile; PrintMenu(taskManager, taskManager.settings.downloadLocation); while (selection != ConsoleKey.Q) @@ -160,22 +80,22 @@ public static class Tranga_Cli Console.ReadKey(); break; case ConsoleKey.C: - CreateTask(taskManager, logger); + CreateTask(taskManager); Console.WriteLine("Press any key."); Console.ReadKey(); break; case ConsoleKey.D: - DeleteTask(taskManager, logger); + DeleteTask(taskManager); Console.WriteLine("Press any key."); Console.ReadKey(); break; case ConsoleKey.E: - ExecuteTaskNow(taskManager, logger); + ExecuteTaskNow(taskManager); Console.WriteLine("Press any key."); Console.ReadKey(); break; case ConsoleKey.S: - SearchTasks(taskManager, logger); + SearchTasks(taskManager); Console.WriteLine("Press any key."); Console.ReadKey(); break; @@ -241,7 +161,7 @@ public static class Tranga_Cli // ReSharper disable once RedundantArgumentDefaultValue Better readability taskManager.Shutdown(false); } - + private static void PrintMenu(TaskManager taskManager, string folderPath) { int taskCount = taskManager.GetAllTasks().Length; @@ -258,10 +178,10 @@ public static class Tranga_Cli Console.WriteLine($"{"M: New Download Manga Task",-30}{"", -30}{"", -30}"); Console.WriteLine($"{"",-30}{"F: Show Log",-30}{"Q: Exit",-30}"); } - - private static void PrintTasks(TrangaTask[] tasks, Logger logger) + + private static void PrintTasks(TrangaTask[] tasks, Logger? logger) { - logger.WriteLine("Tranga_CLI", "Printing Tasks"); + logger?.WriteLine("Tranga_CLI", "Printing Tasks"); int taskCount = tasks.Length; int taskRunningCount = tasks.Count(task => task.state == TrangaTask.ExecutionState.Running); int taskEnqueuedCount = tasks.Count(task => task.state == TrangaTask.ExecutionState.Enqueued); @@ -280,19 +200,19 @@ public static class Tranga_Cli } - private static TrangaTask[] SelectTasks(TrangaTask[] tasks, Logger logger) + private static TrangaTask[] SelectTasks(TrangaTask[] tasks, Logger? logger) { - logger.WriteLine("Tranga_CLI", "Menu: Select task"); + logger?.WriteLine("Tranga_CLI", "Menu: Select task"); if (tasks.Length < 1) { Console.Clear(); Console.WriteLine("There are no available Tasks."); - logger.WriteLine("Tranga_CLI", "No available Tasks."); + logger?.WriteLine("Tranga_CLI", "No available Tasks."); return Array.Empty(); } PrintTasks(tasks, logger); - logger.WriteLine("Tranga_CLI", "Selecting Task to Remove (from queue)"); + logger?.WriteLine("Tranga_CLI", "Selecting Task to Remove (from queue)"); Console.WriteLine("Enter q to abort"); Console.WriteLine($"Select Task(s) (0-{tasks.Length - 1}):"); @@ -304,7 +224,7 @@ public static class Tranga_Cli { Console.Clear(); Console.WriteLine("aborted."); - logger.WriteLine("Tranga_CLI", "aborted"); + logger?.WriteLine("Tranga_CLI", "aborted"); return Array.Empty(); } @@ -330,7 +250,7 @@ public static class Tranga_Cli if (connector is null) return; - Publication? publication = SelectPublication(taskManager, connector, logger); + Publication? publication = SelectPublication(taskManager, connector); if (publication is null) return; @@ -386,10 +306,10 @@ public static class Tranga_Cli } } - private static void CreateTask(TaskManager taskManager, Logger logger) + private static void CreateTask(TaskManager taskManager) { - logger.WriteLine("Tranga_CLI", "Menu: Creating Task"); - TrangaTask.Task? tmpTask = SelectTaskType(logger); + taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "Menu: Creating Task"); + TrangaTask.Task? tmpTask = SelectTaskType(taskManager.commonObjects.logger); if (tmpTask is null) return; TrangaTask.Task task = (TrangaTask.Task)tmpTask; @@ -397,7 +317,7 @@ public static class Tranga_Cli Connector? connector = null; if (task != TrangaTask.Task.UpdateLibraries) { - connector = SelectConnector(taskManager.GetAvailableConnectors().Values.ToArray(), logger); + connector = SelectConnector(taskManager.GetAvailableConnectors().Values.ToArray(), taskManager.commonObjects.logger); if (connector is null) return; } @@ -405,22 +325,22 @@ public static class Tranga_Cli Publication? publication = null; if (task != TrangaTask.Task.UpdateLibraries) { - publication = SelectPublication(taskManager, connector!, logger); + publication = SelectPublication(taskManager, connector!); if (publication is null) return; } if (task is TrangaTask.Task.MonitorPublication) { - TimeSpan reoccurrence = SelectReoccurrence(logger); - logger.WriteLine("Tranga_CLI", "Sending Task to TaskManager"); + TimeSpan reoccurrence = SelectReoccurrence(taskManager.commonObjects.logger); + taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "Sending Task to TaskManager"); TrangaTask newTask = new MonitorPublicationTask(connector!.name, (Publication)publication!, reoccurrence, "en"); taskManager.AddTask(newTask); Console.WriteLine(newTask); }else if (task is TrangaTask.Task.DownloadChapter) { - foreach (Chapter chapter in SelectChapters(connector!, (Publication)publication!, logger)) + foreach (Chapter chapter in SelectChapters(connector!, (Publication)publication!, taskManager.commonObjects.logger)) { TrangaTask newTask = new DownloadChapterTask(connector!.name, (Publication)publication, chapter, "en"); taskManager.AddTask(newTask); @@ -429,31 +349,31 @@ public static class Tranga_Cli } } - private static void ExecuteTaskNow(TaskManager taskManager, Logger logger) + private static void ExecuteTaskNow(TaskManager taskManager) { - logger.WriteLine("Tranga_CLI", "Menu: Executing Task"); + taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "Menu: Executing Task"); TrangaTask[] tasks = taskManager.GetAllTasks().Where(nTask => nTask.state is not TrangaTask.ExecutionState.Running).ToArray(); - TrangaTask[] selectedTasks = SelectTasks(tasks, logger); - logger.WriteLine("Tranga_CLI", $"Sending {selectedTasks.Length} Tasks to TaskManager"); + TrangaTask[] selectedTasks = SelectTasks(tasks, taskManager.commonObjects.logger); + taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", $"Sending {selectedTasks.Length} Tasks to TaskManager"); foreach(TrangaTask task in selectedTasks) taskManager.ExecuteTaskNow(task); } - private static void DeleteTask(TaskManager taskManager, Logger logger) + private static void DeleteTask(TaskManager taskManager) { - logger.WriteLine("Tranga_CLI", "Menu: Delete Task"); + taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "Menu: Delete Task"); TrangaTask[] tasks = taskManager.GetAllTasks(); - TrangaTask[] selectedTasks = SelectTasks(tasks, logger); - logger.WriteLine("Tranga_CLI", $"Sending {selectedTasks.Length} Tasks to TaskManager"); + TrangaTask[] selectedTasks = SelectTasks(tasks, taskManager.commonObjects.logger); + taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", $"Sending {selectedTasks.Length} Tasks to TaskManager"); foreach(TrangaTask task in selectedTasks) taskManager.DeleteTask(task); } - private static TrangaTask.Task? SelectTaskType(Logger logger) + private static TrangaTask.Task? SelectTaskType(Logger? logger) { - logger.WriteLine("Tranga_CLI", "Menu: Select TaskType"); + logger?.WriteLine("Tranga_CLI", "Menu: Select TaskType"); Console.Clear(); string[] taskNames = Enum.GetNames(); @@ -473,7 +393,7 @@ public static class Tranga_Cli { Console.Clear(); Console.WriteLine("aborted."); - logger.WriteLine("Tranga_CLI", "aborted."); + logger?.WriteLine("Tranga_CLI", "aborted."); return null; } @@ -486,22 +406,22 @@ public static class Tranga_Cli catch (Exception e) { Console.WriteLine($"Exception: {e.Message}"); - logger.WriteLine("Tranga_CLI", e.Message); + logger?.WriteLine("Tranga_CLI", e.Message); } return null; } - private static TimeSpan SelectReoccurrence(Logger logger) + private static TimeSpan SelectReoccurrence(Logger? logger) { - logger.WriteLine("Tranga_CLI", "Menu: Select Reoccurrence"); + logger?.WriteLine("Tranga_CLI", "Menu: Select Reoccurrence"); Console.WriteLine("Select reoccurrence Timer (Format hh:mm:ss):"); return TimeSpan.Parse(Console.ReadLine()!, new CultureInfo("en-US")); } - private static Chapter[] SelectChapters(Connector connector, Publication publication, Logger logger) + private static Chapter[] SelectChapters(Connector connector, Publication publication, Logger? logger) { - logger.WriteLine("Tranga_CLI", "Menu: Select Chapters"); + logger?.WriteLine("Tranga_CLI", "Menu: Select Chapters"); Chapter[] availableChapters = connector.GetChapters(publication, "en"); int cIndex = 0; Console.WriteLine("Chapters:"); @@ -540,9 +460,9 @@ public static class Tranga_Cli return connector.SelectChapters(publication, selectedChapters); } - private static Connector? SelectConnector(Connector[] connectors, Logger logger) + private static Connector? SelectConnector(Connector[] connectors, Logger? logger) { - logger.WriteLine("Tranga_CLI", "Menu: Select Connector"); + logger?.WriteLine("Tranga_CLI", "Menu: Select Connector"); Console.Clear(); int cIndex = 0; @@ -561,7 +481,7 @@ public static class Tranga_Cli { Console.Clear(); Console.WriteLine("aborted."); - logger.WriteLine("Tranga_CLI", "aborted."); + logger?.WriteLine("Tranga_CLI", "aborted."); return null; } @@ -573,15 +493,15 @@ public static class Tranga_Cli catch (Exception e) { Console.WriteLine($"Exception: {e.Message}"); - logger.WriteLine("Tranga_CLI", e.Message); + logger?.WriteLine("Tranga_CLI", e.Message); } return null; } - private static Publication? SelectPublication(TaskManager taskManager, Connector connector, Logger logger) + private static Publication? SelectPublication(TaskManager taskManager, Connector connector) { - logger.WriteLine("Tranga_CLI", "Menu: Select Publication"); + taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "Menu: Select Publication"); Console.Clear(); Console.WriteLine($"Connector: {connector.name}"); @@ -592,7 +512,7 @@ public static class Tranga_Cli if (publications.Length < 1) { - logger.WriteLine("Tranga_CLI", "No publications returned"); + taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "No publications returned"); Console.WriteLine($"No publications for query '{query}' returned;"); return null; } @@ -613,7 +533,7 @@ public static class Tranga_Cli { Console.Clear(); Console.WriteLine("aborted."); - logger.WriteLine("Tranga_CLI", "aborted."); + taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "aborted."); return null; } @@ -625,21 +545,21 @@ public static class Tranga_Cli catch (Exception e) { Console.WriteLine($"Exception: {e.Message}"); - logger.WriteLine("Tranga_CLI", e.Message); + taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", e.Message); } return null; } - private static void SearchTasks(TaskManager taskManager, Logger logger) + private static void SearchTasks(TaskManager taskManager) { - logger.WriteLine("Tranga_CLI", "Menu: Search task"); + taskManager.commonObjects.logger?.WriteLine("Tranga_CLI", "Menu: Search task"); Console.Clear(); Console.WriteLine("Enter search query:"); string? query = Console.ReadLine(); while (query is null || query.Length < 4) query = Console.ReadLine(); PrintTasks(taskManager.GetAllTasks().Where(qTask => - qTask.ToString().ToLower().Contains(query, StringComparison.OrdinalIgnoreCase)).ToArray(), logger); + qTask.ToString().ToLower().Contains(query, StringComparison.OrdinalIgnoreCase)).ToArray(), taskManager.commonObjects.logger); } } \ No newline at end of file diff --git a/Tranga/Tranga.csproj b/Tranga/Tranga.csproj index a2edb49..2e4242b 100644 --- a/Tranga/Tranga.csproj +++ b/Tranga/Tranga.csproj @@ -4,6 +4,7 @@ net7.0 enable enable + Exe @@ -16,4 +17,11 @@ + + + .dockerignore + Dockerfile + + +