Skip to content

Commit a911806

Browse files
committed
[fs] fix clone to support configurable ssh params
Signed-off-by: Colton J. McCurdy <mccurdyc22@gmail.com>
1 parent ccf5ecc commit a911806

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ against repos.
1111

1212
## Usage
1313

14+
Environment variables
15+
16+
- `SSH_PRIVKAY_PATH` - (default: `$HOME/.ssh/id_rsa`). Path to your SSH private
17+
key if it is not in the default location.
18+
- `SSH_PRIVKAY_PASS` - (default: `""`). SSH private key passphrase if it is not
19+
the default.
20+
1421
Global arguments
1522

1623
- `--root <path>` - specify `$GITRS_ROOT`. Defaults to `$HOME/src`.

src/fs.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::repo;
22
use anyhow::{anyhow, Result};
33
use git2::{Cred, RemoteCallbacks};
44
use home;
5-
use log::debug;
5+
use log::{debug, error};
66
use std::collections::HashMap;
77
use std::{env, fs, path::Path, path::PathBuf};
88
use walkdir::WalkDir;
@@ -47,20 +47,36 @@ pub fn sync(root: PathBuf, repos: &HashMap<String, repo::Repo>, _clean_only: &bo
4747
}
4848

4949
// https://docs.rs/git2/latest/git2/build/struct.RepoBuilder.html
50-
// TODO: make the SSH params configurable.
5150
fn clone_ssh(url: &str, dst: &Path) -> Result<()> {
5251
let mut callbacks = RemoteCallbacks::new();
53-
// TODO: fix this
54-
callbacks.credentials(|_url, username_from_url, _allowed_types| {
52+
53+
callbacks.credentials(|_url, username, _allowed_types| {
54+
let mut ssh_privkey = PathBuf::new();
55+
let mut ssh_privkey_pass = String::from("");
56+
57+
// default
58+
if let Some(h) = home::home_dir() {
59+
ssh_privkey = h.join(".ssh/id_rsa");
60+
}
61+
62+
// default
63+
if let Ok(pw) = env::var("SSH_PRIVKEY_PASS") {
64+
ssh_privkey_pass = pw;
65+
}
66+
67+
if !ssh_privkey.exists() {
68+
ssh_privkey = PathBuf::from(env::var("SSH_PRIVKEY_PATH").expect("$HOME/.ssh/id_rsa doesn't exists, you must specify an ssh private key path via SSH_PRIVKEY_PATH"));
69+
if !ssh_privkey.exists() {
70+
error!("$SSH_PRIVKEY_PATH doesn't exists");
71+
}
72+
}
73+
5574
// https://libgit2.org/libgit2/#HEAD/group/credential/git_credential_ssh_key_from_agent
5675
Cred::ssh_key(
57-
username_from_url.unwrap(),
58-
Some(Path::new(&format!(
59-
"{}/.ssh/fastly_rsa.pub",
60-
env::var("HOME").unwrap()
61-
))),
62-
Path::new(&format!("{}/.ssh/fastly_rsa", env::var("HOME").unwrap())),
63-
Some(env::var("SSH_PASS").unwrap().as_str()),
76+
username.unwrap(),
77+
None,
78+
&ssh_privkey.as_path(),
79+
Some(ssh_privkey_pass.as_str()),
6480
)
6581
});
6682

0 commit comments

Comments
 (0)