示例#1
0
        public CommandHelp GetCommandHelp(CommandInputModel command, IConsoleCommand consoleCommand, bool showSyntax = false, bool showLearn = false)
        {
            var cacheKey = (string.Join("_", command.Args) + "_" + PortalController.Instance.GetCurrentPortalSettings()?.DefaultLanguage).Replace("-", "_");

            cacheKey = $"{cacheKey}_{(showSyntax ? "1" : "0")}_{(showLearn ? "1" : "0")}}}";
            return(DataCache.GetCachedData <CommandHelp>(new CacheItemArgs(cacheKey, CacheItemPriority.Low),
                                                         c => this.GetCommandHelpInternal(consoleCommand, showSyntax, showLearn)));
        }
示例#2
0
        public HttpResponseMessage Cmd([FromBody] CommandInputModel command)
        {
            try
            {
                var args     = command.GetArgs();
                var cmdName  = args.First().ToUpper();
                var Commands = CommandRepository.Instance.GetCommands();
                if (!Commands.ContainsKey(cmdName) && cmdName.IndexOf('.') == -1)
                {
                    var seek = Commands.Values.FirstOrDefault(c => c.Name.ToUpper() == cmdName);
                    // if there is a command which matches then we assume the user meant that namespace
                    if (seek != null)
                    {
                        cmdName = string.Format("{0}.{1}", seek.NameSpace.ToUpper(), cmdName);
                    }
                }

                // if no command found notify
                if (!Commands.ContainsKey(cmdName))
                {
                    StringBuilder sbError    = new StringBuilder();
                    string        suggestion = GetSuggestedCommand(cmdName);
                    sbError.AppendFormat("Command '{0}' not found.", cmdName.ToLower());
                    if (!string.IsNullOrEmpty(suggestion))
                    {
                        sbError.AppendFormat(" Did you mean '{0}'?", suggestion);
                    }

                    return(BadRequestResponse(sbError.ToString()));
                }
                Type cmdTypeToRun = Commands[cmdName].CommandType;

                // Instantiate and run the command
                try
                {
                    var cmdObj = (IConsoleCommand)Activator.CreateInstance(cmdTypeToRun);
                    // set env. data for command use
                    cmdObj.Init(args, PortalSettings, UserInfo, command.currentPage);
                    if (cmdObj.IsValid())
                    {
                        return(Request.CreateResponse(HttpStatusCode.OK, cmdObj.Run()));
                    }
                    else
                    {
                        return(BadRequestResponse(cmdObj.ValidationMessage));
                    }
                }
                catch (Exception ex)
                {
                    DotNetNuke.Services.Exceptions.Exceptions.LogException(ex);
                    return(BadRequestResponse());
                }
            }
            catch (Exception ex)
            {
                return(BadRequestResponse());
            }
        }
示例#3
0
 public static void Create(string inputid, string command, string layout, string inputlocation, string notes)
 {
     var model = new CommandInputModel {
         _Id = inputid,
         Guid = inputid,
         Date = DateTime.Now,
         File = command.GetFirstString(),
         Arguments = command.GetAllStringsButFirst(),
         Layout = layout,
         InputLocation = inputlocation,
         Notes = notes
     };
     DeNSo.Session.New.Set(model);
 }
示例#4
0
        public static void Create(string inputid, string command, string layout, string inputlocation, string notes)
        {
            var model = new CommandInputModel {
                _Id           = inputid,
                Guid          = inputid,
                Date          = DateTime.Now,
                File          = command.GetFirstString(),
                Arguments     = command.GetAllStringsButFirst(),
                Layout        = layout,
                InputLocation = inputlocation,
                Notes         = notes
            };

            DeNSo.Session.New.Set(model);
        }
