config now runs error, ssh refactor

This commit is contained in:
Bryson Steck 2025-03-16 16:01:20 -06:00
parent 6d3ff502a9
commit bca1d20e83
Signed by: bryson
SSH key fingerprint: SHA256:XpKABw/nP4z8UVaH+weLaBnEOD86+cVwif+QjuYLGT4
3 changed files with 46 additions and 37 deletions

View file

@ -5,7 +5,9 @@ pub enum ExitCode {
FilesystemError = 3, FilesystemError = 3,
RepositoryError = 4, RepositoryError = 4,
RemoteError = 5, RemoteError = 5,
PushError = 6 PushError = 6,
FetchError = 7,
ConfigError = 8
} }
pub struct ReturnData { pub struct ReturnData {

View file

@ -90,10 +90,12 @@ fn main() -> Result<(), String> {
return Ok(()) return Ok(())
} }
let cfgs = match config::read_config(args.config, &refractr) { let mut cfgs = vec![];
Ok(cfgs) => cfgs, match config::read_config(args.config, &refractr) {
Err(e) => return Err(e) Ok(c) => cfgs = c,
Err(e) => common::error(format!("{}", e), common::ExitCode::ConfigError)
}; };
if refractr.verbose >= 2 { if refractr.verbose >= 2 {
// no need to loop over configs if verbose is not at the correct level // no need to loop over configs if verbose is not at the correct level
for i in &cfgs { for i in &cfgs {
@ -108,5 +110,4 @@ fn main() -> Result<(), String> {
}; };
Ok(()) Ok(())
} }

View file

@ -60,35 +60,33 @@ impl Refractr {
Ok(()) Ok(())
} }
fn fetch(&self, repo: &Repository, branches: &Vec<String>, ssh: Option<&String>) -> Result<(), Error> { fn fetch(&self, repo: &Repository, branches: &Vec<String>, ssh: bool, ssh_key: &String) -> Result<(), Error> {
match ssh { let mut cb = RemoteCallbacks::new();
Some(key) => { let mut fo = FetchOptions::new();
let mut cb = RemoteCallbacks::new(); if ssh {
let mut fo = FetchOptions::new(); let key_string: String = ssh_key.clone();
cb.credentials(move |_,_,_| Cred::ssh_key( cb.credentials(move |_,_,_| Cred::ssh_key(
"git", "git",
None, None,
Path::new(&key), Path::new(&key_string),
None)); None));
cb.certificate_check(|cert, url| { cb.certificate_check(|cert, url| {
let mut sha256 = String::new(); let mut sha256 = String::new();
for i in cert.as_hostkey().unwrap().hash_sha256().unwrap().to_vec() { for i in cert.as_hostkey().unwrap().hash_sha256().unwrap().to_vec() {
sha256.push_str(&hex::encode(i.to_string())); sha256.push_str(&hex::encode(i.to_string()));
} }
common::warning( common::warning(
format!("implicitly trusting unknown host {} with sha256 host key {}", format!("implicitly trusting unknown host {} with sha256 host key {}",
url, url,
hex::encode(cert.as_hostkey().unwrap().hash_sha256().unwrap().to_vec()))); hex::encode(cert.as_hostkey().unwrap().hash_sha256().unwrap().to_vec())));
common::warning( common::warning(
format!("to ignore this error in the future, add this host to your known_hosts file")); format!("to ignore this error in the future, add this host to your known_hosts file"));
Ok(CertificateCheckStatus::CertificateOk) Ok(CertificateCheckStatus::CertificateOk)
}); });
fo.download_tags(git2::AutotagOption::All); }
fo.remote_callbacks(cb); fo.download_tags(git2::AutotagOption::All);
repo.find_remote("origin")?.fetch(&branches, Some(&mut fo), None)?; fo.remote_callbacks(cb);
}, repo.find_remote("origin")?.fetch(&branches, Some(&mut fo), None)?;
None => repo.find_remote("origin")?.fetch(&branches, None, None)?
};
Ok(()) Ok(())
} }
@ -273,12 +271,13 @@ impl Refractr {
}) })
}; };
let ssh = cfg.config.from.starts_with("ssh://");
let mut builder = RepoBuilder::new(); let mut builder = RepoBuilder::new();
let mut cb = RemoteCallbacks::new(); let mut cb = RemoteCallbacks::new();
let mut fo = FetchOptions::new(); let mut fo = FetchOptions::new();
// make initial clone // make initial clone
if cfg.config.from.starts_with("ssh://") { if ssh {
let key_string = cfg.config.git.ssh_identity_file.clone(); let key_string = cfg.config.git.ssh_identity_file.clone();
cb.credentials(move |_,_,_| Cred::ssh_key( cb.credentials(move |_,_,_| Cred::ssh_key(
"git", "git",
@ -311,7 +310,9 @@ impl Refractr {
let repo = match builder.clone(&cfg.config.from, Path::new(&repo_dir)) { let repo = match builder.clone(&cfg.config.from, Path::new(&repo_dir)) {
Ok(repo) => repo, Ok(repo) => repo,
Err(e) => { Err(e) => {
println!("{}", e); if e.code() != ErrorCode::Exists {
common::error(format!("failed to clone repo to {}: {}", repo_dir, e), ExitCode::FilesystemError);
}
common::warning(format!("found existing repo at {}, attempting to use", repo_dir)); common::warning(format!("found existing repo at {}, attempting to use", repo_dir));
match self.fast_forward(&repo_dir, &cfg.config.branches) { match self.fast_forward(&repo_dir, &cfg.config.branches) {
Ok(_) => if let Ok(repo) = Repository::open(Path::new(&repo_dir)) { Ok(_) => if let Ok(repo) = Repository::open(Path::new(&repo_dir)) {
@ -331,7 +332,12 @@ impl Refractr {
}; };
self.set_up_refs(&repo, &cfg.config.branches).unwrap(); self.set_up_refs(&repo, &cfg.config.branches).unwrap();
self.fetch(&repo, &cfg.config.branches, Some(&cfg.config.git.ssh_identity_file)).unwrap(); if let Err(e) = self.fetch(&repo,
&cfg.config.branches,
ssh,
&cfg.config.git.ssh_identity_file) {
common::error(format!("failed to fetch repo {}: {}", cfg.config.from, e), ExitCode::FetchError);
}
let remotes = match self.make_remotes(&repo, &cfg) { let remotes = match self.make_remotes(&repo, &cfg) {
Ok(v) => v, Ok(v) => v,