Exemple #1
0
        private void Start(ICommandRuntime commandRuntime)
        {
            _log.Trace("Creating the Autofac container");
            _container = BuildContainer();
            RegisterAdditionalModules();
            ICommandLocator commandLocator = _container.Resolve <ICommandLocator>();
            string          commandName    = ExtractCommandName(ref _commandLineArguments);

            _log.TraceFormat("Finding the command: {0}", commandName);
            Lazy <ICommand, CommandMetadata> lazy = commandLocator.Find(commandName);

            if (lazy == null)
            {
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    lazy = commandLocator.Find("run");
                }
                else
                {
                    lazy = commandLocator.Find("help");
                    Environment.ExitCode = -1;
                }
            }
            _commandInstance = lazy.Value;
            _log.TraceFormat("Executing command: {0}", _commandInstance.GetType().Name);
            _commandInstance.Start(_commandLineArguments, commandRuntime, CommonOptions);
        }
Exemple #2
0
        public void Process(string[] args)
        {
            var first = GetFirstArgument(args);

            var command =
                commandLocator.Find(first) ??
                commandLocator.Find("help");

            if (command == null)
            {
                throw new InvalidOperationException(string.Format("The command '{0}' is not supported and no help exists.", first));
            }

            log.Info("Octopus Command Line Tool, version " + GetType().Assembly.GetFileVersion());
            log.Info(string.Empty);

            args = args.Skip(1).ToArray();

            try
            {
                var options = command.Options;
                options.Parse(args);

                command.Execute();
            }
            catch (Exception ex)
            {
                PrintError(ex);
                throw;
            }
        }
Exemple #3
0
        public void Process(string[] args)
        {
            var first = GetFirstArgument(args);

            var command =
                commandLocator.Find(first) ??
                commandLocator.Find("help");

            if (command == null)
            {
                throw new InvalidOperationException(string.Format("The command '{0}' is not supported and no help exists.", first));
            }

            log.Info("Octopus Command Line Tool, version " + GetType().Assembly.GetFileVersion());
            log.Info(string.Empty);

            args = args.Skip(1).ToArray();

            try
            {
                var options   = command.Options;
                var leftovers = options.Parse(args);
                if (leftovers.Any())
                {
                    throw new CommandException(string.Format("Extra non-recognized parameters for command: {0}", string.Join(" ", leftovers)));
                }

                command.Execute();
            }
            catch (Exception ex)
            {
                PrintError(ex);
                throw new ApplicationException("Handled error", ex);
            }
        }
 protected override void Load(ContainerBuilder builder)
 {
     RegisterNormalCommand(builder);
     // Only in the event that the primary command was the help command do
     // we go ahead and register the Command object that the help command
     // will display the details of.
     if (CommandLocator.Find(commandName, ThisAssembly) == typeof(HelpCommand))
     {
         RegisterHelpCommand(builder);
     }
     RegisterCommandAttributes(builder);
 }
Exemple #5
0
        static ICommand GetCommand(string first, ICommandLocator commandLocator)
        {
            if (string.IsNullOrWhiteSpace(first))
            {
                return commandLocator.Find("help");
            }

            var command = commandLocator.Find(first);
            if (command == null)
                throw new CommandException("Error: Unrecognized command '" + first + "'");

            return command;
        }
Exemple #6
0
        static ICommand GetCommand(string first, ICommandLocator commandLocator)
        {
            if (string.IsNullOrWhiteSpace(first))
            {
                return commandLocator.Find("help");
            }

            var command = commandLocator.Find(first);
            if (command == null)
                throw new CommandException("Error: Unrecognized command '" + first + "'");

            return command;
        }
