32 lines
1.0 KiB
C#
32 lines
1.0 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace FlowScope.Config
|
|
{
|
|
public sealed class FileConfigSource : IConfigSource
|
|
{
|
|
private readonly string _rootPath;
|
|
|
|
public FileConfigSource(string rootPath, string name = "File")
|
|
{
|
|
_rootPath = string.IsNullOrWhiteSpace(rootPath)
|
|
? throw new ArgumentException("Config root path is required.", nameof(rootPath))
|
|
: rootPath;
|
|
Name = string.IsNullOrWhiteSpace(name)
|
|
? throw new ArgumentException("Config source name is required.", nameof(name))
|
|
: name;
|
|
}
|
|
|
|
public string Name { get; }
|
|
|
|
public Task<string> LoadTextAsync(string fileName, CancellationToken cancellationToken)
|
|
{
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
var path = Path.Combine(_rootPath, fileName);
|
|
return Task.Run(() => File.ReadAllText(path), cancellationToken);
|
|
}
|
|
}
|
|
}
|