Skip to content

Commit

Permalink
[fs] fix clone to support configurable ssh params
Browse files Browse the repository at this point in the history
Signed-off-by: Colton J. McCurdy <[email protected]>
  • Loading branch information
mccurdyc committed Jun 11, 2023
1 parent ccf5ecc commit a911806
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ against repos.

## Usage

Environment variables

- `SSH_PRIVKAY_PATH` - (default: `$HOME/.ssh/id_rsa`). Path to your SSH private
key if it is not in the default location.
- `SSH_PRIVKAY_PASS` - (default: `""`). SSH private key passphrase if it is not
the default.

Global arguments

- `--root <path>` - specify `$GITRS_ROOT`. Defaults to `$HOME/src`.
Expand Down
38 changes: 27 additions & 11 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::repo;
use anyhow::{anyhow, Result};
use git2::{Cred, RemoteCallbacks};
use home;
use log::debug;
use log::{debug, error};
use std::collections::HashMap;
use std::{env, fs, path::Path, path::PathBuf};
use walkdir::WalkDir;
Expand Down Expand Up @@ -47,20 +47,36 @@ pub fn sync(root: PathBuf, repos: &HashMap<String, repo::Repo>, _clean_only: &bo
}

// https://docs.rs/git2/latest/git2/build/struct.RepoBuilder.html
// TODO: make the SSH params configurable.
fn clone_ssh(url: &str, dst: &Path) -> Result<()> {
let mut callbacks = RemoteCallbacks::new();
// TODO: fix this
callbacks.credentials(|_url, username_from_url, _allowed_types| {

callbacks.credentials(|_url, username, _allowed_types| {
let mut ssh_privkey = PathBuf::new();
let mut ssh_privkey_pass = String::from("");

// default
if let Some(h) = home::home_dir() {
ssh_privkey = h.join(".ssh/id_rsa");
}

// default
if let Ok(pw) = env::var("SSH_PRIVKEY_PASS") {
ssh_privkey_pass = pw;
}

if !ssh_privkey.exists() {
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"));
if !ssh_privkey.exists() {
error!("$SSH_PRIVKEY_PATH doesn't exists");
}
}

// https://libgit2.org/libgit2/#HEAD/group/credential/git_credential_ssh_key_from_agent
Cred::ssh_key(
username_from_url.unwrap(),
Some(Path::new(&format!(
"{}/.ssh/fastly_rsa.pub",
env::var("HOME").unwrap()
))),
Path::new(&format!("{}/.ssh/fastly_rsa", env::var("HOME").unwrap())),
Some(env::var("SSH_PASS").unwrap().as_str()),
username.unwrap(),
None,
&ssh_privkey.as_path(),
Some(ssh_privkey_pass.as_str()),
)
});

Expand Down

0 comments on commit a911806

Please sign in to comment.