BaseCommand.cs 4.2 KB

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