示例#5
0
        public HttpResponseMessage Cmd(int portalId, [FromBody] CommandInputModel command)
        {
            var portal = PortalController.Instance.GetPortal(portalId);

            if (portal == null)
            {
                var errorMessage = string.Format(Localization.GetString("Prompt_GetPortal_NotFound", Constants.LocalResourcesFile), portalId);
                Logger.Error(errorMessage);
                return(this.AddLogAndReturnResponse(null, null, command, DateTime.Now, errorMessage));
            }

            this.PortalId = portalId;
            this.SetupPortalSettings(portalId);

            return(this.Cmd(command));
        }
示例#6
0
        public int Execute(string[] args)
        {
            var inputModel            = new CommandInputModel(args);
            var completedSuccessfully = _conExec.Execute(inputModel);

            _logger.Message("\n");

            var exitCode = completedSuccessfully ? 0 : -1;

            if (completedSuccessfully)
            {
                _logger.Success("Build Finished Successfully.");
            }
            else
            {
                _logger.Error("Build Failed.");
            }

            return(exitCode);
        }
 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 TryRunOldCommand(CommandInputModel command, Type cmdTypeToRun, string[] args, bool isHelpCmd, DateTime startTime)
 {
     // Instantiate and run the command
     try
     {
         var cmdObj = (IConsoleCommand)Activator.CreateInstance(cmdTypeToRun);
         if (isHelpCmd)
         {
             return(this.Request.CreateResponse(HttpStatusCode.OK, Components.Repositories.CommandRepository.Instance.GetCommandHelp(command.Args, cmdObj)));
         }
         // set env. data for command use
         cmdObj.Initialize(args, this.PortalSettings, this.UserInfo, command.CurrentPage);
         return(this.AddLogAndReturnResponse(cmdObj, cmdTypeToRun, command, startTime));
     }
     catch (Exception ex)
     {
         Logger.Error(ex);
         return(this.AddLogAndReturnResponse(null, null, command, startTime, ex.Message));
     }
 }