Exemple #7
0
        public int Execute(string[] commandLineArguments)
        {
            var executable = Path.GetFileNameWithoutExtension(typeof(HelpCommand).Assembly.Location);

            var commandName = commandLineArguments.FirstOrDefault();

            if (string.IsNullOrEmpty(commandName))
            {
                PrintGeneralHelp(executable);
            }
            else
            {
                var command = commands.Find(commandName);

                if (command == null)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Command '{0}' is not supported", commandName);
                    Console.ResetColor();
                    PrintGeneralHelp(executable);
                }
                else
                {
                    PrintCommandHelp(executable, command, commandName);
                }
            }

            return(helpWasAskedFor ? 0 : 1);
        }
        public void Execute(params string[] commandLineArguments)
        {
            var executable = Path.GetFileNameWithoutExtension(typeof(HelpCommand).Assembly.FullLocalPath());

            var commandName = commandLineArguments.FirstOrDefault();

            if (string.IsNullOrEmpty(commandName))
            {
                PrintGeneralHelp(executable);
            }
            else
            {
                var command = commands.Find(commandName);

                if (command == null)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Command '{0}' does not exist", commandName);
                    Console.ResetColor();
                    PrintGeneralHelp(executable);
                }
                else
                {
                    PrintCommandHelp(executable, command, commandName);
                }
            }
        }
        public void Execute()
        {
            var executable = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().FullLocalPath());

            if (string.IsNullOrEmpty(commandName))
            {
                PrintGeneralHelp(executable);
            }
            else
            {
                var command = commands.Find(commandName);

                if (command == null)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(string.Format("Command '{0}' is not supported", commandName));
                    Console.ResetColor();
                    PrintGeneralHelp(executable);
                }
                else
                {
                    PrintCommandHelp(executable, command);
                }
            }
        }
Exemple #10
0
        public void Start(string[] commandLineArguments, ICommandRuntime commandRuntime, OptionSet commonOptions)
        {
            string withoutExtension = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().FullLocalPath());
            string name             = commandLineArguments.Length > 0 ? commandLineArguments[0] : null;

            if (string.IsNullOrEmpty(name))
            {
                PrintGeneralHelp(withoutExtension);
            }
            else
            {
                Lazy <ICommand, CommandMetadata> lazy = _commands.Find(name);
                if (lazy == null)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Command '{0}' is not supported", name);
                    Console.ResetColor();
                    PrintGeneralHelp(withoutExtension);
                }
                else
                {
                    PrintCommandHelp(withoutExtension, lazy.Value, lazy.Metadata, commonOptions);
                }
            }
        }
Exemple #11
0
        public Task Execute(string[] commandLineArguments)
        {
            return(Task.Run(() =>
            {
                var executable = Path.GetFileNameWithoutExtension(typeof(HelpCommand).GetTypeInfo().Assembly.FullLocalPath());

                var commandName = commandLineArguments.FirstOrDefault();

                if (string.IsNullOrEmpty(commandName))
                {
                    PrintGeneralHelp(executable);
                }
                else
                {
                    var command = commands.Find(commandName);

                    if (command == null)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Command '{0}' is not supported", commandName);
                        Console.ResetColor();
                        PrintGeneralHelp(executable);
                    }
                    else
                    {
                        PrintCommandHelp(executable, command, commandName);
                    }
                }
            }));
        }
Exemple #12
0
        /// <summary>
        /// Replicate the code that used to be run when executing a command
        /// </summary>
        private void DoOldCalamariExecutionPipeline()
        {
            // We manually replicate the type lookups that used to happen
            var runCommand = (ICommand)Activator.CreateInstance(
                CommandLocator.Find("run-script", typeof(RunScriptCommand).Assembly),
                new CalamariVariableDictionary(),
                new CombinedScriptEngine());
            var helpCommand = (ICommand)Activator.CreateInstance(
                CommandLocator.Find("help", typeof(HelpCommand).Assembly),
                CommandLocator.List(typeof(RunScriptCommand).Assembly),
                runCommand);

            new Calamari.Program(
                helpCommand,
                new HelpCommand(Enumerable.Empty <ICommandMetadata>()));
        }
