public void TestSshConfigFiles() { var configFilename = Path.GetTempFileName(); var config = new SshConfigFile(configFilename); Assert.IsTrue(config.Add("Host *", "test 5")); Assert.IsTrue(config.Save()); Assert.AreEqual(@"Host * test 5 ", File.ReadAllText(configFilename)); Assert.IsFalse(config.Add("Host *", "test 5")); Assert.IsFalse(config.Save()); }
/// <summary> /// Update the ssh config file to define the key to used to clone the repository /// </summary> /// <returns></returns> private bool AddSshKey(string hostname) { // https://medium.com/@xiaolishen/use-multiple-ssh-keys-for-different-github-accounts-on-the-same-computer-7d7103ca8693 var sshConfigFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".ssh", "config"); var config = new SshConfigFile(sshConfigFile); var substituteHostname = SubstituteHostname(hostname); // define a substitute hostname to define an ssh private key for each repository. Even if there are several repositories on a same hotname. config.Add($"Host {substituteHostname}", "StrictHostKeyChecking no"); config.Add($"Host {substituteHostname}", $"HostName {hostname}"); // The real hostname config.Add($"Host {substituteHostname}", $"User git"); // always git user, for git repositories config.Add($"Host {substituteHostname}", $"IdentityFile ~/.ssh/{SSHKeyFilename}"); return(config.Save()); }