示例#9
0
        public HttpResponseMessage Cmd([FromBody] CommandInputModel command)
        {
            var startTime = DateTime.Now;

            try
            {
                var args         = command.Args;
                var isHelpCmd    = args.First().ToUpper() == "HELP";
                var isHelpLearn  = isHelpCmd && args.Length > 1 && args[1].ToUpper() == "LEARN";
                var isHelpSyntax = isHelpCmd && args.Length > 1 && args[1].ToUpper() == "SYNTAX";
                var cmdName      = isHelpCmd ? (args.Length > 1 ? args[1].ToUpper() : "") : args.First().ToUpper();
                if (isHelpCmd && (isHelpSyntax || isHelpLearn))
                {
                    return(this.GetHelp(command, null, isHelpSyntax, isHelpLearn));
                }
                if (isHelpCmd && args.Length == 1)
                {
                    return(this.AddLogAndReturnResponse(null, null, command, startTime,
                                                        string.Format(Localization.GetString("CommandNotFound", Constants.LocalResourcesFile),
                                                                      cmdName.ToLower())));
                }

                var allCommands = CommandRepository.Instance.GetCommands();
                // if no command found notify
                if (!allCommands.ContainsKey(cmdName))
                {
                    var sbError    = new StringBuilder();
                    var suggestion = Utilities.GetSuggestedCommand(cmdName);
                    sbError.AppendFormat(Localization.GetString("CommandNotFound", Constants.LocalResourcesFile), cmdName.ToLower());
                    if (!string.IsNullOrEmpty(suggestion))
                    {
                        sbError.AppendFormat(Localization.GetString("DidYouMean", Constants.LocalResourcesFile), suggestion);
                    }
                    return(this.AddLogAndReturnResponse(null, null, command, startTime, sbError.ToString()));
                }
                var cmdTypeToRun = allCommands[cmdName].CommandType;

                // Instantiate and run the command
                try
                {
                    var cmdObj = (IConsoleCommand)Activator.CreateInstance(cmdTypeToRun);
                    if (isHelpCmd)
                    {
                        return(this.GetHelp(command, cmdObj));
                    }
                    // set env. data for command use
                    cmdObj.Initialize(args, this.PortalSettings, this.UserInfo, command.CurrentPage);
                    return(this.AddLogAndReturnResponse(cmdObj, cmdTypeToRun, command, startTime));
                }
                catch (Exception ex)
                {
                    Logger.Error(ex);
                    return(this.AddLogAndReturnResponse(null, null, command, startTime, ex.Message));
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return(this.AddLogAndReturnResponse(null, null, command, startTime, ex.Message));
            }
        }
示例#10
0
 private HttpResponseMessage GetHelp(CommandInputModel command, IConsoleCommand consoleCommand, bool showSyntax = false, bool showLearn = false)
 {
     return(this.Request.CreateResponse(HttpStatusCode.OK, CommandRepository.Instance.GetCommandHelp(command, consoleCommand, showSyntax, showLearn)));
 }
示例#11
0
        /// <summary>
        /// Log every command run by a users.
        /// </summary>
        /// <param name="consoleCommand"></param>
        /// <param name="cmdTypeToRun"></param>
        /// <param name="command"></param>
        /// <param name="startTime"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        private HttpResponseMessage AddLogAndReturnResponse(IConsoleCommand consoleCommand, Type cmdTypeToRun, 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
            {
                if (cmdTypeToRun != null)
                {
                    logInfo.LogProperties.Add(new LogDetailInfo("TypeFullName", cmdTypeToRun.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);
        }
        public HttpResponseMessage Cmd([FromBody] CommandInputModel command)
        {
            var startTime = DateTime.Now;

            try
            {
                var args         = command.Args;
                var isHelpCmd    = args.First().ToUpper() == "HELP";
                var isHelpLearn  = isHelpCmd && args.Length > 1 && args[1].ToUpper() == "LEARN";
                var isHelpSyntax = isHelpCmd && args.Length > 1 && args[1].ToUpper() == "SYNTAX";
                var cmdName      = isHelpCmd ? (args.Length > 1 ? args[1].ToUpper() : "") : args.First().ToUpper();
                if (isHelpSyntax)
                {
                    return(this.Request.CreateResponse(HttpStatusCode.OK, new CommandHelp()
                    {
                        ResultHtml = Localization.GetString("Prompt_CommandHelpSyntax", Constants.LocalResourcesFile),
                    }));
                }
                else if (isHelpLearn)
                {
                    return(this.Request.CreateResponse(HttpStatusCode.OK, new CommandHelp()
                    {
                        ResultHtml = Localization.GetString("Prompt_CommandHelpLearn", Constants.LocalResourcesFile),
                    }));
                }
                else if (isHelpCmd && args.Length == 1)
                {
                    return(this.AddLogAndReturnResponse(null, null, command, startTime,
                                                        string.Format(Localization.GetString("CommandNotFound", Constants.LocalResourcesFile),
                                                                      cmdName.ToLower())));
                }

                // first look in new commands, then in the old commands
                var newCommand = DotNetNuke.Prompt.CommandRepository.Instance.GetCommand(cmdName);
                if (newCommand == null)
                {
                    var allCommands = Components.Repositories.CommandRepository.Instance.GetCommands();
                    // if no command found notify
                    if (!allCommands.ContainsKey(cmdName))
                    {
                        var sbError    = new StringBuilder();
                        var suggestion = Utilities.GetSuggestedCommand(cmdName);
                        sbError.AppendFormat(Localization.GetString("CommandNotFound", Constants.LocalResourcesFile), cmdName.ToLower());
                        if (!string.IsNullOrEmpty(suggestion))
                        {
                            sbError.AppendFormat(Localization.GetString("DidYouMean", Constants.LocalResourcesFile), suggestion);
                        }
                        return(this.AddLogAndReturnResponse(null, null, command, startTime, sbError.ToString()));
                    }
                    return(this.TryRunOldCommand(command, allCommands[cmdName].CommandType, args, isHelpCmd, startTime));
                }
                else
                {
                    return(this.TryRunNewCommand(command, newCommand, args, isHelpCmd, startTime));
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return(this.AddLogAndReturnResponse(null, null, command, startTime, ex.Message));
            }
        }