/// <summary>
        /// The DefaultPushSource can be:
        /// - An absolute URL
        /// - An absolute file path
        /// - A relative file path
        /// - The name of a registered source from a config file
        /// </summary>
        public static string GetDefaultPushSource(ISettings settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            var configSection = settings.GetSection(ConfigurationConstants.Config);
            var configSetting = configSection?.GetFirstItemWithAttribute <AddItem>(ConfigurationConstants.KeyAttribute, ConfigurationConstants.DefaultPushSource);

            var source = configSetting?.Value;

            var sourceUri = UriUtility.TryCreateSourceUri(source, UriKind.RelativeOrAbsolute);

            if (sourceUri != null && !sourceUri.IsAbsoluteUri)
            {
                // For non-absolute sources, it could be the name of a config source, or a relative file path.
                IPackageSourceProvider sourceProvider = new PackageSourceProvider(settings);
                var allSources = sourceProvider.LoadPackageSources();

                if (!allSources.Any(s => s.IsEnabled && s.Name.Equals(source, StringComparison.OrdinalIgnoreCase)))
                {
                    // It wasn't the name of a source, so treat it like a relative file
                    source = Settings.ResolvePathFromOrigin(configSetting.Origin.DirectoryPath, configSetting.Origin.ConfigFilePath, source);
                }
            }

            return(source);
        }
Example #2
0
        public static string GetDecryptedValueForAddItem(ISettings settings, string section, string key, bool isPath = false)
        {
            if (string.IsNullOrEmpty(section))
            {
                throw new ArgumentException(Resources.Argument_Cannot_Be_Null_Or_Empty, nameof(section));
            }

            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException(Resources.Argument_Cannot_Be_Null_Or_Empty, nameof(key));
            }

            var sectionElement = settings.GetSection(section);

            var encryptedItem   = sectionElement?.GetFirstItemWithAttribute <AddItem>(ConfigurationConstants.KeyAttribute, key);
            var encryptedString = encryptedItem?.Value;

            if (encryptedString == null)
            {
                return(null);
            }

            var decryptedString = EncryptionUtility.DecryptString(encryptedString);

            if (isPath)
            {
                return(Settings.ResolvePathFromOrigin(encryptedItem.Origin.DirectoryPath, encryptedItem.Origin.ConfigFilePath, decryptedString));
            }

            return(decryptedString);
        }