feat: add app settings manager to do the reading and writing

This commit is contained in:
Stevan Freeborn
2025-03-14 22:02:22 -05:00
parent e9b9f7fc13
commit 414e712f51
14 changed files with 229 additions and 214 deletions
@@ -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;
}