예제 #1
0
        /// <summary>
        /// When overridden in the derived class, performs initialization
        /// of command execution.
        /// Default implementation in the base class just returns.
        /// </summary>
        /// <exception cref="T:System.Management.Automation.PipelineStoppedException">The pipeline has already been terminated, or was terminated
        /// during the execution of this method.
        /// The Cmdlet should generally just allow PipelineStoppedException
        /// to percolate up to the caller of ProcessRecord etc.</exception>
        protected override void BeginProcessing()
        {
            AzureDevOpsAccount account =
                AzureDevOpsConfiguration.Config.Accounts.Accounts.FirstOrDefault(i => i.FriendlyName.Equals(this.GetPsBoundParameter <string>("AccountName"), StringComparison.OrdinalIgnoreCase));

            if ((account == null) | (account == default(AzureDevOpsAccount)))
            {
                this.WriteError(
                    new ErrorRecord(
                        new InvalidOperationException("The specified account was not found"),
                        "AzureDevOpsMgmt.Accounts.SetAccountContext.AccountNotFoundException",
                        ErrorCategory.InvalidArgument,
                        this.GetPsBoundParameter <string>("AccountName")));
            }
            else if (!account.AccountProjects.Contains(this.ProjectName, StringComparer.OrdinalIgnoreCase))
            {
                this.WriteError(
                    new ErrorRecord(
                        new InvalidOperationException("The specified project was not found"),
                        "AzureDevOpsMgmt.Accounts.SetAccountContext.ProjectNotFoundException",
                        ErrorCategory.InvalidArgument,
                        this.ProjectName));
            }
#pragma warning disable 618,612
            else if (account.TokenId == null)
#pragma warning restore 618,612
            {
                this.WriteError(
                    new NoPatTokenLinkedException(account.FriendlyName),
                    this.BuildStandardErrorId(DevOpsModelTarget.InternalOperation, "PatTokenNotLinked"),
                    ErrorCategory.InvalidOperation,
                    this);
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="T:AzureDevOpsMgmt.Models.CurrentConnection" /> class.
 /// </summary>
 /// <param name="account">The account.</param>
 /// <param name="token">The token.</param>
 /// <param name="projectName">Name of the project.</param>
 public CurrentConnection(AzureDevOpsAccount account, AzureDevOpsPatToken token, string projectName)
 {
     this.Account     = account;
     this.Token       = token;
     this.ProjectName = projectName;
 }
        public void OnImport()
        {
            var accountData   = this.LoadAccountData();
            var configuration = this.LoadUserConfiguration();

            AzureDevOpsConfiguration.Config.Accounts      = accountData;
            AzureDevOpsConfiguration.Config.Configuration = configuration;

            if ((configuration.DefaultAccount != null) & (configuration.DefaultProject != null))
            {
                AzureDevOpsAccount  defaultAccount  = null;
                AzureDevOpsPatToken defaultPatToken = null;

                try
                {
                    defaultAccount  = accountData.Accounts.First(a => a.FriendlyName == configuration.DefaultAccount);
                    defaultPatToken = accountData.PatTokens.First(a => defaultAccount.LinkedPatTokens.Contains(a.Id));
                }
                catch (InvalidOperationException ioe) when(ioe.Message == "Sequence contains no matching element")
                {
                    this.ResetUserDefaultSettings();

                    var warningParams = new Dictionary <string, string>()
                    {
                        {
                            "Message",
                            "Corruption encountered well loading user default settings!  Settings have been reset to default (empty) values and will need to be configured again."
                        }
                    };

                    this.InvokePsCommand("Write-Warning", warningParams);
                }

                if (defaultAccount != null && defaultPatToken != null)
                {
                    AzureDevOpsConfiguration.Config.CurrentConnection = new CurrentConnection(defaultAccount, defaultPatToken, configuration.DefaultProject);

                    var outputParams = new Dictionary <string, string>()
                    {
                        {
                            "Message",
                            $"Default Account Settings Loaded Successfully.\r\nAccount Name: {configuration.DefaultAccount}\r\nProject Name: {configuration.DefaultProject}"
                        }
                    };
                    this.InvokePsCommand("Write-Host", outputParams);
                }
            }

            if (!AzureDevOpsConfiguration.Config.Accounts.HasCompleted1905Upgrade)
            {
                foreach (var token in AzureDevOpsConfiguration.Config.Accounts.PatTokens)
                {
                    ModuleStartup.ProcessCredentials(token);
                }

                AzureDevOpsConfiguration.Config.Accounts.HasCompleted1905Upgrade = true;
                FileHelpers.WriteFileJson(FileNames.AccountData, AzureDevOpsConfiguration.Config.Accounts);
            }

            var setVariableParams = new Dictionary <string, object>()
            {
                { "Name", "AzureDevOpsConfiguration" },
                { "Value", AzureDevOpsConfiguration.Config }
            };

            this.InvokePsCommand("Set-Variable", setVariableParams);
        }