BaseLocalCommand.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.Json;
  10. using Quadarax.Foundation.Core.Logging;
  11. using Quadarax.Foundation.Core.Object.Extensions;
  12. using Quadarax.Foundation.Core.QConsole;
  13. using Quadarax.Foundation.Core.QConsole.Argument;
  14. using Quadarax.Foundation.Core.QConsole.Command.Base;
  15. namespace BO.ProcessServer.Commands.Base
  16. {
  17. internal abstract class BaseLocalCommand : AbstractCommand, ILogHandler
  18. {
  19. #region *** Constants ***
  20. protected const string CS_ARG_NAME_DUMP = "dump";
  21. private const string CS_ARG_HINT_DUMP = "<is_dump_reponses>";
  22. private const string CS_ARG_DESC_DUMP = "Flag if set dumps incomming json messages.";
  23. protected const string CS_ARG_NAME_DEBUG = "dbg";
  24. private const string CS_ARG_HINT_DEBUG = "<is_debug_mode>";
  25. private const string CS_ARG_DESC_DEBUG = "Flag if set writes debug messages.";
  26. #endregion
  27. protected Configuration Configuration { get; private set; }
  28. protected bool? IsForce { get; private set; }
  29. protected bool IsDebug { get; private set; }
  30. protected IFileSystem FileSystem { get; private set; }
  31. protected BaseLocalCommand(Engine engine) : base(engine)
  32. {
  33. }
  34. protected override void OnInitialize()
  35. {
  36. base.OnInitialize();
  37. var binder = new Binder(FileSystem);
  38. Configuration = binder.Load<Configuration>(Constants.CS_CONFIGURATION_FILE);
  39. }
  40. protected override void OnValidateArguments()
  41. {
  42. base.OnValidateArguments();
  43. IsDebug = WasArgumentSpecified(CS_ARG_NAME_DEBUG);
  44. if (IsDebug)
  45. DebugConsoleWriter.IsWriteDebugEnabled = true;
  46. }
  47. protected override IEnumerable<AbstractArgument> OnSetupArguments()
  48. {
  49. FileSystem = new FileSystem();
  50. var args = base.OnSetupArguments().ToList();
  51. args.Add(new FlagArgument(CS_ARG_NAME_DUMP, CS_ARG_DESC_DUMP, CS_ARG_HINT_DUMP, false));
  52. args.Add(new FlagArgument(CS_ARG_NAME_DEBUG, CS_ARG_DESC_DEBUG, CS_ARG_HINT_DEBUG, false));
  53. return args;
  54. }
  55. public void Log(LogSeverityEnum type, string message, Exception e = null)
  56. {
  57. if ((type == LogSeverityEnum.Trace || type == LogSeverityEnum.Debug) && !IsDebug)
  58. return;
  59. WriteInfo($"{type}: {message}" + (e == null ? "" : e.Message));
  60. }
  61. protected void WriteDtoToConsole(IDto dto)
  62. {
  63. var props = dto.GetAllPropertyValues();
  64. using (var wName = new ConsoleWriter(ConsoleColor.White))
  65. {
  66. using (var wValue = new ConsoleWriter(ConsoleColor.Yellow))
  67. {
  68. var maxKeyLength = props.Keys.Max(x => x.Length);
  69. foreach (var prop in props)
  70. {
  71. wName.Write(prop.Key);
  72. wName.Write(new string(' ', maxKeyLength - prop.Key.Length) + " :\t");
  73. wValue.WriteLine(prop.Value?.ToString());
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }