private HttpResponseMessage TryRunNewCommand(CommandInputModel command, DotNetNuke.Abstractions.Prompt.IConsoleCommand cmdTypeToRun, string[] args, bool isHelpCmd, DateTime startTime) { // Instantiate and run the command that uses the new interfaces and base class try { var cmdObj = (DotNetNuke.Abstractions.Prompt.IConsoleCommand)Activator.CreateInstance(cmdTypeToRun.GetType()); if (isHelpCmd) { return(this.Request.CreateResponse(HttpStatusCode.OK, DotNetNuke.Prompt.CommandRepository.Instance.GetCommandHelp(cmdObj))); } // set env. data for command use cmdObj.Initialize(args, this.PortalSettings, this.UserInfo, command.CurrentPage); return(this.AddLogAndReturnResponseNewCommands(cmdObj, command, startTime)); } catch (Exception ex) { Logger.Error(ex); return(this.AddLogAndReturnResponse(null, null, command, startTime, ex.Message)); } }
private HttpResponseMessage AddLogAndReturnResponseNewCommands(DotNetNuke.Abstractions.Prompt.IConsoleCommand consoleCommand, CommandInputModel command, DateTime startTime, string error = null) { HttpResponseMessage message; var isValid = consoleCommand?.IsValid() ?? false; var logInfo = new LogInfo { LogTypeKey = "PROMPT_ALERT", }; logInfo.LogProperties.Add(new LogDetailInfo("Command", FilterCommand(command.CmdLine))); logInfo.LogProperties.Add(new LogDetailInfo("IsValid", isValid.ToString())); try { logInfo.LogProperties.Add(new LogDetailInfo("TypeFullName", consoleCommand.GetType().FullName)); if (isValid) { var result = consoleCommand.Run(); if (result.PagingInfo != null) { if (result.PagingInfo.PageNo < result.PagingInfo.TotalPages) { result.Output = string.Format(Localization.GetString("Prompt_PagingMessageWithLoad", Constants.LocalResourcesFile), result.PagingInfo.PageNo, result.PagingInfo.TotalPages); var args = command.Args; var indexOfPage = args.Any(x => x.ToLowerInvariant() == "--page") ? args.TakeWhile(arg => arg.ToLowerInvariant() != "--page").Count() : -1; if (indexOfPage > -1) { args[indexOfPage + 1] = (result.PagingInfo.PageNo + 1).ToString(); } var nextPageCommand = string.Join(" ", args); if (indexOfPage == -1) { nextPageCommand += " --page " + (result.PagingInfo.PageNo + 1); } result.NextPageCommand = nextPageCommand; } else if (result.Records > 0) { result.Output = string.Format(Localization.GetString("Prompt_PagingMessage", Constants.LocalResourcesFile), result.PagingInfo.PageNo, result.PagingInfo.TotalPages); } } message = this.Request.CreateResponse(HttpStatusCode.OK, result); logInfo.LogProperties.Add(new LogDetailInfo("RecordsAffected", result.Records.ToString())); logInfo.LogProperties.Add(new LogDetailInfo("Output", result.Output)); } else { logInfo.LogProperties.Add(new LogDetailInfo("Output", consoleCommand?.ValidationMessage ?? error)); message = this.BadRequestResponse(consoleCommand?.ValidationMessage ?? error); } } catch (Exception ex) { logInfo.Exception = new ExceptionInfo(ex); message = this.BadRequestResponse(ex.Message); } logInfo.LogProperties.Add(new LogDetailInfo("ExecutionTime(hh:mm:ss)", TimeSpan.FromMilliseconds(DateTime.Now.Subtract(startTime).TotalMilliseconds).ToString(@"hh\:mm\:ss\.ffffff"))); LogController.Instance.AddLog(logInfo); return(message); }