/// <summary>
        /// Executes a new command with the given arguments. Returns true if the command process exits with a 0 exit code, otherwise returns false.
        /// </summary>
        /// <param name="arguments"></param>
        /// <param name="workingDir"></param>
        /// <returns></returns>
        public async Task<bool> ExecuteWindowlessCommandAsync(string arguments, string workingDir, StdOutDelegate stdOutHandler)
        {
            //* Create your Process
            Process process = new Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.Arguments = "/c " + arguments;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.CreateNoWindow = true;
            if (workingDir != null) process.StartInfo.WorkingDirectory = workingDir;

            //* Set your output and error (asynchronous) handlers
            StdOutHandler = stdOutHandler;
            process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
            process.ErrorDataReceived += new DataReceivedEventHandler(ErrorHandler);

            //* Start process and handlers
            process.Start();
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();

            // Begin blocking call
            await Task.Run(() => process.WaitForExit());

            if (process.ExitCode == 0) return true;
            return false;
        }
        public async Task <DetailedResult> DeployAppAsync(CloudFoundryInstance targetCf, CloudFoundryOrganization targetOrg, CloudFoundrySpace targetSpace, string appName, string appProjPath, StdOutDelegate stdOutHandler)
        {
            if (!_fileLocatorService.DirContainsFiles(appProjPath))
            {
                return(new DetailedResult(false, emptyOutputDirMessage));
            }

            DetailedResult cfTargetResult = await _cfCliService.ExecuteCfCliCommandAsync(arguments : $"target -o {targetOrg.OrgName} -s {targetSpace.SpaceName}", stdOutHandler : stdOutHandler);

            if (!cfTargetResult.Succeeded)
            {
                return(new DetailedResult(false, $"Unable to target org '{targetOrg.OrgName}' or space '{targetSpace.SpaceName}'.\n{cfTargetResult.Explanation}"));
            }

            DetailedResult cfPushResult = await _cfCliService.ExecuteCfCliCommandAsync(arguments : "push " + appName, workingDir : appProjPath, stdOutHandler : stdOutHandler);

            if (!cfPushResult.Succeeded)
            {
                return(new DetailedResult(false, $"Successfully targeted org '{targetOrg.OrgName}' and space '{targetSpace.SpaceName}' but app deployment failed at the `cf push` stage.\n{cfPushResult.Explanation}"));
            }

            return(new DetailedResult(true, $"App successfully deploying to org '{targetOrg.OrgName}', space '{targetSpace.SpaceName}'..."));
        }
Пример #3
0
        /// <summary>
        /// Initiate a new Cloud Foundry CLI command with the given arguments.
        /// Invoke the command prompt and wait for the process to exit before returning.
        /// Return true if no StdError was captured over the course of the process, false otherwise.
        /// </summary>
        /// <param name="arguments">Parameters to include along with the `cf` command (e.g. "push", "apps")</param>
        /// <param name="workingDir"></param>
        /// <returns></returns>
        public async Task <DetailedResult> ExecuteCfCliCommandAsync(string arguments, StdOutDelegate stdOutHandler, string workingDir = null)
        {
            string pathToCfExe = _fileLocatorService.FullPathToCfExe;

            if (string.IsNullOrEmpty(pathToCfExe))
            {
                return(new DetailedResult(false, $"Unable to locate cf.exe."));
            }

            string commandStr = '"' + pathToCfExe + '"' + ' ' + arguments;
            bool   commandSucceededWithoutError = await _cmdProcessService.ExecuteWindowlessCommandAsync(commandStr, workingDir, stdOutHandler);

            if (commandSucceededWithoutError)
            {
                return(new DetailedResult(succeeded: true));
            }

            string reason = $"Unable to execute `cf {arguments}`.";

            return(new DetailedResult(false, reason));
        }