feat(configmanager): add AutoDiscoveryService with tests
Add service for auto-discovering configuration file locations. The service searches in prioritized order: 1. JDESCOPING_CONFIG_PATH environment variable 2. Same directory as executable 3. ../JdeScoping.Host/ relative to executable 4. User config directory (~/.jdescoping on Unix, %LOCALAPPDATA%\JdeScoping on Windows) Includes 9 unit tests covering all search locations, priority order, edge cases (missing directory, missing appsettings.json), and cancellation token support.
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace JdeScoping.ConfigManager.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Service for auto-discovering configuration file locations.
|
||||
/// Searches in prioritized order:
|
||||
/// 1. JDESCOPING_CONFIG_PATH environment variable
|
||||
/// 2. Same directory as executable
|
||||
/// 3. ../JdeScoping.Host/ relative to executable
|
||||
/// 4. User config directory (~/.jdescoping on Unix, %LOCALAPPDATA%\JdeScoping on Windows)
|
||||
/// </summary>
|
||||
public class AutoDiscoveryService : IAutoDiscoveryService
|
||||
{
|
||||
private readonly IFileSystem _fileSystem;
|
||||
private readonly ILogger<AutoDiscoveryService>? _logger;
|
||||
private const string EnvVarName = "JDESCOPING_CONFIG_PATH";
|
||||
private const string AppSettingsFileName = "appsettings.json";
|
||||
|
||||
public AutoDiscoveryService(IFileSystem fileSystem, ILogger<AutoDiscoveryService>? logger = null)
|
||||
{
|
||||
_fileSystem = fileSystem;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task<string?> FindConfigFolderAsync(CancellationToken ct = default)
|
||||
{
|
||||
// 1. Check environment variable
|
||||
var envPath = Environment.GetEnvironmentVariable(EnvVarName);
|
||||
if (!string.IsNullOrEmpty(envPath) && IsValidConfigFolder(envPath))
|
||||
{
|
||||
_logger?.LogInformation("Found config folder from environment variable: {Path}", envPath);
|
||||
return Task.FromResult<string?>(envPath);
|
||||
}
|
||||
|
||||
// 2. Check same directory as executable
|
||||
var exeDir = AppContext.BaseDirectory;
|
||||
if (IsValidConfigFolder(exeDir))
|
||||
{
|
||||
_logger?.LogInformation("Found config folder in executable directory: {Path}", exeDir);
|
||||
return Task.FromResult<string?>(exeDir);
|
||||
}
|
||||
|
||||
// 3. Check ../JdeScoping.Host/ relative to executable
|
||||
var hostDir = _fileSystem.Combine(exeDir, "..", "JdeScoping.Host");
|
||||
if (IsValidConfigFolder(hostDir))
|
||||
{
|
||||
_logger?.LogInformation("Found config folder in host directory: {Path}", hostDir);
|
||||
return Task.FromResult<string?>(hostDir);
|
||||
}
|
||||
|
||||
// 4. Check user config directory
|
||||
var userConfigDir = GetUserConfigDirectory();
|
||||
if (userConfigDir != null && IsValidConfigFolder(userConfigDir))
|
||||
{
|
||||
_logger?.LogInformation("Found config folder in user directory: {Path}", userConfigDir);
|
||||
return Task.FromResult<string?>(userConfigDir);
|
||||
}
|
||||
|
||||
_logger?.LogWarning("Could not find config folder in any standard location");
|
||||
return Task.FromResult<string?>(null);
|
||||
}
|
||||
|
||||
private bool IsValidConfigFolder(string path)
|
||||
{
|
||||
if (!_fileSystem.DirectoryExists(path))
|
||||
return false;
|
||||
|
||||
var appSettingsPath = _fileSystem.Combine(path, AppSettingsFileName);
|
||||
return _fileSystem.FileExists(appSettingsPath);
|
||||
}
|
||||
|
||||
private string? GetUserConfigDirectory()
|
||||
{
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
||||
return _fileSystem.Combine(localAppData, "JdeScoping");
|
||||
}
|
||||
else
|
||||
{
|
||||
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
|
||||
return _fileSystem.Combine(home, ".jdescoping");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace JdeScoping.ConfigManager.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Service for auto-discovering configuration file locations.
|
||||
/// </summary>
|
||||
public interface IAutoDiscoveryService
|
||||
{
|
||||
/// <summary>
|
||||
/// Searches for a configuration folder containing appsettings.json.
|
||||
/// Search order:
|
||||
/// 1. JDESCOPING_CONFIG_PATH environment variable
|
||||
/// 2. Same directory as executable
|
||||
/// 3. ../JdeScoping.Host/ relative to executable
|
||||
/// 4. User config directory (~/.jdescoping on Unix, %LOCALAPPDATA%\JdeScoping on Windows)
|
||||
/// </summary>
|
||||
/// <param name="ct">Cancellation token.</param>
|
||||
/// <returns>The path to the config folder, or null if not found.</returns>
|
||||
Task<string?> FindConfigFolderAsync(CancellationToken ct = default);
|
||||
}
|
||||
Reference in New Issue
Block a user