using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Configuration;
namespace Jellyfin.Server.Extensions;
///
/// Extensions for building the application configuration.
///
public static class ConfigurationBuilderExtensions
{
///
/// The environment variable prefix that maps onto this fork's own Jellyfin:* configuration
/// keys, for example Jellyfin__TranscodeStore__RedisConnectionString.
///
public const string JellyfinSectionEnvironmentPrefix = "Jellyfin__";
///
/// The configuration section root this fork keeps its own settings under.
///
public const string JellyfinSectionRoot = "Jellyfin";
///
/// Adds environment variables named Jellyfin__Section__Key as the configuration keys
/// Jellyfin:Section:Key.
///
///
/// The base configuration only reads JELLYFIN_ prefixed environment variables, so the
/// unprefixed form every manifest, chart and document uses would otherwise be dropped and the
/// feature it configures would stay off with no error.
///
/// The configuration builder.
/// The updated configuration builder.
public static IConfigurationBuilder AddJellyfinSectionEnvironmentVariables(this IConfigurationBuilder builder)
{
// Read through the framework provider so "__" to ":" normalisation and case handling match the
// prefixed form exactly; the prefix it strips is then put back as the section root.
var scoped = new ConfigurationBuilder()
.AddEnvironmentVariables(JellyfinSectionEnvironmentPrefix)
.Build();
var entries = scoped.AsEnumerable()
.Where(entry => entry.Value is not null)
.Select(entry => new KeyValuePair(
ConfigurationPath.Combine(JellyfinSectionRoot, entry.Key),
entry.Value))
.ToList();
return builder.AddInMemoryCollection(entries);
}
}