示例#1
0
        private async Task <ExitCode> ExecuteAsync(string file,
                                                   SemanticVersion?version,
                                                   CancellationToken cancellationToken = default)
        {
            if (string.IsNullOrWhiteSpace(file))
            {
                throw new ArgumentNullException(nameof(file));
            }

            if (!File.Exists(file))
            {
                Logger.Error("The deployment manifest file '{File}' does not exist", file);
                return(ExitCode.Failure);
            }

            string data = DeploymentExecutionDefinitionFileReader.ReadAllData(file);

            var deploymentExecutionDefinitions =
                DeploymentExecutionDefinitionParser.Deserialize(data);

            if (deploymentExecutionDefinitions.Length == 0)
            {
                Logger.Error("Could not find any deployment definitions in file '{File}'", file);
                return(ExitCode.Failure);
            }

            if (deploymentExecutionDefinitions.Length == 1)
            {
                Logger.Information("Found 1 deployment definition");
            }
            else
            {
                Logger.Information("Found {Length} deployment definitions", deploymentExecutionDefinitions.Length);
            }

            Logger.Verbose("{Definitions}",
                           string.Join(", ", deploymentExecutionDefinitions.Select(definition => $"{definition}")));

            if (deploymentExecutionDefinitions.Length == 1 &&
                deploymentExecutionDefinitions[0].SemanticVersion is null &&
                version is null &&
                _allowInteractive)
            {
                Logger.Debug("Found one definition without version and no version has been explicitly set");
                Console.WriteLine(
                    "Version is missing in manifest and no version has been set in command line args. Enter a semantic version, eg. 1.2.3");
                string?inputVersion = null;

                if (Environment.UserInteractive && !Debugger.IsAttached && !UnitTestDetector.HasUnitTestInAppDomain)
                {
                    inputVersion = Console.ReadLine();
                }

                if (string.IsNullOrWhiteSpace(inputVersion))
                {
                    throw new InvalidOperationException("Missing version");
                }

                if (SemanticVersion.TryParse(inputVersion, out SemanticVersion semanticInputVersion))
                {
                    version = semanticInputVersion;
                    Logger.Debug("Using interactive version from user: {Version}",
                                 semanticInputVersion.ToNormalizedString());
                }
            }

            var exitCode = await _deploymentService
                           .DeployAsync(deploymentExecutionDefinitions, version, cancellationToken).ConfigureAwait(false);

            if (exitCode.IsSuccess)
            {
                Logger.Information(
                    "Successfully deployed deployment execution definition {DeploymentExecutionDefinition}",
                    deploymentExecutionDefinitions);
            }
            else
            {
                Logger.Error("Failed to deploy definition {DeploymentExecutionDefinition}",
                             deploymentExecutionDefinitions);
            }

            return(exitCode);
        }