-
Notifications
You must be signed in to change notification settings - Fork 648
Add aspire config commands for managing configuration settings #9676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a0ebf12
Initial plan for issue
Copilot 975d4fb
Implement config commands with set/get functionality
Copilot 18855a6
Fix ConfigCommand to use IInteractionService instead of IAnsiConsole
Copilot a9de999
Add config list and delete commands with comprehensive functionality
Copilot 5cb52b0
Refactor config architecture to use built-in .NET configuration
Copilot ae7c7d1
Update configuration loading to use nearest local file and correct pr…
Copilot f59c5ef
Modify CliTestHelper to support explicit settings content instead of …
Copilot b48f7da
Fix CliTestHelper and ConfigCommand test issues - remove file discove…
Copilot e6c94e9
Remove Directory.SetCurrentDirectory from additional test
Copilot 5710af2
Add test to verify config preservation when appHostPath is set
Copilot d40e883
Fix CliTestHelper to provide unique temporary directories for test is…
Copilot 11d7677
WIP: Fixing up broken tests.
mitchdenny 140edbd
WIP.
mitchdenny 2529ed4
Tests passing.
mitchdenny 1b0d90d
Ooops.
mitchdenny e35ef4f
FIx config list commmand.
mitchdenny fe3ffea
Fix configuration precedence order - local settings now properly over…
Copilot d70a728
Add test back in.
mitchdenny c517e8d
Namespaces.
mitchdenny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.CommandLine; | ||
using Aspire.Cli.Configuration; | ||
using Aspire.Cli.Interaction; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Aspire.Cli.Commands; | ||
|
||
internal sealed class ConfigCommand : BaseCommand | ||
{ | ||
private readonly IConfiguration _configuration; | ||
private readonly IConfigurationService _configurationService; | ||
private readonly IInteractionService _interactionService; | ||
|
||
public ConfigCommand(IConfiguration configuration, IConfigurationService configurationService, IInteractionService interactionService) | ||
: base("config", "Manage configuration settings.") | ||
{ | ||
ArgumentNullException.ThrowIfNull(configuration); | ||
ArgumentNullException.ThrowIfNull(configurationService); | ||
ArgumentNullException.ThrowIfNull(interactionService); | ||
|
||
_configuration = configuration; | ||
_configurationService = configurationService; | ||
_interactionService = interactionService; | ||
|
||
var getCommand = new GetCommand(_configuration, _interactionService); | ||
var setCommand = new SetCommand(configurationService, _interactionService); | ||
var listCommand = new ListCommand(configurationService, _interactionService); | ||
var deleteCommand = new DeleteCommand(configurationService, _interactionService); | ||
|
||
Subcommands.Add(getCommand); | ||
Subcommands.Add(setCommand); | ||
Subcommands.Add(listCommand); | ||
Subcommands.Add(deleteCommand); | ||
} | ||
|
||
protected override Task<int> ExecuteAsync(ParseResult parseResult, CancellationToken cancellationToken) | ||
{ | ||
// When no subcommand is provided, the system will automatically show help and return 0 | ||
return Task.FromResult(0); | ||
} | ||
|
||
private sealed class GetCommand : BaseCommand | ||
{ | ||
private readonly IConfiguration _configuration; | ||
private readonly IInteractionService _interactionService; | ||
|
||
public GetCommand(IConfiguration configuration, IInteractionService interactionService) | ||
: base("get", "Get a configuration value.") | ||
{ | ||
_configuration = configuration; | ||
_interactionService = interactionService; | ||
|
||
var keyArgument = new Argument<string>("key") | ||
{ | ||
Description = "The configuration key to retrieve." | ||
}; | ||
Arguments.Add(keyArgument); | ||
} | ||
|
||
protected override Task<int> ExecuteAsync(ParseResult parseResult, CancellationToken cancellationToken) | ||
{ | ||
var key = parseResult.GetValue<string>("key"); | ||
if (key is null) | ||
{ | ||
_interactionService.DisplayError("Configuration key is required."); | ||
return Task.FromResult(1); | ||
} | ||
|
||
var value = _configuration[key]; | ||
|
||
if (value is not null) | ||
{ | ||
Console.WriteLine(value); | ||
return Task.FromResult(0); | ||
} | ||
else | ||
{ | ||
_interactionService.DisplayError($"Configuration key '{key}' not found."); | ||
return Task.FromResult(1); | ||
} | ||
} | ||
} | ||
|
||
private sealed class SetCommand : BaseCommand | ||
{ | ||
private readonly IConfigurationService _configurationService; | ||
private readonly IInteractionService _interactionService; | ||
|
||
public SetCommand(IConfigurationService configurationService, IInteractionService interactionService) | ||
: base("set", "Set a configuration value.") | ||
{ | ||
_configurationService = configurationService; | ||
_interactionService = interactionService; | ||
|
||
var keyArgument = new Argument<string>("key") | ||
{ | ||
Description = "The configuration key to set." | ||
}; | ||
Arguments.Add(keyArgument); | ||
|
||
var valueArgument = new Argument<string>("value") | ||
{ | ||
Description = "The configuration value to set." | ||
}; | ||
Arguments.Add(valueArgument); | ||
|
||
var globalOption = new Option<bool>("--global", "-g") | ||
{ | ||
Description = "Set the configuration value globally in $HOME/.aspire/settings.json instead of the local settings file." | ||
}; | ||
Options.Add(globalOption); | ||
} | ||
|
||
protected override async Task<int> ExecuteAsync(ParseResult parseResult, CancellationToken cancellationToken) | ||
{ | ||
var key = parseResult.GetValue<string>("key"); | ||
var value = parseResult.GetValue<string>("value"); | ||
var isGlobal = parseResult.GetValue<bool>("--global"); | ||
|
||
if (key is null) | ||
{ | ||
_interactionService.DisplayError("Configuration key is required."); | ||
return 1; | ||
} | ||
|
||
if (value is null) | ||
{ | ||
_interactionService.DisplayError("Configuration value is required."); | ||
return 1; | ||
} | ||
|
||
try | ||
{ | ||
await _configurationService.SetConfigurationAsync(key, value, isGlobal, cancellationToken); | ||
var scope = isGlobal ? "globally" : "locally"; | ||
_interactionService.DisplaySuccess($"Configuration '{key}' set to '{value}' {scope}."); | ||
return 0; | ||
} | ||
catch (Exception ex) | ||
{ | ||
_interactionService.DisplayError($"Error setting configuration: {ex.Message}"); | ||
return 1; | ||
} | ||
} | ||
} | ||
|
||
private sealed class ListCommand : BaseCommand | ||
{ | ||
private readonly IConfigurationService _configurationService; | ||
private readonly IInteractionService _interactionService; | ||
|
||
public ListCommand(IConfigurationService configurationService, IInteractionService interactionService) | ||
: base("list", "List all configuration values.") | ||
{ | ||
_configurationService = configurationService; | ||
_interactionService = interactionService; | ||
} | ||
|
||
protected override async Task<int> ExecuteAsync(ParseResult parseResult, CancellationToken cancellationToken) | ||
{ | ||
var allConfig = await _configurationService.GetAllConfigurationAsync(cancellationToken); | ||
|
||
if (allConfig.Count == 0) | ||
{ | ||
_interactionService.DisplayMessage("ℹ️", "No configuration values found."); | ||
return ExitCodeConstants.Success; | ||
} | ||
|
||
foreach (var kvp in allConfig) | ||
{ | ||
Console.WriteLine($"{kvp.Key}={kvp.Value}"); | ||
} | ||
|
||
return ExitCodeConstants.Success; | ||
} | ||
} | ||
|
||
private sealed class DeleteCommand : BaseCommand | ||
{ | ||
private readonly IConfigurationService _configurationService; | ||
private readonly IInteractionService _interactionService; | ||
|
||
public DeleteCommand(IConfigurationService configurationService, IInteractionService interactionService) | ||
: base("delete", "Delete a configuration value.") | ||
{ | ||
_configurationService = configurationService; | ||
_interactionService = interactionService; | ||
|
||
var keyArgument = new Argument<string>("key") | ||
{ | ||
Description = "The configuration key to delete." | ||
}; | ||
Arguments.Add(keyArgument); | ||
|
||
var globalOption = new Option<bool>("--global", "-g") | ||
{ | ||
Description = "Delete the configuration value from the global $HOME/.aspire/settings.json instead of the local settings file." | ||
}; | ||
Options.Add(globalOption); | ||
} | ||
|
||
protected override async Task<int> ExecuteAsync(ParseResult parseResult, CancellationToken cancellationToken) | ||
{ | ||
var key = parseResult.GetValue<string>("key"); | ||
var isGlobal = parseResult.GetValue<bool>("--global"); | ||
|
||
if (key is null) | ||
{ | ||
_interactionService.DisplayError("Configuration key is required."); | ||
return 1; | ||
} | ||
|
||
try | ||
{ | ||
var deleted = await _configurationService.DeleteConfigurationAsync(key, isGlobal, cancellationToken); | ||
|
||
if (deleted) | ||
{ | ||
var scope = isGlobal ? "globally" : "locally"; | ||
_interactionService.DisplaySuccess($"Configuration '{key}' deleted {scope}."); | ||
return 0; | ||
} | ||
else | ||
{ | ||
_interactionService.DisplayError($"Configuration key '{key}' not found."); | ||
return 1; | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
_interactionService.DisplayError($"Error deleting configuration: {ex.Message}"); | ||
return 1; | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.