feat: add app settings manager to do the reading and writing
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
|
||||
<InternalsVisibleTo Include="$(AssemblyName).Tests" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -2,16 +2,14 @@ namespace AltGen.Console.Config;
|
||||
|
||||
sealed class AddConfigCommand(
|
||||
IAnsiConsole console,
|
||||
IFileSystem fileSystem
|
||||
IAppSettingsManager settingsManager
|
||||
) : AsyncCommand<AddConfigCommand.Settings>
|
||||
{
|
||||
readonly IAnsiConsole _console = console;
|
||||
readonly IFileSystem _fileSystem = fileSystem;
|
||||
readonly IAppSettingsManager _settingsManager = settingsManager;
|
||||
|
||||
public class Settings(IFileSystem filesystem) : CommandSettings
|
||||
public class Settings : CommandSettings
|
||||
{
|
||||
readonly IFileSystem _fileSystem = filesystem;
|
||||
|
||||
[CommandArgument(1, "<provider>")]
|
||||
[Description("The provider to configure.")]
|
||||
public string Provider { get; init; } = string.Empty;
|
||||
@@ -23,32 +21,23 @@ sealed class AddConfigCommand(
|
||||
[CommandOption("-d|--default")]
|
||||
[Description("Set the provider as the default.")]
|
||||
public bool Default { get; init; }
|
||||
|
||||
public string SettingsPath => _fileSystem.Path.Combine(AppContext.BaseDirectory, AppSettings.SettingsFileName);
|
||||
}
|
||||
|
||||
public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
|
||||
{
|
||||
var settingsExist = _fileSystem.File.Exists(settings.SettingsPath);
|
||||
|
||||
if (settingsExist is false)
|
||||
if (_settingsManager.AppSettingsExist() is false)
|
||||
{
|
||||
var appSettings = new AppSettings([
|
||||
new(settings.Provider, settings.Key, settings.Default)
|
||||
]);
|
||||
|
||||
var json = JsonSerializer.Serialize(appSettings, JsonOptions.Default);
|
||||
await _fileSystem.File.WriteAllTextAsync(settings.SettingsPath, json);
|
||||
await _settingsManager.SaveAppSettingsAsync(appSettings);
|
||||
_console.MarkupLine($"[bold]{settings.Provider}[/] has been configured.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
var existingJson = await _fileSystem.File.ReadAllTextAsync(settings.SettingsPath);
|
||||
var existingAppSettings = JsonSerializer.Deserialize<AppSettings>(existingJson, JsonOptions.Default)
|
||||
?? throw new ConfigException("Failed to deserialize settings.");
|
||||
var existingAppSettings = await _settingsManager.GetAppSettingsAsync();
|
||||
var updatedAppSettings = existingAppSettings.AddOrUpdateProvider(settings);
|
||||
var udpatedJson = JsonSerializer.Serialize(updatedAppSettings, JsonOptions.Default);
|
||||
await _fileSystem.File.WriteAllTextAsync(settings.SettingsPath, udpatedJson);
|
||||
await _settingsManager.SaveAppSettingsAsync(updatedAppSettings);
|
||||
_console.MarkupLine($"[bold]{settings.Provider}[/] has been configured.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
namespace AltGen.Console.Config;
|
||||
|
||||
class AppSettingsManager(IFileSystem fileSystem) : IAppSettingsManager
|
||||
{
|
||||
const string SettingsFileName = "appsettings.json";
|
||||
readonly IFileSystem _fileSystem = fileSystem;
|
||||
|
||||
public bool AppSettingsExist()
|
||||
{
|
||||
return _fileSystem.Path.Exists(SettingsFileName);
|
||||
}
|
||||
|
||||
public async Task<AppSettings> GetAppSettingsAsync()
|
||||
{
|
||||
var existingJson = await _fileSystem.File.ReadAllTextAsync(GetSettingsPath());
|
||||
var existingAppSettings = JsonSerializer.Deserialize<AppSettings>(existingJson, JsonOptions.Default) ?? new AppSettings([]);
|
||||
return existingAppSettings;
|
||||
}
|
||||
|
||||
public async Task SaveAppSettingsAsync(AppSettings appSettings)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(appSettings, JsonOptions.Default);
|
||||
await _fileSystem.File.WriteAllTextAsync(GetSettingsPath(), json);
|
||||
}
|
||||
|
||||
string GetSettingsPath()
|
||||
{
|
||||
return _fileSystem.Path.Combine(AppContext.BaseDirectory, SettingsFileName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace AltGen.Console.Config;
|
||||
|
||||
interface IAppSettingsManager
|
||||
{
|
||||
bool AppSettingsExist();
|
||||
Task<AppSettings> GetAppSettingsAsync();
|
||||
Task SaveAppSettingsAsync(AppSettings appSettings);
|
||||
}
|
||||
@@ -1,25 +1,23 @@
|
||||
|
||||
namespace AltGen.Console.Config;
|
||||
|
||||
sealed class ListConfigCommand(IAnsiConsole console, IFileSystem fileSystem) : AsyncCommand
|
||||
sealed class ListConfigCommand(
|
||||
IAnsiConsole console,
|
||||
IAppSettingsManager settingsManager
|
||||
) : AsyncCommand
|
||||
{
|
||||
readonly IAnsiConsole _console = console;
|
||||
readonly IFileSystem _fileSystem = fileSystem;
|
||||
readonly IAppSettingsManager _settingsManager = settingsManager;
|
||||
|
||||
public override async Task<int> ExecuteAsync(CommandContext context)
|
||||
{
|
||||
var settingsPath = _fileSystem.Path.Combine(AppContext.BaseDirectory, AppSettings.SettingsFileName);
|
||||
var settingsExist = _fileSystem.File.Exists(settingsPath);
|
||||
|
||||
if (settingsExist is false)
|
||||
if (_settingsManager.AppSettingsExist() is false)
|
||||
{
|
||||
_console.MarkupLine("No settings found.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
var settingsJson = await _fileSystem.File.ReadAllTextAsync(settingsPath);
|
||||
var appSettings = JsonSerializer.Deserialize<AppSettings>(settingsJson, JsonOptions.Default)
|
||||
?? throw new ConfigException("Failed to deserialize settings.");
|
||||
var appSettings = await _settingsManager.GetAppSettingsAsync();
|
||||
|
||||
foreach (var provider in appSettings.Providers)
|
||||
{
|
||||
|
||||
@@ -1,39 +1,30 @@
|
||||
namespace AltGen.Console.Config;
|
||||
|
||||
sealed class RemoveConfigCommand(
|
||||
IFileSystem fileSystem,
|
||||
IAnsiConsole console
|
||||
IAnsiConsole console,
|
||||
IAppSettingsManager settingsManager
|
||||
) : AsyncCommand<RemoveConfigCommand.Settings>
|
||||
{
|
||||
readonly IFileSystem _fileSystem = fileSystem;
|
||||
readonly IAnsiConsole _console = console;
|
||||
readonly IAppSettingsManager _settingsManager = settingsManager;
|
||||
|
||||
public class Settings(IFileSystem fileSystem) : CommandSettings
|
||||
public class Settings : CommandSettings
|
||||
{
|
||||
readonly IFileSystem _fileSystem = fileSystem;
|
||||
|
||||
[CommandArgument(1, "<provider>")]
|
||||
[Description("The provider to remove.")]
|
||||
public string Provider { get; init; } = string.Empty;
|
||||
|
||||
public string SettingsPath => _fileSystem.Path.Combine(AppContext.BaseDirectory, AppSettings.SettingsFileName);
|
||||
}
|
||||
|
||||
public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
|
||||
{
|
||||
var exists = _fileSystem.Path.Exists(settings.SettingsPath);
|
||||
|
||||
if (exists is false)
|
||||
if (_settingsManager.AppSettingsExist() is false)
|
||||
{
|
||||
throw new ConfigException("No existing settings found.");
|
||||
}
|
||||
|
||||
var settingsJson = await _fileSystem.File.ReadAllTextAsync(settings.SettingsPath);
|
||||
var appSettings = JsonSerializer.Deserialize<AppSettings>(settingsJson, JsonOptions.Default)
|
||||
?? throw new ConfigException("Failed to deserialize app settings.");
|
||||
var appSettings = await _settingsManager.GetAppSettingsAsync();
|
||||
var updatedAppSettings = appSettings.RemoveProvider(settings);
|
||||
var updatedJson = JsonSerializer.Serialize(updatedAppSettings, JsonOptions.Default);
|
||||
await _fileSystem.File.WriteAllTextAsync(settings.SettingsPath, updatedJson);
|
||||
await _settingsManager.SaveAppSettingsAsync(updatedAppSettings);
|
||||
_console.MarkupLine($"[bold]{settings.Provider}[/] has been removed.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
interface IAltGenService
|
||||
{
|
||||
Task<string> GenerateAltTextAsync(GenerateAltTextRequest req);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
.ConfigureServices(static (_, services) =>
|
||||
{
|
||||
services.AddSingleton<IFileSystem, FileSystem>();
|
||||
services.AddSingleton<IAppSettingsManager, AppSettingsManager>();
|
||||
services.AddSingleton(AnsiConsole.Console);
|
||||
services.AddHttpClient<IAltGenService, AltGenService>()
|
||||
.AddStandardResilienceHandler();
|
||||
|
||||
Reference in New Issue
Block a user