| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- using Quadarax.Foundation.Core.QConsole.Argument;
- using Quadarax.Foundation.Core.QConsole.Command.Base;
- using System.IO.Abstractions;
- using System.Text;
- namespace qdr.app.tools.qfu.Commands.Aspects
- {
- internal class FilesAspect
- {
- #region *** Constants ***
- private const string ARG_FILES_SEP = "|";
- public const string ARG_FILES_NAME = "files";
- private const string ARG_FILES_HINT = "files";
- private const string ARG_FILES_DESCR = "One or more files separated by character pipe (" + ARG_FILES_SEP + ")";
- private const int ARG_SRC_ORD = 1;
- public const string ARG_PATHENS_NAME = "pe";
- private const string ARG_PATHENS_HINT = "path_ensure";
- private const string ARG_PATHENS_DESCR = "Flag if set, then path of the files will be ensured (created if not exists). Otherwise fails.";
- #endregion
- #region *** Public Operations ***
- public static IEnumerable<AbstractArgument> SetupArguments()
- {
- return
- [
- new OrdinalArgument(ARG_SRC_ORD, false, ARG_FILES_NAME, ARG_FILES_DESCR, ARG_FILES_HINT,Quadarax.Foundation.Core.QConsole.Value.TypeValuesEnum.String, string.Empty, true),
- new FlagArgument(ARG_PATHENS_NAME, ARG_PATHENS_DESCR, ARG_PATHENS_HINT,false)
- ];
- }
- public static bool EnsureFilePath(IFileSystem fs, string filePath)
- {
- if (string.IsNullOrEmpty(filePath))
- throw new ArgumentException("File path cannot be null or empty.", nameof(filePath));
- var directory = Path.GetDirectoryName(filePath);
- if (string.IsNullOrEmpty(directory) || fs.Directory.Exists(directory))
- return true;
- fs.Directory.CreateDirectory(directory);
- return false;
- }
- public static string[] GetFiles(AbstractCommand context, IFileSystem fs, bool checkValidity = false)
- {
- if (context == null)
- throw new ArgumentNullException(nameof(context), "Command context cannot be null.");
- var filesArgument = context.Arguments.First(x => string.Equals(x.Code, ARG_FILES_NAME));
- if (filesArgument.Value.Get() == null)
- throw new ArgumentException($"Argument '{ARG_FILES_NAME}' is not set.", ARG_FILES_NAME);
- var files = ParseFiles(filesArgument.Value.Get()?.ToString()!);
- if (checkValidity)
- {
- var sb = new StringBuilder();
- foreach (var file in files)
- {
- if (string.IsNullOrEmpty(file))
- continue;
- if (!fs.File.Exists(file) && !fs.Directory.Exists(file))
- sb.AppendLine(file);
- }
- if (sb.Length > 0)
- throw new FileNotFoundException($"The following files do not exist:\n{sb.ToString()}");
- }
- return files;
- }
- public static string[] ParseFiles(string filesArgument)
- {
- if (string.IsNullOrEmpty(filesArgument))
- throw new ArgumentException($"Argument '{ARG_FILES_NAME}' is not defined or empty.", ARG_FILES_NAME);
- return filesArgument.Split(new[] { ARG_FILES_SEP }, StringSplitOptions.RemoveEmptyEntries)
- .Select(f => f.Trim())
- .Where(f => !string.IsNullOrEmpty(f))
- .ToArray();
- }
- #endregion
- }
- }
|