mirror of
https://github.com/Tribufu/TribufuDotNet
synced 2026-08-09 12:51:06 +00:00
Add shared packages
This commit is contained in:
parent
6ebfde013a
commit
5b2588b47f
32 changed files with 981 additions and 7 deletions
53
src/Tribufu.Configuration/ConfigurationManager.cs
Normal file
53
src/Tribufu.Configuration/ConfigurationManager.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 ConfigurationManager
|
||||
{
|
||||
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