| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- using Quadarax.Foundation.Core.QConsole.Argument;
- using Quadarax.Foundation.Core.QConsole.Command.Base;
- using System;
- using System.Collections.Generic;
- using System.IO.Abstractions;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace qdr.app.tools.qfu.Commands.Aspects
- {
- internal class ListAspect
- {
- #region *** Constants ***
- private const string ARG_LIST_NAME = "list";
- private const string ARG_LIST_HINT = "list";
- private const string ARG_LIST_DESCR = "Flag if defined, then first argument of target file represents text file with new line defined full file paths";
- #endregion
- #region *** Public Operations ***
- public static IEnumerable<AbstractArgument> SetupArguments()
- {
- return
- [
- new FlagArgument(ARG_LIST_NAME, ARG_LIST_DESCR, ARG_LIST_HINT, false)
- ];
- }
- public static string[] GetFiles(AbstractCommand context, IFileSystem fs,IEnumerable<string> defaultResult, 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_LIST_NAME));
- if (filesArgument.Value.Get() == null)
- throw new ArgumentException($"Argument '{ARG_LIST_NAME}' is not set.", ARG_LIST_NAME);
- if (!(bool)filesArgument.Value.Get()!)
- return defaultResult.ToArray();
- var listFileName = defaultResult.First();
- if (!fs.File.Exists(listFileName))
- throw new FileNotFoundException($"List file '{listFileName}' does not exist.");
- var files = fs.File.ReadAllLines(listFileName).Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
- 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;
- }
- #endregion
- }
- }
|