Skip to content

Commit 213de7a

Browse files
committed
Removes unnecessary ICredentialManager interface
Go back to original implementation, and instead introduce a public way of creating the context. Now the context is the one that does all the work, since the store can be retrieved directly from it.
1 parent 78c647b commit 213de7a

File tree

2 files changed

+39
-35
lines changed

2 files changed

+39
-35
lines changed
Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
using System.Collections.Generic;
2-
3-
namespace GitCredentialManager;
4-
5-
public interface ICredentialManager : ICredentialStore
6-
{
7-
ICommandContext Context { get; }
8-
}
1+
namespace GitCredentialManager;
92

103
/// <summary>
114
/// Provides the factory method <see cref="Create"/> to instantiate a
@@ -24,20 +17,13 @@ public static class CredentialManager
2417
/// </summary>
2518
/// <param name="namespace">Optional namespace to scope credential operations.</param>
2619
/// <returns>The <see cref="ICredentialStore"/>.</returns>
27-
public static ICredentialManager Create(string? @namespace = default)
28-
{
29-
// The context already does the check for the platform and configured store to initialize.
30-
// By overriding the settings with our wrapper, we ensure just the namespace is overriden.
31-
var context = new CommandContextWrapper(new CommandContext(), @namespace);
32-
return new CredentialManagerStore(new CredentialStore(context), context);
33-
}
20+
public static ICredentialStore Create(string? @namespace = default)
21+
=> CreateContext(@namespace).CredentialStore;
3422

35-
class CredentialManagerStore(ICredentialStore store, ICommandContext context) : ICredentialManager
36-
{
37-
public ICommandContext Context => context;
38-
public void AddOrUpdate(string service, string account, string secret) => store.AddOrUpdate(service, account, secret);
39-
public ICredential Get(string service, string account) => store.Get(service, account);
40-
public IList<string> GetAccounts(string service) => store.GetAccounts(service);
41-
public bool Remove(string service, string account) => store.Remove(service, account);
42-
}
23+
/// <summary>
24+
/// Creates a new <see cref="ICommandContext"/> that can be used for GCM operations
25+
/// without depending on a git installation.
26+
/// </summary>
27+
public static ICommandContext CreateContext(string? @namespace = default)
28+
=> new CommandContextAdapter(new CommandContext(), @namespace);
4329
}

src/CredentialManager/NoGitWrappers.cs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,36 @@ namespace GitCredentialManager;
1313
/// A wrapper for <see cref="ICommandContext"/> that overrides the namespace for credentials and also
1414
/// allows git-less usage except for the git cache store.
1515
/// </summary>
16-
class CommandContextWrapper(CommandContext context, string? @namespace) : ICommandContext
16+
class CommandContextAdapter : ICommandContext
1717
{
18-
readonly ISettings settings = new SettingsWrapper(
19-
context.Settings is WindowsSettings ?
20-
new NoGitWindowsSettings(context.Environment, context.Git, context.Trace) :
21-
new NoGitSettings(context.Environment, context.Git), @namespace);
18+
readonly CommandContext context;
19+
readonly ICredentialStore store;
20+
readonly ISettings settings;
21+
readonly IHttpClientFactory clientFactory;
22+
23+
public CommandContextAdapter(CommandContext context, string? @namespace = default)
24+
{
25+
this.context = context;
26+
27+
store = new CredentialStore(this);
28+
29+
settings = new SettingsAdapter(
30+
context.Settings is WindowsSettings ?
31+
new NoGitWindowsSettings(context.Environment, context.Git, context.Trace) :
32+
new NoGitSettings(context.Environment, context.Git), @namespace);
33+
34+
clientFactory = new HttpClientFactory(
35+
context.FileSystem, context.Trace, context.Trace2, settings, context.Streams);
36+
}
2237

2338
public ISettings Settings => settings;
2439

40+
public ICredentialStore CredentialStore => store;
41+
42+
public IHttpClientFactory HttpClientFactory => clientFactory;
43+
44+
public IGit Git => new NoGit(context.Git);
45+
2546
#region pass-through impl.
2647

2748
public string ApplicationPath { get => ((ICommandContext)context).ApplicationPath; set => ((ICommandContext)context).ApplicationPath = value; }
@@ -40,12 +61,6 @@ context.Settings is WindowsSettings ?
4061

4162
public IFileSystem FileSystem => ((ICommandContext)context).FileSystem;
4263

43-
public ICredentialStore CredentialStore => ((ICommandContext)context).CredentialStore;
44-
45-
public IHttpClientFactory HttpClientFactory => ((ICommandContext)context).HttpClientFactory;
46-
47-
public IGit Git => new NoGit(context.Git);
48-
4964
public IEnvironment Environment => ((ICommandContext)context).Environment;
5065

5166
public IProcessManager ProcessManager => ((ICommandContext)context).ProcessManager;
@@ -137,11 +152,14 @@ public bool TryGet(GitConfigurationLevel level, GitConfigurationType type, strin
137152
}
138153
}
139154

155+
/// <summary>Adapts <see cref="Settings"/> to use <see cref="NoGit"/>.</summary>
140156
class NoGitSettings(IEnvironment environment, IGit git) : Settings(environment, new NoGit(git)) { }
141157

158+
/// <summary>Adapts <see cref="WindowsSettings"/> to use <see cref="NoGit"/>.</summary>
142159
class NoGitWindowsSettings(IEnvironment environment, IGit git, ITrace trace) : WindowsSettings(environment, new NoGit(git), trace) { }
143160

144-
class SettingsWrapper(ISettings settings, string? @namespace) : ISettings
161+
/// <summary>Allows overriding the credential namespace.</summary>
162+
class SettingsAdapter(ISettings settings, string? @namespace) : ISettings
145163
{
146164
public string CredentialNamespace => @namespace ?? settings.CredentialNamespace;
147165

0 commit comments

Comments
 (0)