Beispiel #1
0
        public CommandContext()
        {
            Streams = new StandardStreams();
            Trace   = new Trace();

            if (PlatformUtils.IsWindows())
            {
                FileSystem     = new WindowsFileSystem();
                SessionManager = new WindowsSessionManager();
                SystemPrompts  = new WindowsSystemPrompts();
                Environment    = new WindowsEnvironment(FileSystem);
                Terminal       = new WindowsTerminal(Trace);
                string gitPath = GetGitPath(Environment, FileSystem);
                Git = new GitProcess(
                    Trace,
                    gitPath,
                    FileSystem.GetCurrentDirectory()
                    );
                Settings        = new Settings(Environment, Git);
                CredentialStore = new WindowsCredentialManager(Settings.CredentialNamespace);
            }
            else if (PlatformUtils.IsMacOS())
            {
                FileSystem     = new MacOSFileSystem();
                SessionManager = new MacOSSessionManager();
                SystemPrompts  = new MacOSSystemPrompts();
                Environment    = new PosixEnvironment(FileSystem);
                Terminal       = new PosixTerminal(Trace);
                string gitPath = GetGitPath(Environment, FileSystem);
                Git = new GitProcess(
                    Trace,
                    gitPath,
                    FileSystem.GetCurrentDirectory()
                    );
                Settings        = new Settings(Environment, Git);
                CredentialStore = new MacOSKeychain(Settings.CredentialNamespace);
            }
            else if (PlatformUtils.IsLinux())
            {
                FileSystem = new LinuxFileSystem();
                // TODO: support more than just 'Posix' or X11
                SessionManager = new PosixSessionManager();
                SystemPrompts  = new LinuxSystemPrompts();
                Environment    = new PosixEnvironment(FileSystem);
                Terminal       = new PosixTerminal(Trace);
                string gitPath = GetGitPath(Environment, FileSystem);
                Git = new GitProcess(
                    Trace,
                    gitPath,
                    FileSystem.GetCurrentDirectory()
                    );
                Settings = new Settings(Environment, Git);
                IGpg gpg = new Gpg(
                    Environment.LocateExecutable("gpg"),
                    SessionManager
                    );
                CredentialStore = new LinuxCredentialStore(FileSystem, Settings, SessionManager, gpg, Environment);
            }
            else
            {
                throw new PlatformNotSupportedException();
            }

            HttpClientFactory = new HttpClientFactory(Trace, Settings, Streams);

            // Set the parent window handle/ID
            SystemPrompts.ParentWindowId = Settings.ParentWindowId;
        }
        private void EnsureBackingStore()
        {
            if (_backingStore != null)
            {
                return;
            }

            string ns            = _context.Settings.CredentialNamespace;
            string credStoreName = _context.Settings.CredentialBackingStore?.ToLowerInvariant()
                                   ?? GetDefaultStore();

            switch (credStoreName)
            {
            case StoreNames.WindowsCredentialManager:
                ValidateWindowsCredentialManager();
                _backingStore = new WindowsCredentialManager(ns);
                break;

            case StoreNames.Dpapi:
                ValidateDpapi(out string dpapiStoreRoot);
                _backingStore = new DpapiCredentialStore(_context.FileSystem, dpapiStoreRoot, ns);
                break;

            case StoreNames.MacOSKeychain:
                ValidateMacOSKeychain();
                _backingStore = new MacOSKeychain(ns);
                break;

            case StoreNames.SecretService:
                ValidateSecretService();
                _backingStore = new SecretServiceCollection(ns);
                break;

            case StoreNames.Gpg:
                ValidateGpgPass(out string gpgStoreRoot, out string gpgExec);
                IGpg gpg = new Gpg(gpgExec, _context.SessionManager);
                _backingStore = new GpgPassCredentialStore(_context.FileSystem, gpg, gpgStoreRoot, ns);
                break;

            case StoreNames.Cache:
                ValidateCredentialCache(out string options);
                _backingStore = new CredentialCacheStore(_context.Git, options);
                break;

            case StoreNames.Plaintext:
                ValidatePlaintext(out string plainStoreRoot);
                _backingStore = new PlaintextCredentialStore(_context.FileSystem, plainStoreRoot, ns);
                break;

            default:
                var sb = new StringBuilder();
                sb.AppendLine(string.IsNullOrWhiteSpace(credStoreName)
                        ? "No credential store has been selected."
                        : $"Unknown credential store '{credStoreName}'.");
                sb.AppendFormat(
                    "{3}Set the {0} environment variable or the {1}.{2} Git configuration setting to one of the following options:{3}{3}",
                    Constants.EnvironmentVariables.GcmCredentialStore,
                    Constants.GitConfiguration.Credential.SectionName,
                    Constants.GitConfiguration.Credential.CredentialStore,
                    Environment.NewLine);
                AppendAvailableStoreList(sb);
                sb.AppendLine();
                sb.AppendLine($"See {Constants.HelpUrls.GcmCredentialStores} for more information.");
                throw new Exception(sb.ToString());
            }
        }