Exemple #1
0
        private void OnAddRepositoryClick(object sender, RoutedEventArgs e)
        {
            string address = RepositoryAddress.Text;

            if (string.IsNullOrWhiteSpace(address))
            {
                return;
            }

            if (_repositorySettings.AddRepository(address))
            {
                RepositoryAddress.Text = string.Empty;
            }
        }
        private PSRepositoryInfo RepoValidationHelper(Hashtable repo)
        {
            if (!repo.ContainsKey("Name") || repo["Name"] == null || String.IsNullOrWhiteSpace(repo["Name"].ToString()))
            {
                WriteError(new ErrorRecord(
                               new PSInvalidOperationException("Repository name cannot be null"),
                               "NullNameForRepositoriesParameterSetRegistration",
                               ErrorCategory.InvalidArgument,
                               this));
                return(null);
            }

            if (repo["Name"].ToString().Equals("PSGallery"))
            {
                WriteError(new ErrorRecord(
                               new PSInvalidOperationException("Cannot register PSGallery with -Name parameter. Try: Register-PSResourceRepository -PSGallery"),
                               "PSGalleryProvidedAsNameRepoPSet",
                               ErrorCategory.InvalidArgument,
                               this));
                return(null);
            }

            if (!repo.ContainsKey("Uri") || repo["Uri"] == null || String.IsNullOrEmpty(repo["Uri"].ToString()))
            {
                WriteError(new ErrorRecord(
                               new PSInvalidOperationException("Repository Uri cannot be null"),
                               "NullUriForRepositoriesParameterSetRegistration",
                               ErrorCategory.InvalidArgument,
                               this));
                return(null);
            }

            if (!Utils.TryCreateValidUri(uriString: repo["Uri"].ToString(),
                                         cmdletPassedIn: this,
                                         uriResult: out Uri repoUri,
                                         errorRecord: out ErrorRecord errorRecord))
            {
                WriteError(errorRecord);
                return(null);
            }

            PSCredentialInfo repoCredentialInfo = null;

            if (repo.ContainsKey("CredentialInfo") &&
                !Utils.TryCreateValidPSCredentialInfo(credentialInfoCandidate: (PSObject)repo["CredentialInfo"],
                                                      cmdletPassedIn: this,
                                                      repoCredentialInfo: out repoCredentialInfo,
                                                      errorRecord: out ErrorRecord errorRecord1))
            {
                WriteError(errorRecord1);
                return(null);
            }

            try
            {
                WriteVerbose(String.Format("(RepositoriesParameterSet): on repo: {0}. Registers Name based repository", repo["Name"]));
                var addedRepo = RepositorySettings.AddRepository(repo["Name"].ToString(),
                                                                 repoUri,
                                                                 repo.ContainsKey("Priority") ? Convert.ToInt32(repo["Priority"].ToString()) : DefaultPriority,
                                                                 repo.ContainsKey("Trusted") ? Convert.ToBoolean(repo["Trusted"].ToString()) : DefaultTrusted,
                                                                 repoCredentialInfo,
                                                                 Force,
                                                                 this,
                                                                 out string errorMsg);

                if (!string.IsNullOrEmpty(errorMsg))
                {
                    ThrowTerminatingError(new ErrorRecord(
                                              new PSInvalidOperationException(errorMsg),
                                              "RegisterRepositoryError",
                                              ErrorCategory.ResourceUnavailable,
                                              this));
                }

                return(addedRepo);
            }
            catch (Exception e)
            {
                if (!(e is ArgumentException || e is PSInvalidOperationException))
                {
                    ThrowTerminatingError(new ErrorRecord(
                                              new PSInvalidOperationException(e.Message),
                                              "TerminatingErrorParsingAddingIndividualRepo",
                                              ErrorCategory.InvalidArgument,
                                              this));
                }

                WriteError(new ErrorRecord(
                               new PSInvalidOperationException(e.Message),
                               "ErrorParsingIndividualRepo",
                               ErrorCategory.InvalidArgument,
                               this));
                return(null);
            }
        }
        protected override void ProcessRecord()
        {
            List <PSRepositoryInfo> items = new List <PSRepositoryInfo>();

            switch (ParameterSetName)
            {
            case NameParameterSet:
                if (!Utils.TryCreateValidUri(uriString: Uri,
                                             cmdletPassedIn: this,
                                             uriResult: out _uri,
                                             errorRecord: out ErrorRecord errorRecord))
                {
                    ThrowTerminatingError(errorRecord);
                }

                try
                {
                    items.Add(RepositorySettings.AddRepository(Name, _uri, Priority, Trusted, CredentialInfo, Force, this, out string errorMsg));

                    if (!string.IsNullOrEmpty(errorMsg))
                    {
                        ThrowTerminatingError(new ErrorRecord(
                                                  new PSInvalidOperationException(errorMsg),
                                                  "ErrorInNameParameterSet",
                                                  ErrorCategory.InvalidArgument,
                                                  this));
                    }
                }
                catch (Exception e)
                {
                    ThrowTerminatingError(new ErrorRecord(
                                              new PSInvalidOperationException(e.Message),
                                              "ErrorInNameParameterSet",
                                              ErrorCategory.InvalidArgument,
                                              this));
                }
                break;

            case PSGalleryParameterSet:
                try
                {
                    items.Add(PSGalleryParameterSetHelper(Priority, Trusted));
                }
                catch (Exception e)
                {
                    ThrowTerminatingError(new ErrorRecord(
                                              new PSInvalidOperationException(e.Message),
                                              "ErrorInPSGalleryParameterSet",
                                              ErrorCategory.InvalidArgument,
                                              this));
                }
                break;

            case RepositoriesParameterSet:
                try
                {
                    items = RepositoriesParameterSetHelper();
                }
                catch (Exception e)
                {
                    ThrowTerminatingError(new ErrorRecord(
                                              new PSInvalidOperationException(e.Message),
                                              "ErrorInRepositoriesParameterSet",
                                              ErrorCategory.InvalidArgument,
                                              this));
                }
                break;

            default:
                Dbg.Assert(false, "Invalid parameter set");
                break;
            }

            if (PassThru)
            {
                foreach (PSRepositoryInfo repo in items)
                {
                    WriteObject(repo);
                }
            }
        }