Files
alt-gen/src/AltGen.Console/Config/RemoveConfigCommand.cs
T

40 lines
1.4 KiB
C#
Raw Normal View History

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
public string SettingsPath => _fileSystem.Path.Combine(AppContext.BaseDirectory, "appsettings.json");
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)
{
_console.MarkupLine("[bold]No existing settings found.[/]");
return 1;
}
var settingsJson = await _fileSystem.File.ReadAllTextAsync(settings.SettingsPath);
var appSettings = JsonSerializer.Deserialize<AppSettings>(settingsJson) ?? throw new ConfigException("Failed to deserialize app settings.");
var updatedAppSettings = appSettings.RemoveProvider(settings);
var updatedJson = JsonSerializer.Serialize(updatedAppSettings);
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;
}
}