BaseCommand.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. private 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. private 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. private 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. private 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; }
  41. protected BaseCommand(Engine engine) : base(engine)
  42. {
  43. FileSystem = new FileSystem();
  44. }
  45. protected override void OnInitialize()
  46. {
  47. base.OnInitialize();
  48. Configuration = JsonSerializer.Deserialize<Configuration>(File.ReadAllText(Constants.CS_CONFIGURATION_FILE));
  49. Client = new Connection(Configuration.ApiBaseUrl, Configuration.ApiTimeout, this, GetArgumentValueOrDefault<bool>(CS_ARG_NAME_DUMP));
  50. Client.Open(Configuration.ApiUser, Configuration.ApiUserPwd, Configuration.ApiMagic);
  51. }
  52. protected override void OnValidateArguments()
  53. {
  54. base.OnValidateArguments();
  55. if (ContainsArgument(CS_ARG_NAME_USER))
  56. UserName = GetArgumentValueOrDefault<string>(CS_ARG_NAME_USER);
  57. if (ContainsArgument(CS_ARG_NAME_FORCE))
  58. IsForce = WasArgumentSpecified(CS_ARG_NAME_FORCE);
  59. if (WasArgumentSpecified(CS_ARG_NAME_DEBUG))
  60. DebugConsoleWriter.IsWriteDebugEnabled = true;
  61. }
  62. protected override IEnumerable<AbstractArgument> OnSetupArguments()
  63. {
  64. var args = base.OnSetupArguments().ToList();
  65. args.Add(new FlagArgument(CS_ARG_NAME_DUMP, CS_ARG_DESC_DUMP, CS_ARG_HINT_DUMP, false));
  66. args.Add(new FlagArgument(CS_ARG_NAME_DEBUG, CS_ARG_DESC_DEBUG, CS_ARG_HINT_DEBUG, false));
  67. return args;
  68. }
  69. protected void AppendUserArgument(IList<AbstractArgument> args, string alternativeUserDescription = null)
  70. {
  71. 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));
  72. }
  73. protected void AppendForceArgument(IList<AbstractArgument> args, string alternativeUserDescription = null)
  74. {
  75. args.Add(new FlagArgument(CS_ARG_NAME_FORCE, string.IsNullOrEmpty(alternativeUserDescription) ? CS_ARG_DESC_FORCE : alternativeUserDescription,CS_ARG_HINT_FORCE, false));
  76. }
  77. public void Log(LogSeverityEnum type, string message, Exception e = null)
  78. {
  79. WriteInfo($"{type}: {message}" + (e == null ? "" : e.Message));
  80. }
  81. protected void WriteDtoToConsole(IDto dto)
  82. {
  83. var props = dto.GetAllPropertyValues();
  84. using (var wName = new ConsoleWriter(ConsoleColor.White))
  85. {
  86. using (var wValue = new ConsoleWriter(ConsoleColor.Yellow))
  87. {
  88. var maxKeyLength = props.Keys.Max(x=>x.Length);
  89. foreach (var prop in props)
  90. {
  91. wName.Write(prop.Key);
  92. wName.Write(new string(' ',maxKeyLength - prop.Key.Length) + " :\t");
  93. wValue.WriteLine(prop.Value?.ToString());
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }