Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/CredentialManager/CredentialManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace GitCredentialManager;

public interface ICredentialManager : ICredentialStore
{
ICommandContext Context { get; }
}

/// <summary>
/// Provides the factory method <see cref="Create"/> to instantiate a
/// <see cref="ICredentialStore"/> appropriate for the current platform and
Expand All @@ -21,11 +26,21 @@ public static class CredentialManager
/// </summary>
/// <param name="namespace">Optional namespace to scope credential operations.</param>
/// <returns>The <see cref="ICredentialStore"/>.</returns>
public static ICredentialStore Create(string? @namespace = default)
public static ICredentialManager Create(string? @namespace = default)
{
// The context already does the check for the platform and configured store to initialize.
// By overriding the settings with our wrapper, we ensure just the namespace is overriden.
return new CredentialStore(new CommandContextWrapper(new CommandContext(), @namespace));
var context = new CommandContextWrapper(new CommandContext(), @namespace);
return new CredentialManagerStore(new CredentialStore(context), context);
}

class CredentialManagerStore(ICredentialStore store, ICommandContext context) : ICredentialManager
{
public ICommandContext Context => context;
public void AddOrUpdate(string service, string account, string secret) => store.AddOrUpdate(service, account, secret);
public ICredential Get(string service, string account) => store.Get(service, account);
public IList<string> GetAccounts(string service) => store.GetAccounts(service);
public bool Remove(string service, string account) => store.Remove(service, account);
}

class CommandContextWrapper(CommandContext context, string? @namespace) : ICommandContext
Expand Down
2 changes: 2 additions & 0 deletions src/Tests/EndToEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ void Run()
{
var store = CredentialManager.Create(Guid.NewGuid().ToString("N"));

Assert.NotNull(store.Context);

var usr = Guid.NewGuid().ToString("N");
var pwd = Guid.NewGuid().ToString("N");

Expand Down
Loading