diff --git a/CLI/CLI.csproj b/CLI/CLI.csproj new file mode 100644 index 0000000..abf151f --- /dev/null +++ b/CLI/CLI.csproj @@ -0,0 +1,18 @@ + + + + Exe + net7.0 + enable + enable + + + + + + + + + + + diff --git a/CLI/Program.cs b/CLI/Program.cs new file mode 100644 index 0000000..1689ce5 --- /dev/null +++ b/CLI/Program.cs @@ -0,0 +1,152 @@ +using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; +using Logging; +using Spectre.Console; +using Spectre.Console.Cli; +using Tranga; + +var app = new CommandApp(); +return app.Run(args); + +internal sealed class TrangaCli : Command +{ + public sealed class Settings : CommandSettings + { + [Description("Directory to which downloaded Manga are saved")] + [CommandOption("-d|--downloadLocation")] + [DefaultValue(null)] + public string? downloadLocation { get; init; } + + [Description("Directory in which application-data is saved")] + [CommandOption("-w|--workingDirectory")] + [DefaultValue(null)] + public string? workingDirectory { get; init; } + + [Description("Enables the file-logger")] + [CommandOption("-f")] + [DefaultValue(null)] + public bool? fileLogger { get; init; } + + [Description("Path to save logfile to")] + [CommandOption("-l|--fPath")] + [DefaultValue(null)] + public string? fileLoggerPath { get; init; } + + [Description("Port on which to run API on")] + [CommandOption("-p|--port")] + [DefaultValue(null)] + public int? apiPort { get; init; } + } + + public override int Execute([NotNull] CommandContext context, [NotNull] Settings settings) + { + List enabledLoggers = new(); + if(settings.fileLogger.HasValue && settings.fileLogger.Value == true) + enabledLoggers.Add(Logger.LoggerType.FileLogger); + string? logFilePath = settings.fileLoggerPath ?? "";//TODO path + Logger logger = new(enabledLoggers.ToArray(), Console.Out, Console.OutputEncoding, logFilePath); + + TrangaSettings trangaSettings = new (settings.downloadLocation, settings.workingDirectory, settings.apiPort); + + Directory.CreateDirectory(trangaSettings.downloadLocation); + Directory.CreateDirectory(trangaSettings.workingDirectory); + + Tranga.Tranga? api = null; + + Thread trangaApi = new Thread(() => + { + api = new(logger, trangaSettings); + }); + trangaApi.Start(); + + HttpClient client = new(); + + bool exit = false; + while (!exit) + { + string menuSelect = AnsiConsole.Prompt( + new SelectionPrompt() + .Title("Menu") + .PageSize(10) + .MoreChoicesText("Up/Down") + .AddChoices(new[] + { + "CustomRequest", + "Log", + "Exit" + })); + + switch (menuSelect) + { + case "CustomRequest": + string requestType = AnsiConsole.Prompt( + new SelectionPrompt() + .Title("Request Type") + .AddChoices(new[] + { + "GET", + "POST", + "DELETE" + })); + HttpMethod method = requestType == "GET" ? HttpMethod.Get : + requestType == "DELETE" ? HttpMethod.Delete : HttpMethod.Post; + string requestPath = AnsiConsole.Prompt( + new TextPrompt("Request Path:")); + List> parameters = new(); + while (AnsiConsole.Confirm("Add Parameter?")) + { + string name = AnsiConsole.Ask("Parameter Name:"); + string value = AnsiConsole.Ask("Parameter Value:"); + parameters.Add(new ValueTuple(name, value)); + } + + string requestString = $"http://localhost:{trangaSettings.apiPortNumber}/{requestPath}"; + if (parameters.Any()) + { + requestString += "?"; + foreach (ValueTuple parameter in parameters) + requestString += $"{parameter.Item1}={parameter.Item2}&"; + } + + HttpRequestMessage request = new HttpRequestMessage(method, requestString); + AnsiConsole.WriteLine($"Request: {request.Method} {request.RequestUri}"); + HttpResponseMessage response; + if (AnsiConsole.Confirm("Send Request?")) + response = client.Send(request); + else break; + AnsiConsole.WriteLine(response.Content.ReadAsStringAsync().Result); + break; + case "Log": + List lines = logger.Tail(10).ToList(); + Rows rows = new Rows(lines.Select(line => new Text(line))); + + AnsiConsole.Live(rows).Start(context => + { + bool running = true; + while (running) + { + string[] newLines = logger.GetNewLines(); + if (newLines.Length > 0) + { + lines.AddRange(newLines); + rows = new Rows(lines.Select(line => new Text(line))); + context.UpdateTarget(rows); + } + Thread.Sleep(100); + if (Console.KeyAvailable) + running = false; + } + }); + break; + case "Exit": + exit = true; + break; + } + } + + if (api is not null) + api.keepRunning = false; + + return 0; + } +} \ No newline at end of file diff --git a/Tranga.sln b/Tranga.sln index 78a7590..501c3c3 100644 --- a/Tranga.sln +++ b/Tranga.sln @@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tranga", ".\Tranga\Tranga.c EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Logging", "Logging\Logging.csproj", "{415BE889-BB7D-426F-976F-8D977876A462}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CLI", "CLI\CLI.csproj", "{4324C816-F9D2-468F-8ED6-397FE2F0DCB3}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -18,5 +20,9 @@ Global {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 + {4324C816-F9D2-468F-8ED6-397FE2F0DCB3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4324C816-F9D2-468F-8ED6-397FE2F0DCB3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4324C816-F9D2-468F-8ED6-397FE2F0DCB3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4324C816-F9D2-468F-8ED6-397FE2F0DCB3}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal