Compare commits

..
4 changed files with 33 additions and 8 deletions

No files matched your search

+7 -1
View File
@@ -37,7 +37,13 @@ pub struct Manager {
pub env: &'static [(&'static str, &'static str)],
pub args: &'static [Args],
/// Where the manager records registry URLs, which then carry our http:// override.
pub lockfile: Option<&'static str>,
pub lockfile: Option<Lockfile>,
}
/// What to do after a run about http:// registry URLs left in the lockfile at this path.
pub enum Lockfile {
Restore(&'static str),
Warn(&'static str),
}
pub static MANAGERS: &[&Manager] =
+2 -2
View File
@@ -1,4 +1,4 @@
use super::{Manager, PROXY};
use super::{Lockfile, Manager, PROXY};
pub static NPM: Manager = Manager {
command: "npm",
@@ -8,5 +8,5 @@ pub static NPM: Manager = Manager {
("npm_config_registry", "http://registry.npmjs.org/"),
],
args: &[],
lockfile: Some("package-lock.json"),
lockfile: Some(Lockfile::Restore("package-lock.json")),
};
+2 -2
View File
@@ -1,8 +1,8 @@
use super::{Manager, PROXY, PYPI_SIMPLE};
use super::{Lockfile, Manager, PROXY, PYPI_SIMPLE};
pub static UV: Manager = Manager {
command: "uv",
env: &[("HTTP_PROXY", PROXY), ("http_proxy", PROXY), ("UV_DEFAULT_INDEX", PYPI_SIMPLE)],
args: &[],
lockfile: Some("uv.lock"),
lockfile: Some(Lockfile::Warn("uv.lock")),
};
+22 -3
View File
@@ -10,7 +10,7 @@ use crate::policies::registries::BUILT_IN;
use std::io::{Error, ErrorKind};
use std::path::{Path, PathBuf};
use std::process::Command;
use tracing::warn;
use tracing::{info, warn};
pub struct Invocation {
pub program: String,
@@ -54,8 +54,10 @@ pub fn run(port: u16, invocation: &Invocation, dir: Option<&Path>) -> std::io::R
let status = status.map_err(|error| windows::explain(error, &program));
let code = status?.code().unwrap_or(1);
let manager = MANAGERS.iter().find(|manager| manager.command == invocation.program);
if let Some(lockfile) = manager.and_then(|manager| manager.lockfile) {
warn_if_plaintext(lockfile, dir);
match manager.and_then(|manager| manager.lockfile.as_ref()) {
Some(Lockfile::Restore(lockfile)) => restore_https(lockfile, dir),
Some(Lockfile::Warn(lockfile)) => warn_if_plaintext(lockfile, dir),
None => {}
}
Ok(code)
}
@@ -72,3 +74,20 @@ fn warn_if_plaintext(lockfile: &str, dir: Option<&Path>) {
warn!("{lockfile} has http:// registry URLs; change them to https:// before committing");
}
}
fn restore_https(lockfile: &str, dir: Option<&Path>) {
let path = dir.unwrap_or(Path::new(".")).join(lockfile);
let Ok(text) = std::fs::read_to_string(&path) else { return };
let mut fixed = text.clone();
for registry in BUILT_IN {
let host = registry.host();
fixed = fixed.replace(&format!("http://{host}/"), &format!("https://{host}/"));
}
if fixed == text {
return;
}
match std::fs::write(&path, fixed) {
Ok(()) => info!("changed {lockfile}'s registry URLs back to https://"),
Err(e) => warn!("could not change {lockfile}'s registry URLs back to https://: {e}"),
}
}