AbstractWorkspaceCommand.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Quadarax.Foundation.Core.QConsole;
  5. using Quadarax.Foundation.Core.QConsole.Argument;
  6. using Quadarax.Foundation.Core.QConsole.Value;
  7. namespace BO.Console.Commands.Base
  8. {
  9. internal abstract class AbstractWorkspaceCommand : BaseCommand
  10. {
  11. #region *** Constants ***
  12. protected const string CS_ARG_NAME_APIK = "akey";
  13. private const string CS_ARG_HINT_APIK = "<api_key>";
  14. private const string CS_ARG_DESC_APIK = "Workspace related API-KEY, if not defined API-KEY is used from config.";
  15. protected const string CS_ARG_NAME_APIKP = "akeyp";
  16. private const string CS_ARG_HINT_APIKP = "<api_key_password>";
  17. private const string CS_ARG_DESC_APIKP = "Workspace related API-KEY password, if not defined API-KEY password is used from config.";
  18. #endregion
  19. #region *** Properties ***
  20. protected string ApiKey { get; private set; }
  21. protected string ApiKeyPassword { get; private set; }
  22. #endregion
  23. #region *** Constructors ***
  24. protected AbstractWorkspaceCommand(Engine engine) : base(engine)
  25. {
  26. }
  27. #endregion
  28. #region *** Protected Overrides ***
  29. protected override void BeginExecute()
  30. {
  31. base.BeginExecute();
  32. ApiKey = Configuration.Defaults.Workspace.ApiKey;
  33. ApiKeyPassword = Configuration.Defaults.Workspace.ApiKeyPassword;
  34. var apiKey = GetArgumentValueOrDefault<string>(CS_ARG_NAME_APIK);
  35. var apiKeyPassword = GetArgumentValueOrDefault<string>(CS_ARG_NAME_APIKP);
  36. if (!string.IsNullOrEmpty(apiKey))
  37. ApiKey = apiKey;
  38. if (!string.IsNullOrEmpty(apiKeyPassword))
  39. ApiKeyPassword = apiKeyPassword;
  40. if (string.IsNullOrEmpty(ApiKey) || string.IsNullOrEmpty(ApiKeyPassword))
  41. throw new ArgumentException(
  42. $"ApiKey or ApiKeyPassword must be specified by argument {Engine.Configuration.CharacterNamedArgumentSeparator}{CS_ARG_NAME_APIK} or {Engine.Configuration.CharacterNamedArgumentSeparator}{CS_ARG_NAME_APIKP} or specified in configuration file {Constants.CS_CONFIGURATION_FILE} in location 'defaults/workspace/api-key' or 'defaults/workspace/api-key-password'.");
  43. WriteDebugInfo($"ApiKey and password was set as '{ApiKey}'");
  44. WriteInfo($"Using API-KEY: {ApiKey}");
  45. WriteDebugInfo($"Using API-KEY-PASSWORD: {ApiKeyPassword}");
  46. }
  47. protected override IEnumerable<AbstractArgument> OnSetupArguments()
  48. {
  49. var args = base.OnSetupArguments().ToList();
  50. args.Add(new NamedArgument(CS_ARG_NAME_APIK, CS_ARG_DESC_APIK,CS_ARG_HINT_APIK, TypeValuesEnum.String, string.Empty, false));
  51. args.Add(new NamedArgument(CS_ARG_NAME_APIKP, CS_ARG_DESC_APIKP,CS_ARG_HINT_APIKP, TypeValuesEnum.String, string.Empty, false));
  52. return args;
  53. }
  54. #endregion
  55. #region *** Protected Operations ***
  56. protected void SetApiKey(string apiKey, string apiPassword)
  57. {
  58. if (string.IsNullOrEmpty(apiKey))
  59. throw new ArgumentNullException(nameof(apiKey));
  60. if (string.IsNullOrEmpty(apiPassword))
  61. throw new ArgumentNullException(nameof(apiPassword));
  62. ApiKey = apiKey;
  63. ApiKeyPassword = apiPassword;
  64. WriteDebugInfo($"ApiKey and password was set as '{ApiKey}'");
  65. }
  66. #endregion
  67. }
  68. }