Beispiel #1
0
        private PSRepositoryInfo RepoValidationHelper(Hashtable repo)
        {
            WriteVerbose(String.Format("Parsing through repository: {0}", repo["Name"]));

            Uri repoUri = null;

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

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

            bool repoTrusted = false;

            isSet = false;
            if (repo.ContainsKey("Trusted"))
            {
                repoTrusted = (bool)repo["Trusted"];
                isSet       = true;
            }

            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
            {
                var updatedRepo = RepositorySettings.UpdateRepositoryStore(repo["Name"].ToString(),
                                                                           repoUri,
                                                                           repo.ContainsKey("Priority") ? Convert.ToInt32(repo["Priority"].ToString()) : DefaultPriority,
                                                                           repoTrusted,
                                                                           isSet,
                                                                           DefaultPriority,
                                                                           repoCredentialInfo,
                                                                           this,
                                                                           out string errorMsg);

                if (!string.IsNullOrEmpty(errorMsg))
                {
                    WriteError(new ErrorRecord(
                                   new PSInvalidOperationException(errorMsg),
                                   "ErrorSettingRepository",
                                   ErrorCategory.InvalidData,
                                   this));
                }

                return(updatedRepo);
            }
            catch (Exception e)
            {
                WriteError(new ErrorRecord(
                               new PSInvalidOperationException(e.Message),
                               "ErrorSettingIndividualRepoFromRepositories",
                               ErrorCategory.InvalidArgument,
                               this));
                return(null);
            }
        }
Beispiel #2
0
        protected override void ProcessRecord()
        {
            if (MyInvocation.BoundParameters.ContainsKey(nameof(Uri)))
            {
                bool isUriValid = Utils.TryCreateValidUri(Uri, this, out _uri, out ErrorRecord errorRecord);
                if (!isUriValid)
                {
                    ThrowTerminatingError(errorRecord);
                }
            }

            List <PSRepositoryInfo> items = new List <PSRepositoryInfo>();

            switch (ParameterSetName)
            {
            case NameParameterSet:
                try
                {
                    items.Add(RepositorySettings.UpdateRepositoryStore(Name,
                                                                       _uri,
                                                                       Priority,
                                                                       Trusted,
                                                                       isSet,
                                                                       DefaultPriority,
                                                                       CredentialInfo,
                                                                       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 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 item in items)
                {
                    WriteObject(item);
                }
            }
        }