using System; using System.Collections.Generic; using System.Linq; using Quadarax.Foundation.Core.QConsole; using Quadarax.Foundation.Core.QConsole.Argument; using Quadarax.Foundation.Core.QConsole.Value; namespace BO.Console.Commands.Base { internal abstract class AbstractWorkspaceCommand : BaseCommand { #region *** Constants *** protected const string CS_ARG_NAME_APIK = "akey"; private const string CS_ARG_HINT_APIK = ""; private const string CS_ARG_DESC_APIK = "Workspace related API-KEY, if not defined API-KEY is used from config."; protected const string CS_ARG_NAME_APIKP = "akeyp"; private const string CS_ARG_HINT_APIKP = ""; private const string CS_ARG_DESC_APIKP = "Workspace related API-KEY password, if not defined API-KEY password is used from config."; #endregion #region *** Properties *** protected string ApiKey { get; private set; } protected string ApiKeyPassword { get; private set; } #endregion #region *** Constructors *** protected AbstractWorkspaceCommand(Engine engine) : base(engine) { } #endregion #region *** Protected Overrides *** protected override void BeginExecute() { base.BeginExecute(); ApiKey = Configuration.Defaults.Workspace.ApiKey; ApiKeyPassword = Configuration.Defaults.Workspace.ApiKeyPassword; var apiKey = GetArgumentValueOrDefault(CS_ARG_NAME_APIK); var apiKeyPassword = GetArgumentValueOrDefault(CS_ARG_NAME_APIKP); if (!string.IsNullOrEmpty(apiKey)) ApiKey = apiKey; if (!string.IsNullOrEmpty(apiKeyPassword)) ApiKeyPassword = apiKeyPassword; if (string.IsNullOrEmpty(ApiKey) || string.IsNullOrEmpty(ApiKeyPassword)) throw new ArgumentException( $"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'."); WriteDebugInfo($"ApiKey and password was set as '{ApiKey}'"); WriteInfo($"Using API-KEY: {ApiKey}"); WriteDebugInfo($"Using API-KEY-PASSWORD: {ApiKeyPassword}"); } protected override IEnumerable OnSetupArguments() { var args = base.OnSetupArguments().ToList(); args.Add(new NamedArgument(CS_ARG_NAME_APIK, CS_ARG_DESC_APIK,CS_ARG_HINT_APIK, TypeValuesEnum.String, string.Empty, false)); args.Add(new NamedArgument(CS_ARG_NAME_APIKP, CS_ARG_DESC_APIKP,CS_ARG_HINT_APIKP, TypeValuesEnum.String, string.Empty, false)); return args; } #endregion #region *** Protected Operations *** protected void SetApiKey(string apiKey, string apiPassword) { if (string.IsNullOrEmpty(apiKey)) throw new ArgumentNullException(nameof(apiKey)); if (string.IsNullOrEmpty(apiPassword)) throw new ArgumentNullException(nameof(apiPassword)); ApiKey = apiKey; ApiKeyPassword = apiPassword; WriteDebugInfo($"ApiKey and password was set as '{ApiKey}'"); } #endregion } }