Exemple #13
0
        public MemoryStream Execute(string[] commandLineArguments, MemoryStream ms = null)
        {
#if NET40
            var executable = Path.GetFileNameWithoutExtension(new Uri(typeof(HelpCommand).Assembly.CodeBase).LocalPath);
#else
            var executable = Path.GetFileNameWithoutExtension(new Uri(typeof(HelpCommand).GetTypeInfo().Assembly.CodeBase).LocalPath);
#endif

            var commandName = commandLineArguments.FirstOrDefault();

            if (string.IsNullOrEmpty(commandName))
            {
                PrintGeneralHelp(executable);
            }
            else
            {
                var commandMeta = commands.Find(commandName);
                if (commandMeta == null)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Command '{0}' is not supported", commandName);
                    Console.ResetColor();
                    PrintGeneralHelp(executable);
                }
                else
                {
                    var command = commands.Create(commandMeta);
                    PrintCommandHelp(executable, command, commandMeta, commandName);
                }
            }

            return(null);
        }
Exemple #14
0
        public void SetUp()
        {
            originalOutput = Console.Out;
            output         = new StringWriter();
            Console.SetOut(output);

            commandLocator        = Substitute.For <ICommandLocator>();
            logger                = new LoggerConfiguration().WriteTo.TextWriter(output).CreateLogger();
            commandOutputProvider = new CommandOutputProvider(logger);
            commandLocator.List().Returns(new ICommandMetadata[]
            {
                new CommandAttribute("test"),
                new CommandAttribute("help")
            });
            commandLocator.Find("help").Returns(new HelpCommand(commandLocator, commandOutputProvider));
            commandLocator.Find("test").Returns(new TestCommand(commandOutputProvider));
            completeCommand = new CompleteCommand(commandLocator, commandOutputProvider);
        }
Exemple #15
0
        IReadOnlyDictionary <string, string[]> GetCompletionMap()
        {
            var commandMetadatas = commands.List();

            return(commandMetadatas.ToDictionary(
                       c => c.Name,
                       c =>
            {
                var subCommand = (CommandBase)commands.Find(c.Name);
                return subCommand.GetOptionNames().ToArray();
            }));
        }
Exemple #16
0
    public void SetUp()
    {
        originalOutput = Console.Out;
        output         = new StringWriter();
        Console.SetOut(output);

        commandLocator        = Substitute.For <ICommandLocator>();
        logger                = new LoggerConfiguration().WriteTo.TextWriter(output).CreateLogger();
        commandOutputProvider = new CommandOutputProvider("TestApp", "1.0.0", new DefaultCommandOutputJsonSerializer(), logger);
        commandLocator.List()
        .Returns(new ICommandMetadata[]
        {
            new CommandAttribute("test"),
            new CommandAttribute("help")
        });
        var helpCommand = new HelpCommand(new Lazy <ICommandLocator>(() => commandLocator), commandOutputProvider);
        var testCommand = new TestCommand(commandOutputProvider);

        commandLocator.Find("help").Returns(helpCommand);
        commandLocator.Find("test").Returns(testCommand);
        completeCommand = new CompleteCommand(new Lazy <ICommandLocator>(() => commandLocator), commandOutputProvider);
    }
Exemple #17
0
        public override Task Execute(string[] commandLineArguments)
        {
            return(Task.Run(() =>
            {
                Options.Parse(commandLineArguments);

                commandOutputProvider.PrintMessages = OutputFormat == OutputFormat.Default;

                executable = AssemblyExtensions.GetExecutableName();

                var commandName = commandLineArguments.FirstOrDefault();

                if (string.IsNullOrEmpty(commandName))
                {
                    PrintGeneralHelp();
                }
                else
                {
                    var command = commands.Find(commandName);

                    if (command == null)
                    {
                        if (!commandName.StartsWith("--"))
                        {
                            // wasn't a parameter!
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("Command '{0}' is not supported", commandName);
                        }

                        Console.ResetColor();
                        PrintGeneralHelp();
                    }
                    else
                    {
                        PrintCommandHelp(command, commandLineArguments);
                    }
                }
            }));
        }