BaseLocalCommand.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.ProcessServer.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.ProcessServer.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. #endregion
  26. protected Configuration Configuration { get; private set; }
  27. protected bool? IsForce { get; private set; }
  28. protected bool IsDebug { get; private set; }
  29. protected IFileSystem FileSystem { get; private set; }
  30. protected BaseLocalCommand(Engine engine) : base(engine)
  31. {
  32. }
  33. protected override void OnInitialize()
  34. {
  35. base.OnInitialize();
  36. Configuration = JsonSerializer.Deserialize<Configuration>(FileSystem.File.ReadAllText(Constants.CS_CONFIGURATION_FILE));
  37. }
  38. protected override void OnValidateArguments()
  39. {
  40. base.OnValidateArguments();
  41. IsDebug = WasArgumentSpecified(CS_ARG_NAME_DEBUG);
  42. if (IsDebug)
  43. DebugConsoleWriter.IsWriteDebugEnabled = true;
  44. }
  45. protected override IEnumerable<AbstractArgument> OnSetupArguments()
  46. {
  47. FileSystem = new FileSystem();
  48. var args = base.OnSetupArguments().ToList();
  49. args.Add(new FlagArgument(CS_ARG_NAME_DUMP, CS_ARG_DESC_DUMP, CS_ARG_HINT_DUMP, false));
  50. args.Add(new FlagArgument(CS_ARG_NAME_DEBUG, CS_ARG_DESC_DEBUG, CS_ARG_HINT_DEBUG, false));
  51. return args;
  52. }
  53. public void Log(LogSeverityEnum type, string message, Exception e = null)
  54. {
  55. if ((type == LogSeverityEnum.Trace || type == LogSeverityEnum.Debug) && !IsDebug)
  56. return;
  57. WriteInfo($"{type}: {message}" + (e == null ? "" : e.Message));
  58. }
  59. protected void WriteDtoToConsole(IDto dto)
  60. {
  61. var props = dto.GetAllPropertyValues();
  62. using (var wName = new ConsoleWriter(ConsoleColor.White))
  63. {
  64. using (var wValue = new ConsoleWriter(ConsoleColor.Yellow))
  65. {
  66. var maxKeyLength = props.Keys.Max(x => x.Length);
  67. foreach (var prop in props)
  68. {
  69. wName.Write(prop.Key);
  70. wName.Write(new string(' ', maxKeyLength - prop.Key.Length) + " :\t");
  71. wValue.WriteLine(prop.Value?.ToString());
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }