From 5f1c1087f3bcae33a64801e0182036ed32498d16 Mon Sep 17 00:00:00 2001 From: Matthew John Cheetham Date: Mon, 1 Feb 2021 13:19:09 +0000 Subject: [PATCH] cmd: add ability for providers to expose custom cmds Add the ability for host providers to register themselves as offering custom commands, under the provider ID name. For example a provider with the ID 'foo' would be able to expose commands under the `git-credential-manager-core foo <..>` command. --- .../Application.cs | 13 ++++++++++++ .../Commands/ProviderCommand.cs | 20 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/shared/Microsoft.Git.CredentialManager/Commands/ProviderCommand.cs diff --git a/src/shared/Microsoft.Git.CredentialManager/Application.cs b/src/shared/Microsoft.Git.CredentialManager/Application.cs index 297cded97..21ac00b23 100644 --- a/src/shared/Microsoft.Git.CredentialManager/Application.cs +++ b/src/shared/Microsoft.Git.CredentialManager/Application.cs @@ -17,6 +17,7 @@ public class Application : ApplicationBase, IConfigurableComponent private readonly string _appPath; private readonly IHostProviderRegistry _providerRegistry; private readonly IConfigurationService _configurationService; + private readonly IList _providerCommands = new List(); public Application(ICommandContext context, string appPath) : this(context, new HostProviderRegistry(context), new ConfigurationService(context), appPath) @@ -49,6 +50,13 @@ public void RegisterProvider(IHostProvider provider, HostProviderPriority priori { _configurationService.AddComponent(configurableProvider); } + + // If the provider has custom commands to offer then create them here + if (provider is ICommandProvider cmdProvider) + { + ProviderCommand providerCommand = cmdProvider.CreateCommand(); + _providerCommands.Add(providerCommand); + } } protected override async Task RunInternalAsync(string[] args) @@ -62,6 +70,11 @@ protected override async Task RunInternalAsync(string[] args) rootCommand.AddCommand(new ConfigureCommand(Context, _configurationService)); rootCommand.AddCommand(new UnconfigureCommand(Context, _configurationService)); + // Add any custom provider commands + foreach (ProviderCommand providerCommand in _providerCommands) + { + rootCommand.AddCommand(providerCommand); + } // Trace the current version and program arguments Context.Trace.WriteLine($"{Constants.GetProgramHeader()} '{string.Join(" ", args)}'"); diff --git a/src/shared/Microsoft.Git.CredentialManager/Commands/ProviderCommand.cs b/src/shared/Microsoft.Git.CredentialManager/Commands/ProviderCommand.cs new file mode 100644 index 000000000..88f8ec105 --- /dev/null +++ b/src/shared/Microsoft.Git.CredentialManager/Commands/ProviderCommand.cs @@ -0,0 +1,20 @@ +using System.CommandLine; + +namespace Microsoft.Git.CredentialManager.Commands +{ + public interface ICommandProvider + { + /// + /// Create a custom provider command. + /// + ProviderCommand CreateCommand(); + } + + public class ProviderCommand : Command + { + public ProviderCommand(IHostProvider provider) + : base(provider.Id, $"Commands for interacting with the {provider.Name} host provider") + { + } + } +}