feat: add remove, list, and add config commands
This commit is contained in:
@@ -9,12 +9,11 @@ static class HostBuilderExtensions
|
||||
|
||||
app.Configure(static c =>
|
||||
{
|
||||
c.PropagateExceptions();
|
||||
|
||||
c.AddBranch("config", static c =>
|
||||
{
|
||||
c.AddCommand<AddConfigCommand>("add");
|
||||
c.AddCommand<RemoveConfigCommand>("remove");
|
||||
c.AddCommand<ListConfigCommand>("list");
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ sealed class AddConfigCommand(
|
||||
[Description("Set the provider as the default.")]
|
||||
public bool Default { get; init; }
|
||||
|
||||
public string SettingsPath => _fileSystem.Path.Combine(AppContext.BaseDirectory, "appsettings.json");
|
||||
public string SettingsPath => _fileSystem.Path.Combine(AppContext.BaseDirectory, AppSettings.SettingsFileName);
|
||||
}
|
||||
|
||||
public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
|
||||
|
||||
@@ -2,6 +2,8 @@ namespace AltGen.Console.Config;
|
||||
|
||||
record AppSettings
|
||||
{
|
||||
public const string SettingsFileName = "appsettings.json";
|
||||
|
||||
public ProviderSettings[] Providers { get; init; } = [];
|
||||
|
||||
public AppSettings(ProviderSettings[] providers)
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
|
||||
namespace AltGen.Console.Config;
|
||||
|
||||
sealed class ListConfigCommand(IAnsiConsole console, IFileSystem fileSystem) : AsyncCommand
|
||||
{
|
||||
readonly IAnsiConsole _console = console;
|
||||
readonly IFileSystem _fileSystem = fileSystem;
|
||||
|
||||
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)
|
||||
{
|
||||
_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.");
|
||||
|
||||
foreach (var provider in appSettings.Providers)
|
||||
{
|
||||
var providerName = provider.Provider;
|
||||
var providerKey = provider.Key;
|
||||
var isDefault = provider.Default ? " (default)" : string.Empty;
|
||||
_console.MarkupLine($"[bold]{providerName}[/] [dim]{providerKey}[/]{isDefault}");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ sealed class RemoveConfigCommand(
|
||||
[Description("The provider to remove.")]
|
||||
public string Provider { get; init; } = string.Empty;
|
||||
|
||||
public string SettingsPath => _fileSystem.Path.Combine(AppContext.BaseDirectory, "appsettings.json");
|
||||
public string SettingsPath => _fileSystem.Path.Combine(AppContext.BaseDirectory, AppSettings.SettingsFileName);
|
||||
}
|
||||
|
||||
public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
|
||||
@@ -29,7 +29,8 @@ sealed class RemoveConfigCommand(
|
||||
}
|
||||
|
||||
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 = JsonSerializer.Deserialize<AppSettings>(settingsJson, JsonOptions.Default)
|
||||
?? throw new ConfigException("Failed to deserialize app settings.");
|
||||
var updatedAppSettings = appSettings.RemoveProvider(settings);
|
||||
var updatedJson = JsonSerializer.Serialize(updatedAppSettings, JsonOptions.Default);
|
||||
await _fileSystem.File.WriteAllTextAsync(settings.SettingsPath, updatedJson);
|
||||
|
||||
Reference in New Issue
Block a user