using System; using System.Collections.Generic; using System.IO; using System.IO.Abstractions; using System.Linq; using System.Text.Json; using BO.Connector.Console; using BO.Console.Options; using Quadarax.Foundation.Core.Console; using Quadarax.Foundation.Core.Data.Interface.Entity; using Quadarax.Foundation.Core.Logging; using Quadarax.Foundation.Core.Object.Extensions; using Quadarax.Foundation.Core.QConsole; using Quadarax.Foundation.Core.QConsole.Argument; using Quadarax.Foundation.Core.QConsole.Command.Base; using Quadarax.Foundation.Core.QConsole.Value; // ReSharper disable InconsistentNaming namespace BO.Console.Commands.Base { internal abstract class BaseCommand : AbstractCommand, ILogHandler { #region *** Constants *** private const string CS_ARG_NAME_DUMP = "dump"; private const string CS_ARG_HINT_DUMP = ""; private const string CS_ARG_DESC_DUMP = "Flag if set dumps incomming json messages."; private const string CS_ARG_NAME_DEBUG = "dbg"; private const string CS_ARG_HINT_DEBUG = ""; private const string CS_ARG_DESC_DEBUG = "Flag if set writes debug messages."; private const string CS_ARG_NAME_USER = "user"; private const string CS_ARG_HINT_USER = ""; private const string CS_ARG_DESC_USER = "User name context. If not specified use default user context (who is logged in console)."; private const string CS_ARG_NAME_FORCE = "force"; private const string CS_ARG_HINT_FORCE = ""; private const string CS_ARG_DESC_FORCE = "Flag if set, then overrite existing entity, otherwise cancel operation."; #endregion protected Connection Client { get; private set; } protected Configuration Configuration { get; private set; } protected string UserName { get; private set; } protected bool? IsForce { get; private set; } protected IFileSystem FileSystem { get; } protected BaseCommand(Engine engine) : base(engine) { FileSystem = new FileSystem(); } protected override void OnInitialize() { base.OnInitialize(); Configuration = JsonSerializer.Deserialize(File.ReadAllText(Constants.CS_CONFIGURATION_FILE)); Client = new Connection(Configuration.ApiBaseUrl, Configuration.ApiTimeout, this, GetArgumentValueOrDefault(CS_ARG_NAME_DUMP)); Client.Open(Configuration.ApiUser, Configuration.ApiUserPwd, Configuration.ApiMagic); } protected override void OnValidateArguments() { base.OnValidateArguments(); if (ContainsArgument(CS_ARG_NAME_USER)) UserName = GetArgumentValueOrDefault(CS_ARG_NAME_USER); if (ContainsArgument(CS_ARG_NAME_FORCE)) IsForce = WasArgumentSpecified(CS_ARG_NAME_FORCE); if (WasArgumentSpecified(CS_ARG_NAME_DEBUG)) DebugConsoleWriter.IsWriteDebugEnabled = true; } protected override IEnumerable OnSetupArguments() { var args = base.OnSetupArguments().ToList(); args.Add(new FlagArgument(CS_ARG_NAME_DUMP, CS_ARG_DESC_DUMP, CS_ARG_HINT_DUMP, false)); args.Add(new FlagArgument(CS_ARG_NAME_DEBUG, CS_ARG_DESC_DEBUG, CS_ARG_HINT_DEBUG, false)); return args; } protected void AppendUserArgument(IList args, string alternativeUserDescription = null) { 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)); } protected void AppendForceArgument(IList args, string alternativeUserDescription = null) { args.Add(new FlagArgument(CS_ARG_NAME_FORCE, string.IsNullOrEmpty(alternativeUserDescription) ? CS_ARG_DESC_FORCE : alternativeUserDescription,CS_ARG_HINT_FORCE, false)); } public void Log(LogSeverityEnum type, string message, Exception e = null) { WriteInfo($"{type}: {message}" + (e == null ? "" : e.Message)); } protected void WriteDtoToConsole(IDto dto) { var props = dto.GetAllPropertyValues(); using (var wName = new ConsoleWriter(ConsoleColor.White)) { using (var wValue = new ConsoleWriter(ConsoleColor.Yellow)) { var maxKeyLength = props.Keys.Max(x=>x.Length); foreach (var prop in props) { wName.Write(prop.Key); wName.Write(new string(' ',maxKeyLength - prop.Key.Length) + " :\t"); wValue.WriteLine(prop.Value?.ToString()); } } } } } }