mirror of
https://github.com/Tribufu/TribufuDotNet
synced 2026-08-08 04:11:06 +00:00
* Migrate to new tribufu project structure * Update .gitignore * Remove old api files * Update Directory.Build.props * Rename solution
50 lines
1.7 KiB
C#
50 lines
1.7 KiB
C#
// Copyright (c) Tribufu. All Rights Reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
using System.IO;
|
|
using Tomlyn.Extensions.Configuration;
|
|
using Tribufu.Logging;
|
|
using Tribufu.Platform;
|
|
|
|
namespace Tribufu.Configuration
|
|
{
|
|
public static class ConfigurationLoader
|
|
{
|
|
public static IConfiguration Load(string[] fileNames)
|
|
{
|
|
var configDirectory = Paths.GetApplicationConfigDirectory();
|
|
var configurationBuilder = new ConfigurationBuilder();
|
|
configurationBuilder.AddEnvironmentVariables();
|
|
|
|
foreach (var fileName in fileNames)
|
|
{
|
|
var fullPath = Path.Combine(configDirectory, fileName);
|
|
if (!File.Exists(fullPath))
|
|
{
|
|
Logger.Debug($"Config file '{fullPath}' not found, skipping.");
|
|
continue;
|
|
}
|
|
|
|
var extension = Path.GetExtension(fullPath).ToLowerInvariant();
|
|
switch (extension)
|
|
{
|
|
case ".ini":
|
|
configurationBuilder.AddIniFile(fullPath, true, false);
|
|
break;
|
|
case ".json":
|
|
configurationBuilder.AddJsonFile(fullPath, true, false);
|
|
break;
|
|
case ".toml":
|
|
configurationBuilder.AddTomlFile(fullPath, true, false);
|
|
break;
|
|
default:
|
|
Logger.Warn($"Unsupported config file extension: {extension}");
|
|
break;
|
|
}
|
|
}
|
|
|
|
return configurationBuilder.Build();
|
|
}
|
|
}
|
|
}
|