Skip to content

Commit

Permalink
make mirrors and config work
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv committed Mar 10, 2024
1 parent 062af5c commit 7b328da
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
76 changes: 68 additions & 8 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use miette::{Context, IntoDiagnostic};
use rattler_networking::mirror_middleware::Mirror;
use serde::Deserialize;
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

use miette::{Context, IntoDiagnostic};
use serde::Deserialize;
use url::Url;

use crate::consts;

Expand Down Expand Up @@ -51,16 +52,68 @@ pub fn get_cache_dir() -> miette::Result<PathBuf> {
})
}

#[derive(Clone, Debug, Deserialize)]
#[serde(untagged)]
pub enum MirrorUrlOrConfig {
Url(Url),
Config {
url: Url,
no_bz2: bool,
no_zstd: bool,
no_jlap: bool,
},
}

impl MirrorUrlOrConfig {
pub fn url(&self) -> &Url {
match self {
MirrorUrlOrConfig::Url(url) => url,
MirrorUrlOrConfig::Config { url, .. } => url,
}
}
}

impl From<MirrorUrlOrConfig> for Mirror {
fn from(value: MirrorUrlOrConfig) -> Self {
let mut url = value.url().clone();
url.set_path(&format!("{}/", url.path().trim_end_matches('/')));

match value {
MirrorUrlOrConfig::Url(_) => Mirror {
url,
no_bz2: false,
no_zstd: false,
no_jlap: false,
max_failures: Some(3),
},
MirrorUrlOrConfig::Config {
url: _,
no_bz2,
no_zstd,
no_jlap,
} => Mirror {
url,
no_bz2,
no_zstd,
no_jlap,
max_failures: Some(3),
},
}
}
}

#[derive(Clone, Debug, Deserialize)]
pub struct Channel {
pub name: String,
pub url: String,
pub mirrors: Vec<String>,
pub mirrors: Vec<MirrorUrlOrConfig>,
}

#[derive(Clone, Default, Debug, Deserialize)]
pub struct Config {
#[serde(default)]
pub default_channels: Vec<String>,
#[serde(default)]
pub channels: Vec<Channel>,
}

Expand All @@ -78,10 +131,14 @@ impl Config {
.and_then(|p| fs::read_to_string(p).ok());

if let Some(global_config) = global_config {
let config = Config::from_toml(&global_config);
if let Ok(config) = config {
if let Ok(config) = Config::from_toml(&global_config) {
return config;
}
eprintln!(
"Could not load global config (invalid toml): ~/{}/{}",
consts::PIXI_DIR,
consts::CONFIG_FILE
);
}

Config::default()
Expand Down Expand Up @@ -111,10 +168,13 @@ impl Config {
self.channels.extend(other.channels.iter().cloned());
}

pub fn mirror_map(&self) -> HashMap<String, Vec<String>> {
pub fn mirror_map(&self) -> HashMap<Url, Vec<Mirror>> {
let mut mirror_map = HashMap::new();
for channel in &self.channels {
mirror_map.insert(channel.url.clone(), channel.mirrors.clone());
mirror_map.insert(
channel.url.parse().unwrap(),
channel.mirrors.iter().cloned().map(Mirror::from).collect(),
);
}
mirror_map
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn mirror_middleware(config: &Config) -> MirrorMiddleware {
}

fn oci_middleware() -> OciMiddleware {
OciMiddleware::default()
OciMiddleware
}

pub(crate) fn build_reqwest_clients(config: Option<&Config>) -> (Client, ClientWithMiddleware) {
Expand Down

0 comments on commit 7b328da

Please sign in to comment.