Skip to content

Commit f6d918a

Browse files
committed
Allow to pass registry ssh key
1 parent 326694e commit f6d918a

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/crates/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ pub struct Crate(CrateType);
2424

2525
impl Crate {
2626
/// Load a crate from specified registry.
27-
pub fn registry(registry_index: &str, name: &str, version: &str) -> Self {
27+
pub fn registry(registry_index: &str, name: &str, version: &str, key: Option<String>) -> Self {
2828
Crate(CrateType::Registry(registry::RegistryCrate::new(
2929
registry::Registry::Alternative(registry::AlternativeRegistry::new(registry_index)),
3030
name,
3131
version,
32+
key,
3233
)))
3334
}
3435

@@ -38,6 +39,7 @@ impl Crate {
3839
registry::Registry::CratesIo,
3940
name,
4041
version,
42+
None,
4143
)))
4244
}
4345

src/crates/registry.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub(super) struct RegistryCrate {
5555
registry: Registry,
5656
name: String,
5757
version: String,
58+
key: Option<String>,
5859
}
5960

6061
#[derive(serde::Deserialize)]
@@ -63,11 +64,12 @@ struct IndexConfig {
6364
}
6465

6566
impl RegistryCrate {
66-
pub(super) fn new(registry: Registry, name: &str, version: &str) -> Self {
67+
pub(super) fn new(registry: Registry, name: &str, version: &str, key: Option<String>) -> Self {
6768
RegistryCrate {
6869
registry,
6970
name: name.into(),
7071
version: version.into(),
72+
key: key.map(Into::into),
7173
}
7274
}
7375

@@ -92,7 +94,27 @@ impl RegistryCrate {
9294
.join(alt.index_folder());
9395
if !index_path.exists() {
9496
let url = alt.index();
95-
git2::Repository::clone(url, index_path.clone())
97+
let mut fo = git2::FetchOptions::new();
98+
if let Some(key) = self.key.as_deref() {
99+
fo.remote_callbacks({
100+
let mut callbacks = git2::RemoteCallbacks::new();
101+
callbacks.credentials(
102+
move |_url, username_from_url, _allowed_types| {
103+
git2::Cred::ssh_key_from_memory(
104+
username_from_url.unwrap(),
105+
None,
106+
key,
107+
None,
108+
)
109+
},
110+
);
111+
callbacks
112+
});
113+
}
114+
115+
git2::build::RepoBuilder::new()
116+
.fetch_options(fo)
117+
.clone(url, &index_path)
96118
.with_context(|_| format!("unable to update_index at {}", url))?;
97119
info!("cloned registry index");
98120
}

0 commit comments

Comments
 (0)