From f1b2495fb62ff3c35fb50a0e1bf0654c2acf481c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81d=C3=A1m=20Kov=C3=A1cs?= Date: Fri, 1 Mar 2024 22:59:40 +0100 Subject: [PATCH] Readme command --- src/Alma.App/Command/ReadMe/ReadMeCommand.cs | 88 ++++++++++++++++++++ src/Alma/Program.cs | 1 + 2 files changed, 89 insertions(+) create mode 100644 src/Alma.App/Command/ReadMe/ReadMeCommand.cs diff --git a/src/Alma.App/Command/ReadMe/ReadMeCommand.cs b/src/Alma.App/Command/ReadMe/ReadMeCommand.cs new file mode 100644 index 0000000..8a7ad23 --- /dev/null +++ b/src/Alma.App/Command/ReadMe/ReadMeCommand.cs @@ -0,0 +1,88 @@ + + +using Alma.Command.Install; +using Alma.Configuration.Repository; +using Alma.Logging; +using Alma.Services; + +namespace Alma.Command.List; + +public class ReadMeCommand : RepositoryModuleCommandBase +{ + private ILogger _logger; + + public override string CommandString => "readme"; + + public override string[] CommandAliases => Array.Empty(); + + public ReadMeCommand( + ILogger logger, + IRepositoryConfiguration repositoryConfiguration, + IModuleConfigurationResolver moduleConfigurationResolver, + IPathHelperService pathHelperService) + : base(repositoryConfiguration, pathHelperService, moduleConfigurationResolver) + { + _logger = logger; + } + + public override async Task Run(List parameters) + { + var (repoName, moduleName) = GetRepositoryAndModuleName(parameters); + if (moduleName is null) + { + _logger.LogInformation("No module specified"); + return; + } + + var (repoSource, _) = GetRepositorySourceAndTargetDirectory(repoName); + if (repoSource is null) + { + _logger.LogInformation("No repository source found"); + return; + } + + var fileFound = false; + var readmeMdPath = Path.Combine(repoSource, moduleName, "README.md"); + var readmeTxtPath = Path.Combine(repoSource, moduleName, "README.md"); + var readmePath = Path.Combine(repoSource, moduleName, "README.md"); + if(File.Exists(readmeMdPath)) + { + fileFound = true; + await PrintReadMeMd(readmeMdPath); + } + else if(File.Exists(readmeTxtPath)) + { + fileFound = true; + await PrintReadMeText(readmeMdPath); + } + else if(File.Exists(readmePath)) + { + fileFound = true; + await PrintReadMeText(readmePath); + } + + if(!fileFound) + { + _logger.LogInformation("No README file found. Supported formats: README.md, README.txt, README"); + } + } + + private async Task PrintReadMeMd(string filePath) + { + var content = await File.ReadAllLinesAsync(filePath); + foreach(var line in content) + { + //TODO: Add support for markdown + _logger.LogInformation(line); + } + } + + private async Task PrintReadMeText(string filePath) + { + var content = await File.ReadAllLinesAsync(filePath); + foreach(var line in content) + { + _logger.LogInformation(line); + } + } +} \ No newline at end of file diff --git a/src/Alma/Program.cs b/src/Alma/Program.cs index 9381555..9cbef24 100644 --- a/src/Alma/Program.cs +++ b/src/Alma/Program.cs @@ -86,6 +86,7 @@ public static class Program [Singleton(typeof(ICommand), typeof(HelpCommand))] [Singleton(typeof(ICommand), typeof(ConfigureCommand))] [Singleton(typeof(ICommand), typeof(DiagCommand))] +[Singleton(typeof(ICommand), typeof(ReadMeCommand))] [Singleton(typeof(IModuleConfigurationResolver), typeof(ModuleConfigurationResolver))] [Singleton(typeof(IMetadataHandler), typeof(MetadataHandler))] [Singleton(typeof(IShellService), typeof(ShellService))]