Example #1
0
        public static void Main(string[] args)
        {
            // Process the incoming arguments
            var commandParser = new CommandArgumentsParserService();
            Tuple <string, Dictionary <string, string> > commandArguments;

            try { commandArguments = commandParser.ParseCommandWithArguments(args); }
            catch (FormatException ex)
            {
                Log.Error(ex.Message);
                Environment.ExitCode = ExitCodeInvalidCommandFormat;
                return;
            }

            if (commandArguments == null)
            {
                Log.Info(@"No command specified. " + Environment.NewLine);
                PrintAvailableCommands();
                Environment.ExitCode = ExitCodeOk;
                return;
            }

            // Locate the specified Command
            var command = CommandInstanceFactory.CreateCommandInstance(commandArguments.Item1, commandArguments.Item2);

            if (command == null)
            {
                Log.Info(@"Command not found.");
                Environment.ExitCode = ExitCodeCommandNotFound;
                return;
            }

            // Execute the command only if it is ready
            if (command.Ready())
            {
                CommandExecutionResult result;
                try
                {
                    result = command.Execute();
                }
                catch (Exception ex)
                {
                    result = CommandExecutionResult.Exception(ex);
                }

                Log.Info(result.Message);

                Environment.ExitCode = result.Success
                    ? ExitCodeOk
                    : ExitCodeCommandExecutionFailed;
                return;
            }

            Log.Info(@"Command was not ready to execute.  Check your parameters and try again.");
            Log.Info(command.GetType().GetCustomAttribute <CommandAttribute>().GetCommandInfo());
            Environment.ExitCode = ExitCodeCommandNotReady;
        }
Example #2
0
        public override CommandExecutionResult Execute()
        {
            Log.Info("Processing batch file {0}:", Filename);

            // load the file
            var xDoc = XDocument.Load(Filename);

            // check for a commands node
            var rootNode = xDoc.Element("Commands");

            if (rootNode == null)
            {
                throw new Exception("Invalid command file.");
            }

            // Process the Command Elements in Document Order
            var commandInstances =
                from commandNode in rootNode.Elements("Command")
                let commandName = commandNode.Attribute("name").Value
                                  let commandParameters = GetCommandParameters(commandNode)
                                                          select CommandInstanceFactory.CreateCommandInstance(commandName, commandParameters);

            var commandsReady    = new List <Command>();
            var commandsNotReady = new List <Command>();

            foreach (var commandInstance in commandInstances)
            {
                if (commandInstance.Ready())
                {
                    commandsReady.Add(commandInstance);
                }
                else
                {
                    commandsNotReady.Add(commandInstance);
                }
            }

            // Print Not Ready Commands
            foreach (var commandNotReady in commandsNotReady)
            {
                Log.Info("Command '{0}' not ready.  Please check the arguments and try again.");
                Log.Info(commandNotReady.GetType().GetCustomAttribute <CommandAttribute>().GetCommandInfo());
            }

            // Execute Ready Commands
            var results = commandsReady
                          .Select(x => x.Execute());
            var failures = false;

            results.ForEach(result =>
            {
                if (result.Success)
                {
                    Log.Info(result.Message);
                }
                else
                {
                    Log.Error(result.Message);
                    failures = true;
                }
            });

            return(failures
                ? CommandExecutionResult.Ok("Batch Complete with Failures.")
                : CommandExecutionResult.Ok("Batch Complete."));
        }