BaseLocalCommand.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Abstractions;
  4. using System.Linq;
  5. using System.Text.Json;
  6. using BO.Console.Options;
  7. using Quadarax.Foundation.Core.Console;
  8. using Quadarax.Foundation.Core.Data.Interface.Entity;
  9. using Quadarax.Foundation.Core.Logging;
  10. using Quadarax.Foundation.Core.Object.Extensions;
  11. using Quadarax.Foundation.Core.QConsole;
  12. using Quadarax.Foundation.Core.QConsole.Argument;
  13. using Quadarax.Foundation.Core.QConsole.Command.Base;
  14. namespace BO.Console.Commands.Base
  15. {
  16. internal abstract class BaseLocalCommand : AbstractCommand, ILogHandler
  17. {
  18. #region *** Constants ***
  19. protected const string CS_ARG_NAME_DUMP = "dump";
  20. private const string CS_ARG_HINT_DUMP = "<is_dump_reponses>";
  21. private const string CS_ARG_DESC_DUMP = "Flag if set dumps incomming json messages.";
  22. protected const string CS_ARG_NAME_DEBUG = "dbg";
  23. private const string CS_ARG_HINT_DEBUG = "<is_debug_mode>";
  24. private const string CS_ARG_DESC_DEBUG = "Flag if set writes debug messages.";
  25. protected const string CS_ARG_NAME_FORCE = "force";
  26. private const string CS_ARG_HINT_FORCE = "<is_force>";
  27. private const string CS_ARG_DESC_FORCE = "Flag if set, then overrite existing entity, otherwise cancel operation.";
  28. #endregion
  29. protected Configuration Configuration { get; private set; }
  30. protected bool? IsForce { get; private set; }
  31. protected IFileSystem FileSystem { get; private set; }
  32. protected BaseLocalCommand(Engine engine) : base(engine)
  33. {
  34. }
  35. protected override void OnInitialize()
  36. {
  37. base.OnInitialize();
  38. Configuration = JsonSerializer.Deserialize<Configuration>(FileSystem.File.ReadAllText(Constants.CS_CONFIGURATION_FILE));
  39. }
  40. protected override void OnValidateArguments()
  41. {
  42. base.OnValidateArguments();
  43. if (ContainsArgument(CS_ARG_NAME_FORCE))
  44. {
  45. IsForce = WasArgumentSpecified(CS_ARG_NAME_FORCE);
  46. if (IsForce.GetValueOrDefault(false))
  47. WriteWarning("Force flag is set!");
  48. }
  49. if (WasArgumentSpecified(CS_ARG_NAME_DEBUG))
  50. DebugConsoleWriter.IsWriteDebugEnabled = true;
  51. }
  52. protected override IEnumerable<AbstractArgument> OnSetupArguments()
  53. {
  54. FileSystem = new FileSystem();
  55. var args = base.OnSetupArguments().ToList();
  56. args.Add(new FlagArgument(CS_ARG_NAME_DUMP, CS_ARG_DESC_DUMP, CS_ARG_HINT_DUMP, false));
  57. args.Add(new FlagArgument(CS_ARG_NAME_DEBUG, CS_ARG_DESC_DEBUG, CS_ARG_HINT_DEBUG, false));
  58. return args;
  59. }
  60. protected void AppendForceArgument(IList<AbstractArgument> args, string alternativeUserDescription = null)
  61. {
  62. args.Add(new FlagArgument(CS_ARG_NAME_FORCE, string.IsNullOrEmpty(alternativeUserDescription) ? CS_ARG_DESC_FORCE : alternativeUserDescription, CS_ARG_HINT_FORCE, false));
  63. }
  64. public void Log(LogSeverityEnum type, string message, Exception e = null)
  65. {
  66. WriteInfo($"{type}: {message}" + (e == null ? "" : e.Message));
  67. }
  68. protected void WriteDtoToConsole(IDto dto)
  69. {
  70. var props = dto.GetAllPropertyValues();
  71. using (var wName = new ConsoleWriter(ConsoleColor.White))
  72. {
  73. using (var wValue = new ConsoleWriter(ConsoleColor.Yellow))
  74. {
  75. var maxKeyLength = props.Keys.Max(x => x.Length);
  76. foreach (var prop in props)
  77. {
  78. wName.Write(prop.Key);
  79. wName.Write(new string(' ', maxKeyLength - prop.Key.Length) + " :\t");
  80. wValue.WriteLine(prop.Value?.ToString());
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }