Skip to content

Commit

Permalink
cmd: add ability for providers to expose custom cmds
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mjcheetham committed Feb 5, 2021
1 parent d058996 commit 5f1c108
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/shared/Microsoft.Git.CredentialManager/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class Application : ApplicationBase, IConfigurableComponent
private readonly string _appPath;
private readonly IHostProviderRegistry _providerRegistry;
private readonly IConfigurationService _configurationService;
private readonly IList<ProviderCommand> _providerCommands = new List<ProviderCommand>();

public Application(ICommandContext context, string appPath)
: this(context, new HostProviderRegistry(context), new ConfigurationService(context), appPath)
Expand Down Expand Up @@ -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<int> RunInternalAsync(string[] args)
Expand All @@ -62,6 +70,11 @@ protected override async Task<int> 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)}'");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.CommandLine;

namespace Microsoft.Git.CredentialManager.Commands
{
public interface ICommandProvider
{
/// <summary>
/// Create a custom provider command.
/// </summary>
ProviderCommand CreateCommand();
}

public class ProviderCommand : Command
{
public ProviderCommand(IHostProvider provider)
: base(provider.Id, $"Commands for interacting with the {provider.Name} host provider")
{
}
}
}

0 comments on commit 5f1c108

Please sign in to comment.