-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Implement ConfigCommand and improve configuration handling
Implement the ConfigCommand class to manage configuration settings in SKonsole. Add get and set commands for handling configuration keys and values. Update ConfigurationProvider to read and write configuration data. Replace the usage of environment variables with configuration variables using the ConfigVar method. Update the error message to instruct users to run `skonsole config` to set the configuration variable. Add a new method ReadMutiLineInput to support multi-line user input in the console. PR: #3 Co-authored-by: xbotter <xbotter@users.noreply.github.com>
- Loading branch information
1 parent
b69aed7
commit 7c1226d
Showing
4 changed files
with
193 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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,102 @@ | ||
using Newtonsoft.Json.Linq; | ||
using Spectre.Console; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.CommandLine; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace SKonsole.Commands | ||
{ | ||
public class ConfigCommand : Command | ||
{ | ||
private readonly ConfigurationProvider _config; | ||
|
||
public ConfigCommand(ConfigurationProvider config) : base("config", "skonsole configuration") | ||
{ | ||
this._config = config; | ||
Add(ConfigGetCommand()); | ||
Add(ConfigSetCommand()); | ||
|
||
this.SetHandler(async context => await RunConfigAsync(context.GetCancellationToken())); | ||
} | ||
|
||
Command ConfigGetCommand() | ||
{ | ||
var keyArgument = new Argument<string>("key", "configuration key"); | ||
|
||
var getCommand = new Command("get", "get configuration value"); | ||
|
||
getCommand.AddArgument(keyArgument); | ||
|
||
getCommand.SetHandler((key) => | ||
{ | ||
var value = _config.Get(key); | ||
if (value == null) | ||
{ | ||
AnsiConsole.MarkupLine($"[red]Configuration key '{key}' not found.[/]"); | ||
} | ||
else | ||
{ | ||
AnsiConsole.MarkupLine($"[green]{key}[/]: {value}"); | ||
} | ||
}, keyArgument); | ||
return getCommand; | ||
} | ||
|
||
|
||
Command ConfigSetCommand() | ||
{ | ||
var keyArgument = new Argument<string>("key", "configuration key"); | ||
var valueArgument = new Argument<string>("value", "configuration value"); | ||
|
||
var setCommand = new Command("set", "set configuration value"); | ||
setCommand.AddArgument(keyArgument); | ||
setCommand.AddArgument(valueArgument); | ||
|
||
setCommand.SetHandler(_config.SaveConfig, keyArgument, valueArgument); | ||
return setCommand; | ||
} | ||
|
||
static async Task RunConfigAsync(CancellationToken token) | ||
{ | ||
while (!token.IsCancellationRequested) | ||
{ | ||
var keys = new[] | ||
{ | ||
"AZURE_OPENAI_CHAT_DEPLOYMENT_NAME", | ||
"AZURE_OPENAI_API_ENDPOINT", | ||
"AZURE_OPENAI_API_KEY" | ||
}; | ||
|
||
var config = new ConfigurationProvider(); | ||
var configKey = await new SelectionPrompt<string>() | ||
.Title("Select key to config:") | ||
.AddChoices(keys) | ||
.ShowAsync(AnsiConsole.Console, token); | ||
|
||
var currentValue = config.Get(configKey); | ||
|
||
var value = await new TextPrompt<string>($"Set value for [green]{configKey}[/]") | ||
.DefaultValue(currentValue ?? string.Empty) | ||
.HideDefaultValue() | ||
.Validate((value) => | ||
{ | ||
if (string.IsNullOrWhiteSpace(value)) | ||
{ | ||
return ValidationResult.Error("[red]Value cannot be empty[/]"); | ||
} | ||
return ValidationResult.Success(); | ||
}) | ||
.AllowEmpty() | ||
.ShowAsync(AnsiConsole.Console, token); | ||
if (!string.IsNullOrWhiteSpace(value)) | ||
{ | ||
await config.SaveConfig(configKey, value.Trim()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains 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,60 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using System.Text.Json; | ||
|
||
public class ConfigurationProvider | ||
{ | ||
public static ConfigurationProvider Instance = new(); | ||
|
||
const string _file = ".skonsole"; | ||
|
||
private readonly string _path; | ||
private readonly IConfiguration _configuration; | ||
private readonly Dictionary<string, string?> _config = new(); | ||
|
||
public ConfigurationProvider() | ||
{ | ||
var userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); | ||
_path = Path.Combine(userProfile, _file); | ||
|
||
if (File.Exists(_path)) | ||
{ | ||
_config = FromJson<Dictionary<string, string?>>(File.ReadAllText(_path)) ?? new(); | ||
} | ||
|
||
_configuration = new ConfigurationBuilder() | ||
.AddEnvironmentVariables() | ||
.AddInMemoryCollection(_config) | ||
.Build(); | ||
} | ||
|
||
public async Task SaveConfig(string key, string value) | ||
{ | ||
_config[key] = value; | ||
|
||
await File.WriteAllTextAsync(_path, ToJson(_config)); | ||
} | ||
|
||
public string? Get(string key) | ||
{ | ||
return _configuration[key]; | ||
} | ||
|
||
private static string ToJson<T>(T obj) | ||
{ | ||
return JsonSerializer.Serialize(obj); | ||
} | ||
|
||
private static T? FromJson<T>(string json) | ||
{ | ||
if (json == null) | ||
return default; | ||
try | ||
{ | ||
return JsonSerializer.Deserialize<T>(json); | ||
} | ||
catch | ||
{ | ||
return default; | ||
} | ||
} | ||
} |
This file contains 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 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