BaseCommand.cs 5.1 KB

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