mirror of
https://github.com/Tribufu/TribufuDotNet
synced 2026-08-09 04:41:07 +00:00
Rename ConfigurationLoader
This commit is contained in:
parent
f6c296d9b6
commit
13c5d6a71c
1 changed files with 1 additions and 1 deletions
53
src/Tribufu.Configuration/ConfigurationLoader.cs
Normal file
53
src/Tribufu.Configuration/ConfigurationLoader.cs
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
// 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.Runtime;
|
||||
|
||||
namespace Tribufu.Configuration
|
||||
{
|
||||
public static class ConfigurationLoader
|
||||
{
|
||||
public static IConfiguration Configuration { get; private set; }
|
||||
|
||||
public static IConfiguration Load(string[] fileNames)
|
||||
{
|
||||
var configDirectory = ApplicationContext.GetConfigDirectory();
|
||||
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 ext = Path.GetExtension(fullPath).ToLowerInvariant();
|
||||
switch (ext)
|
||||
{
|
||||
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: {ext}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Configuration = configurationBuilder.Build();
|
||||
return Configuration;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue