-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Solution): Added the CLI application to the solution (WIP)
Signed-off-by: Charles d'Avernas <[email protected]>
- Loading branch information
Showing
31 changed files
with
837 additions
and
42 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
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
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
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
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
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,14 @@ | ||
namespace Synapse.Cli; | ||
|
||
/// <summary> | ||
/// Exposes constants and statics used by the CLI | ||
/// </summary> | ||
internal static class CliConstants | ||
{ | ||
|
||
/// <summary> | ||
/// Gets the name of the CLI configuration file | ||
/// </summary> | ||
public const string ConfigurationFileName = "config.yaml"; | ||
|
||
} |
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,41 @@ | ||
namespace Synapse.Cli.Commands; | ||
|
||
/// <summary> | ||
/// Represents the base class for all <see cref="System.CommandLine.Command"/> implementations | ||
/// </summary> | ||
public abstract class Command | ||
: System.CommandLine.Command | ||
{ | ||
|
||
/// <summary> | ||
/// Initializes a new <see cref="Command"/> | ||
/// </summary> | ||
/// <param name="serviceProvider">The current <see cref="IServiceProvider"/></param> | ||
/// <param name="loggerFactory">The service used to create <see cref="ILogger"/>s</param> | ||
/// <param name="api">The service used to interact with the remote Synapse API</param> | ||
/// <param name="name">The <see cref="Command"/>'s name</param> | ||
/// <param name="description">The <see cref="Command"/>'s description</param> | ||
protected Command(IServiceProvider serviceProvider, ILoggerFactory loggerFactory, ISynapseApiClient api, string name, string description) | ||
: base(name, description) | ||
{ | ||
this.ServiceProvider = serviceProvider; | ||
this.Logger = loggerFactory.CreateLogger(this.GetType()); | ||
this.Api = api; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the current <see cref="IServiceProvider"/> | ||
/// </summary> | ||
protected IServiceProvider ServiceProvider { get; } | ||
|
||
/// <summary> | ||
/// Gets the service used to perform logging | ||
/// </summary> | ||
protected ILogger Logger { get; } | ||
|
||
/// <summary> | ||
/// Gets the service used to interact with the remote Synapse API | ||
/// </summary> | ||
protected ISynapseApiClient Api { get; } | ||
|
||
} |
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,56 @@ | ||
using Microsoft.Extensions.Options; | ||
using Synapse.Cli.Configuration; | ||
using Synapse.Cli.Services; | ||
|
||
namespace Synapse.Cli.Commands.Config; | ||
|
||
/// <summary> | ||
/// Represents the <see cref="Command"/> used to configure the CLI to delete the specified API configuration | ||
/// </summary> | ||
internal class DeleteApiCommand | ||
: Command | ||
{ | ||
|
||
/// <summary> | ||
/// Gets the <see cref="UseApiCommand"/>'s name | ||
/// </summary> | ||
public const string CommandName = "delete-api"; | ||
/// <summary> | ||
/// Gets the <see cref="UseApiCommand"/>'s description | ||
/// </summary> | ||
public const string CommandDescription = "Deletes the API configuration with the specified name."; | ||
|
||
/// <inheritdoc/> | ||
public DeleteApiCommand(IServiceProvider serviceProvider, ILoggerFactory loggerFactory, ISynapseApiClient api, IOptionsManager optionsManager, IOptionsMonitor<ApplicationOptions> applicationOptions) | ||
: base(serviceProvider, loggerFactory, api, CommandName, CommandDescription) | ||
{ | ||
this.OptionsManager = optionsManager; | ||
this.ApplicationOptions = applicationOptions; | ||
this.Add(new Argument<string>("name") { Description = "The name of the API configuration to delete." }); | ||
this.Handler = CommandHandler.Create<string>(HandleAsync); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the service used to manage the application's options | ||
/// </summary> | ||
protected IOptionsManager OptionsManager { get; } | ||
|
||
/// <summary> | ||
/// Gets the current <see cref="ApplicationOptions"/> | ||
/// </summary> | ||
protected IOptionsMonitor<ApplicationOptions> ApplicationOptions { get; } | ||
|
||
/// <summary> | ||
/// Handles the <see cref="DeleteApiCommand"/> | ||
/// </summary> | ||
/// <param name="name">The name of the API configuration to use</param> | ||
/// <returns>A new awaitable <see cref="Task"/></returns> | ||
public async Task HandleAsync(string name) | ||
{ | ||
ArgumentException.ThrowIfNullOrWhiteSpace(name); | ||
if (this.ApplicationOptions.CurrentValue.Api.Current == name) throw new NotSupportedException($"Failed to delete the API configuration with name '{name}' because it is the API currently in use."); | ||
if (!this.ApplicationOptions.CurrentValue.Api.Configurations.Remove(name)) throw new NullReferenceException($"Failed to find a configured API with name '{name}'."); | ||
await this.OptionsManager.UpdateOptionsAsync(this.ApplicationOptions.CurrentValue); | ||
} | ||
|
||
} |
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,72 @@ | ||
using Microsoft.Extensions.Options; | ||
using Synapse.Cli.Configuration; | ||
using Synapse.Cli.Services; | ||
using Synapse.Resources; | ||
|
||
namespace Synapse.Cli.Commands.Config; | ||
|
||
/// <summary> | ||
/// Represents the <see cref="Command"/> used to configure the API used by the Synapse CLI | ||
/// </summary> | ||
internal class GetApisCommand | ||
: Command | ||
{ | ||
|
||
/// <summary> | ||
/// Gets the <see cref="GetApisCommand"/>'s name | ||
/// </summary> | ||
public const string CommandName = "get-apis"; | ||
/// <summary> | ||
/// Gets the <see cref="GetApisCommand"/>'s description | ||
/// </summary> | ||
public const string CommandDescription = "Retrieves all configured APIs"; | ||
|
||
/// <inheritdoc/> | ||
public GetApisCommand(IServiceProvider serviceProvider, ILoggerFactory loggerFactory, ISynapseApiClient api, IOptionsManager optionsManager, IOptionsMonitor<ApplicationOptions> applicationOptions) | ||
: base(serviceProvider, loggerFactory, api, CommandName, CommandDescription) | ||
{ | ||
this.OptionsManager = optionsManager; | ||
this.ApplicationOptions = applicationOptions; | ||
this.Handler = CommandHandler.Create(HandleAsync); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the service used to manage the application's options | ||
/// </summary> | ||
protected IOptionsManager OptionsManager { get; } | ||
|
||
/// <summary> | ||
/// Gets the current <see cref="ApplicationOptions"/> | ||
/// </summary> | ||
protected IOptionsMonitor<ApplicationOptions> ApplicationOptions { get; } | ||
|
||
/// <summary> | ||
/// Handles the <see cref="GetApisCommand"/> | ||
/// </summary> | ||
/// <returns>A new awaitable <see cref="Task"/></returns> | ||
public async Task HandleAsync() | ||
{ | ||
var table = new Table(); | ||
table.Border(TableBorder.None); | ||
table.AddColumn("CURRENT"); | ||
table.AddColumn("NAME"); | ||
foreach (var apiConfig in this.ApplicationOptions.CurrentValue.Api.Configurations) | ||
{ | ||
table.AddRow | ||
( | ||
this.ApplicationOptions.CurrentValue.Api.Current == apiConfig.Key || this.ApplicationOptions.CurrentValue.Api.Configurations.Count == 1 ? "*" : string.Empty, | ||
apiConfig.Key | ||
); | ||
} | ||
AnsiConsole.Write(table); | ||
await Task.CompletedTask; | ||
} | ||
|
||
static class CommandOptions | ||
{ | ||
|
||
public static Option<Uri> Server => new(["-s", "--server"], "The address of the API server to use"); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.