NuGet.Commands.SetApiKeyCommand.ExecuteCommand C# (CSharp) Method

ExecuteCommand() public method

public ExecuteCommand ( ) : void
return void
        public override void ExecuteCommand()
        {
            //Frist argument should be the ApiKey
            string apiKey = Arguments[0];

            bool setSymbolServerKey = false;

            //If the user passed a source use it for the gallery location
            string source;
            if (String.IsNullOrEmpty(Source))
            {
                source = NuGetConstants.DefaultGalleryServerUrl;
                // If no source was specified, set the default symbol server key to be the same
                setSymbolServerKey = true;
            }
            else
            {
                source = SourceProvider.ResolveAndValidateSource(Source);
            }

            Settings.SetEncryptedValue(CommandLineUtility.ApiKeysSectionName, source, apiKey);

            string sourceName = CommandLineUtility.GetSourceDisplayName(source);

            // Setup the symbol server key
            if (setSymbolServerKey)
            {
                Settings.SetEncryptedValue(CommandLineUtility.ApiKeysSectionName, NuGetConstants.DefaultSymbolServerUrl, apiKey);
                Console.WriteLine(NuGetResources.SetApiKeyCommandDefaultApiKeysSaved,
                                  apiKey,
                                  sourceName,
                                  CommandLineUtility.GetSourceDisplayName(NuGetConstants.DefaultSymbolServerUrl));
            }
            else
            {
                Console.WriteLine(NuGetResources.SetApiKeyCommandApiKeySaved, apiKey, sourceName);
            }
        }
    }

Usage Example

 public void SetApiKeyThrowsIfPackageSourceProviderIsNull()
 {
     // Act and Assert
     var setApiKeyCommand = new SetApiKeyCommand();
     setApiKeyCommand.SourceProvider = null;            
     ExceptionAssert.Throws<InvalidOperationException>(() => setApiKeyCommand.ExecuteCommand(), "Property SourceProvider is null.");
 }
All Usage Examples Of NuGet.Commands.SetApiKeyCommand::ExecuteCommand