Exemple #1
0
    private static int ImportCertificate(CommandOption import, CommandOption password, ConsoleReporter reporter)
    {
        var manager = CertificateManager.Instance;

        try
        {
            var result = manager.ImportCertificate(import.Value(), password.Value());
            switch (result)
            {
            case ImportCertificateResult.Succeeded:
                reporter.Output("The certificate was successfully imported.");
                break;

            case ImportCertificateResult.CertificateFileMissing:
                reporter.Error($"The certificate file '{import.Value()}' does not exist.");
                return(MissingCertificateFile);

            case ImportCertificateResult.InvalidCertificate:
                reporter.Error($"The provided certificate file '{import.Value()}' is not a valid PFX file or the password is incorrect.");
                return(FailedToLoadCertificate);

            case ImportCertificateResult.NoDevelopmentHttpsCertificate:
                reporter.Error($"The certificate at '{import.Value()}' is not a valid ASP.NET Core HTTPS development certificate.");
                return(NoDevelopmentHttpsCertificate);

            case ImportCertificateResult.ExistingCertificatesPresent:
                reporter.Error($"There are one or more ASP.NET Core HTTPS development certificates present in the environment. Remove them before importing the given certificate.");
                return(ExistingCertificatesPresent);

            case ImportCertificateResult.ErrorSavingTheCertificateIntoTheCurrentUserPersonalStore:
                reporter.Error("There was an error saving the HTTPS developer certificate to the current user personal certificate store.");
                return(ErrorSavingTheCertificate);

            default:
                break;
            }
        }
        catch (Exception exception)
        {
            reporter.Error($"An unexpected error occurred: {exception}");
            return(ErrorImportingCertificate);
        }

        return(Success);
    }
        public void WritesToStandardStreams()
        {
            var testConsole = new TestConsole();
            var reporter    = new ConsoleReporter(testConsole, verbose: true, quiet: false);

            // stdout
            reporter.Verbose("verbose");
            Assert.Equal("verbose" + EOL, testConsole.GetOutput());
            testConsole.Clear();

            reporter.Output("out");
            Assert.Equal("out" + EOL, testConsole.GetOutput());
            testConsole.Clear();

            reporter.Warn("warn");
            Assert.Equal("warn" + EOL, testConsole.GetOutput());
            testConsole.Clear();

            // stderr
            reporter.Error("error");
            Assert.Equal("error" + EOL, testConsole.GetError());
            testConsole.Clear();
        }
Exemple #3
0
        private async Task <int> OnExecuteAsync()
        {
            var reporter = new ConsoleReporter(PhysicalConsole.Singleton)
            {
                IsVerbose = Verbose
            };

            var installDir = string.IsNullOrEmpty(OutputDirectory)
                ? Path.Combine(Directory.GetCurrentDirectory(), "packages")
                : Path.GetFullPath(OutputDirectory);

            var       tempFilePath = Path.Combine(AppContext.BaseDirectory, "projectThatNeverExists.csproj");
            ISettings settings     = Settings.LoadDefaultSettings(tempFilePath);

            VersionRange versionRange;

            if (!string.IsNullOrEmpty(Version))
            {
                if (!VersionRange.TryParse(Version, out versionRange))
                {
                    reporter.Error($"Invalid nuget version '{Version}'");
                    return(1);
                }
            }
            else
            {
                versionRange = Prerelease
                    ? VersionRange.AllFloating
                    : VersionRange.AllStableFloating;
            }

            var logger = new ConsoleNuGetLogger(reporter);

            var results = await RestoreRunnerEx.RunWithoutCommit(tempFilePath,
                                                                 installDir,
                                                                 PackageId,
                                                                 versionRange,
                                                                 settings,
                                                                 Sources,
                                                                 logger);

            var success = false;

            foreach (var result in results)
            {
                if (result.Result.Success)
                {
                    var installedVersion = result.Result.LockFile.Libraries.FirstOrDefault(l => string.Equals(PackageId, l.Name, StringComparison.OrdinalIgnoreCase));
                    if (installedVersion != null)
                    {
                        var path = installedVersion.Path;
                        reporter.Output($"Installed {installedVersion.Name} {installedVersion.Version}");
                        foreach (var file in installedVersion.Files)
                        {
                            reporter.Verbose("Package file: " + file);
                        }
                        success = true;
                        break;
                    }
                }
                else
                {
                    foreach (var unresolved in result.Result.GetAllUnresolved())
                    {
                        reporter.Warn($"Could not find a package {unresolved.Name} in the version range {unresolved.VersionRange}");
                    }
                }
            }

            if (success)
            {
                reporter.Output("Installation succeeded");
                return(0);
            }

            reporter.Error("Installation failed");

            return(success ? 1 : 0);
        }