Skip to content

Commit 721ed77

Browse files
committed
Expose command context from store
This allows using the same mechanism in other contexts (i.e. when using GCM authentication handlers).
1 parent a5829e5 commit 721ed77

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/CredentialManager/CredentialManager.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
namespace GitCredentialManager;
66

7+
public interface ICredentialManager : ICredentialStore
8+
{
9+
ICommandContext Context { get; }
10+
}
11+
712
/// <summary>
813
/// Provides the factory method <see cref="Create"/> to instantiate a
914
/// <see cref="ICredentialStore"/> appropriate for the current platform and
@@ -21,11 +26,21 @@ public static class CredentialManager
2126
/// </summary>
2227
/// <param name="namespace">Optional namespace to scope credential operations.</param>
2328
/// <returns>The <see cref="ICredentialStore"/>.</returns>
24-
public static ICredentialStore Create(string? @namespace = default)
29+
public static ICredentialManager Create(string? @namespace = default)
2530
{
2631
// The context already does the check for the platform and configured store to initialize.
2732
// By overriding the settings with our wrapper, we ensure just the namespace is overriden.
28-
return new CredentialStore(new CommandContextWrapper(new CommandContext(), @namespace));
33+
var context = new CommandContextWrapper(new CommandContext(), @namespace);
34+
return new CredentialManagerStore(new CredentialStore(context), context);
35+
}
36+
37+
class CredentialManagerStore(ICredentialStore store, ICommandContext context) : ICredentialManager
38+
{
39+
public ICommandContext Context => context;
40+
public void AddOrUpdate(string service, string account, string secret) => store.AddOrUpdate(service, account, secret);
41+
public ICredential Get(string service, string account) => store.Get(service, account);
42+
public IList<string> GetAccounts(string service) => store.GetAccounts(service);
43+
public bool Remove(string service, string account) => store.Remove(service, account);
2944
}
3045

3146
class CommandContextWrapper(CommandContext context, string? @namespace) : ICommandContext

src/Tests/EndToEnd.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ void Run()
103103
{
104104
var store = CredentialManager.Create(Guid.NewGuid().ToString("N"));
105105

106+
Assert.NotNull(store.Context);
107+
106108
var usr = Guid.NewGuid().ToString("N");
107109
var pwd = Guid.NewGuid().ToString("N");
108110

0 commit comments

Comments
 (0)