2025-02-24 22:53:55 -06:00
|
|
|
namespace AltGen.Console.Config;
|
|
|
|
|
|
2025-03-08 19:31:56 -06:00
|
|
|
sealed class RemoveConfigCommand(
|
|
|
|
|
IFileSystem fileSystem,
|
|
|
|
|
IAnsiConsole console
|
|
|
|
|
) : AsyncCommand<RemoveConfigCommand.Settings>
|
2025-02-24 22:53:55 -06:00
|
|
|
{
|
2025-03-08 19:31:56 -06:00
|
|
|
readonly IFileSystem _fileSystem = fileSystem;
|
|
|
|
|
readonly IAnsiConsole _console = console;
|
|
|
|
|
|
|
|
|
|
public class Settings(IFileSystem fileSystem) : CommandSettings
|
2025-02-24 22:53:55 -06:00
|
|
|
{
|
2025-03-08 19:31:56 -06:00
|
|
|
readonly IFileSystem _fileSystem = fileSystem;
|
|
|
|
|
|
2025-02-24 22:53:55 -06:00
|
|
|
[CommandArgument(1, "<provider>")]
|
|
|
|
|
[Description("The provider to remove.")]
|
|
|
|
|
public string Provider { get; init; } = string.Empty;
|
2025-03-08 19:31:56 -06:00
|
|
|
|
2025-03-10 21:45:46 -05:00
|
|
|
public string SettingsPath => _fileSystem.Path.Combine(AppContext.BaseDirectory, AppSettings.SettingsFileName);
|
2025-02-24 22:53:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
|
|
|
|
|
{
|
2025-03-08 19:31:56 -06:00
|
|
|
var exists = _fileSystem.Path.Exists(settings.SettingsPath);
|
|
|
|
|
|
|
|
|
|
if (exists is false)
|
|
|
|
|
{
|
2025-03-09 12:49:28 -05:00
|
|
|
throw new ConfigException("No existing settings found.");
|
2025-03-08 19:31:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var settingsJson = await _fileSystem.File.ReadAllTextAsync(settings.SettingsPath);
|
2025-03-10 21:45:46 -05:00
|
|
|
var appSettings = JsonSerializer.Deserialize<AppSettings>(settingsJson, JsonOptions.Default)
|
|
|
|
|
?? throw new ConfigException("Failed to deserialize app settings.");
|
2025-03-08 19:31:56 -06:00
|
|
|
var updatedAppSettings = appSettings.RemoveProvider(settings);
|
2025-03-09 12:49:28 -05:00
|
|
|
var updatedJson = JsonSerializer.Serialize(updatedAppSettings, JsonOptions.Default);
|
2025-03-08 19:31:56 -06:00
|
|
|
await _fileSystem.File.WriteAllTextAsync(settings.SettingsPath, updatedJson);
|
|
|
|
|
_console.MarkupLine($"[bold]{settings.Provider}[/] has been removed.");
|
2025-02-24 22:53:55 -06:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|