diff --git a/src/CredentialManager/CredentialManager.cs b/src/CredentialManager/CredentialManager.cs index 222071e..24ac5fd 100644 --- a/src/CredentialManager/CredentialManager.cs +++ b/src/CredentialManager/CredentialManager.cs @@ -1,11 +1,4 @@ -using System.Collections.Generic; - -namespace GitCredentialManager; - -public interface ICredentialManager : ICredentialStore -{ - ICommandContext Context { get; } -} +namespace GitCredentialManager; /// /// Provides the factory method to instantiate a @@ -24,20 +17,13 @@ public static class CredentialManager /// /// Optional namespace to scope credential operations. /// The . - 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. - var context = new CommandContextWrapper(new CommandContext(), @namespace); - return new CredentialManagerStore(new CredentialStore(context), context); - } + public static ICredentialStore Create(string? @namespace = default) + => CreateContext(@namespace).CredentialStore; - 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 GetAccounts(string service) => store.GetAccounts(service); - public bool Remove(string service, string account) => store.Remove(service, account); - } + /// + /// Creates a new that can be used for GCM operations + /// without depending on a git installation. + /// + public static ICommandContext CreateContext(string? @namespace = default) + => new CommandContextAdapter(new CommandContext(), @namespace); } diff --git a/src/CredentialManager/NoGitWrappers.cs b/src/CredentialManager/NoGitWrappers.cs index e59c008..79932b8 100644 --- a/src/CredentialManager/NoGitWrappers.cs +++ b/src/CredentialManager/NoGitWrappers.cs @@ -13,15 +13,36 @@ namespace GitCredentialManager; /// A wrapper for that overrides the namespace for credentials and also /// allows git-less usage except for the git cache store. /// -class CommandContextWrapper(CommandContext context, string? @namespace) : ICommandContext +class CommandContextAdapter : ICommandContext { - readonly ISettings settings = new SettingsWrapper( - context.Settings is WindowsSettings ? - new NoGitWindowsSettings(context.Environment, context.Git, context.Trace) : - new NoGitSettings(context.Environment, context.Git), @namespace); + readonly CommandContext context; + readonly ICredentialStore store; + readonly ISettings settings; + readonly IHttpClientFactory clientFactory; + + public CommandContextAdapter(CommandContext context, string? @namespace = default) + { + this.context = context; + + store = new CredentialStore(this); + + settings = new SettingsAdapter( + context.Settings is WindowsSettings ? + new NoGitWindowsSettings(context.Environment, context.Git, context.Trace) : + new NoGitSettings(context.Environment, context.Git), @namespace); + + clientFactory = new HttpClientFactory( + context.FileSystem, context.Trace, context.Trace2, settings, context.Streams); + } public ISettings Settings => settings; + public ICredentialStore CredentialStore => store; + + public IHttpClientFactory HttpClientFactory => clientFactory; + + public IGit Git => new NoGit(context.Git); + #region pass-through impl. public string ApplicationPath { get => ((ICommandContext)context).ApplicationPath; set => ((ICommandContext)context).ApplicationPath = value; } @@ -40,12 +61,6 @@ context.Settings is WindowsSettings ? public IFileSystem FileSystem => ((ICommandContext)context).FileSystem; - public ICredentialStore CredentialStore => ((ICommandContext)context).CredentialStore; - - public IHttpClientFactory HttpClientFactory => ((ICommandContext)context).HttpClientFactory; - - public IGit Git => new NoGit(context.Git); - public IEnvironment Environment => ((ICommandContext)context).Environment; public IProcessManager ProcessManager => ((ICommandContext)context).ProcessManager; @@ -137,11 +152,14 @@ public bool TryGet(GitConfigurationLevel level, GitConfigurationType type, strin } } + /// Adapts to use . class NoGitSettings(IEnvironment environment, IGit git) : Settings(environment, new NoGit(git)) { } + /// Adapts to use . class NoGitWindowsSettings(IEnvironment environment, IGit git, ITrace trace) : WindowsSettings(environment, new NoGit(git), trace) { } - class SettingsWrapper(ISettings settings, string? @namespace) : ISettings + /// Allows overriding the credential namespace. + class SettingsAdapter(ISettings settings, string? @namespace) : ISettings { public string CredentialNamespace => @namespace ?? settings.CredentialNamespace;