CommandRunResult RunGeneric(Action <TextWriter, TextWriter> action) { var errorWriter = this.output.GetErrorWriter(); var infoWriter = this.output.GetInfoWriter(); try { action(errorWriter, infoWriter); return(CommandRunResult.Ok()); } catch (Exception ex) { infoWriter.WriteLine("Exception occurred:"); infoWriter.WriteLine(ex); return(CommandRunResult.Fail("")); } }
public CommandRunResult Run(string commandName) { var command = this.commandsLookups[commandName].FirstOrDefault(); if (command == null) { return(CommandRunResult.Fail( $"Command {commandName} not found. Available commands: [{string.Join(", ", this.commands.Select(x => x.Definition().Name))}]")); } var def = command.Definition(); var parameters = def.Parameters; var taskSpecificVars = vars.WithPrefix($"Commands:{def.Name}:"); var missedRequiredParameters = parameters.Where(x => x.Required && taskSpecificVars[x.Name] == null); if (missedRequiredParameters.Any()) { return(Failed((error, info) => { error.WriteLine($"Missing required parameters {string.Join(", ", missedRequiredParameters.Select(x => "<" + x.Name + ">"))}"); new CommandHelpPrinter(info).Print(command); })); } var varsWithDefaultParameters = new ConfigVariablesBuilder() .Add(new CommandDefinitionConfigVars(def)) .Add(taskSpecificVars) .Build(); return(RunGeneric((error, info) => { command.Run(new CommandRunContext(varsWithDefaultParameters, info, error)); })); }
CommandRunResult Failed(Action <TextWriter, TextWriter> action) { action(output.GetErrorWriter(), output.GetInfoWriter()); return(CommandRunResult.Fail("")); }