BaseLocalCommand.cs 4.1 KB

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