Exemplo n.º 1
0
        public async Task <CommandReturnCodes> RunCommandAsync(TextReader input, TextWriter output, string tenant, string[] args, IDictionary <string, string> switches)
        {
            try
            {
                tenant = tenant ?? ShellHelper.DefaultShellName;

                using (var env = await CreateStandaloneEnvironmentAsync(tenant))
                {
                    var commandManager = env.ServiceProvider.GetService <ICommandManager>();

                    var parameters = new CommandParameters
                    {
                        Arguments = args,
                        Switches  = switches,
                        Input     = input,
                        Output    = output
                    };

                    await commandManager.ExecuteAsync(parameters);
                }

                return(CommandReturnCodes.Ok);
            }
            catch (Exception ex)
            {
                if (ex.IsFatal())
                {
                    throw;
                }
                if (ex is TargetInvocationException &&
                    ex.InnerException != null)
                {
                    // If this is an exception coming from reflection and there is an innerexception which is the actual one, redirect
                    ex = ex.InnerException;
                }
                await OutputExceptionAsync(output, T["Error executing command \"{0}\"", string.Join(" ", args)], ex);

                return(CommandReturnCodes.Fail);
            }
        }
Exemplo n.º 2
0
        public async Task ExecuteAsync(CommandParameters parameters)
        {
            var matches = MatchCommands(parameters) ?? Enumerable.Empty <Match>();

            if (matches.Count() == 1)
            {
                var match = matches.Single();
                await match.CommandHandler.ExecuteAsync(match.Context);
            }
            else
            {
                var commandMatch = string.Join(" ", parameters.Arguments.ToArray());
                var commandList  = string.Join(",", GetCommandDescriptors().SelectMany(d => d.Names).ToArray());
                if (matches.Any())
                {
                    throw new Exception(T["Multiple commands found matching arguments \"{0}\". Commands available: {1}.",
                                          commandMatch, commandList]);
                }
                throw new Exception(T["No command found matching arguments \"{0}\". Commands available: {1}.",
                                      commandMatch, commandList]);
            }
        }