BaseCommand.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.Json;
  6. using BO.Connector.Console;
  7. using BO.Console.Options;
  8. using Quadarax.Foundation.Core.Console;
  9. using Quadarax.Foundation.Core.Data.Interface.Entity;
  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.Console.Commands.Base
  16. {
  17. internal abstract class BaseCommand : AbstractCommand, ILogHandler
  18. {
  19. #region *** Constants ***
  20. private 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. #endregion
  24. protected Connection Client { get; private set; }
  25. protected Configuration Configuration { get; private set; }
  26. protected BaseCommand(Engine engine) : base(engine)
  27. {
  28. }
  29. protected override void OnInitialize()
  30. {
  31. base.OnInitialize();
  32. Configuration = JsonSerializer.Deserialize<Configuration>(File.ReadAllText("console.json"));
  33. Client = new Connection(Configuration.ApiBaseUrl, Configuration.ApiTimeout, this, GetArgumentValueOrDefault<bool>(CS_ARG_NAME_DUMP));
  34. Client.Open(Configuration.ApiUser, Configuration.ApiUserPwd, Configuration.ApiMagic);
  35. }
  36. protected override IEnumerable<AbstractArgument> OnSetupArguments()
  37. {
  38. var args = base.OnSetupArguments().ToList();
  39. args.Add(new FlagArgument(CS_ARG_NAME_DUMP, CS_ARG_DESC_DUMP, CS_ARG_HINT_DUMP, false));
  40. return args;
  41. }
  42. public void Log(LogSeverityEnum type, string message, Exception e = null)
  43. {
  44. WriteInfo($"{type}: {message}" + (e == null ? "" : e.Message));
  45. }
  46. protected void WriteDtoToConsole(IDto dto)
  47. {
  48. var props = dto.GetAllPropertyValues();
  49. using(var wName = new ConsoleWriter(ConsoleColor.White))
  50. using(var wValue = new ConsoleWriter(ConsoleColor.Yellow))
  51. foreach (var prop in props)
  52. {
  53. wName.Write(prop.Key);
  54. wName.Write("\t:\t");
  55. wValue.WriteLine(prop.Value?.ToString());
  56. }
  57. }
  58. }
  59. }