Skip to content

Commit

Permalink
feat: add ability to specify no remote for unfetcheable remote files
Browse files Browse the repository at this point in the history
  • Loading branch information
MrHedmad committed Nov 22, 2024
1 parent 0dcaa52 commit 5f2b144
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 26 deletions.
39 changes: 21 additions & 18 deletions src/commands/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,39 +218,38 @@ pub fn fetch_remote_data(config: KerblamTomlOptions) -> Result<()> {
}

// Send a message reminding the user that there are some unfetcheable remote files
let non_fetcheable: Vec<&RemoteFile> = remote_files
.iter()
.filter_map(|x| {
if x.path.to_str().unwrap() == "_" {
Some(x)
} else {
None
}
})
.collect();
let non_fetcheable: Vec<&RemoteFile> =
remote_files.iter().filter(|x| x.url.is_none()).collect();

if !non_fetcheable.is_empty() {
if non_fetcheable.len() <= 5 {
let names: Vec<&str> = non_fetcheable
.into_iter()
.map(|x| x.to_owned().path.to_str().unwrap())
.collect();
eprintln!(
"There are {} remote files that Kerblam! cannot fetch: {}",
names.len(),
names.join(", ")
);
if names.len() == 1 {
eprintln!(
"❓ There is one remote file that Kerblam! cannot fetch: {}",
names.join(", ")
);
} else {
eprintln!(
"❓ There are {} remote files that Kerblam! cannot fetch: {}",
names.len(),
names.join(", ")
);
}
} else {
eprintln!(
"There are {} remote files that Kerblam! cannot fetch",
"There are {} remote files that Kerblam! cannot fetch",
non_fetcheable.len(),
);
}
}

let remote_files: Vec<RemoteFile> = remote_files
.into_iter()
.filter(|x| x.path.to_str().unwrap() != "_")
.filter(|x| x.url.is_some())
.collect();

log::debug!("Fetching files: {:?}", remote_files);
Expand Down Expand Up @@ -291,7 +290,11 @@ pub fn fetch_remote_data(config: KerblamTomlOptions) -> Result<()> {
continue;
}

if let Err(msg) = fetch_remote_file(&client, file.url, file.path) {
if let Err(msg) = fetch_remote_file(
&client,
file.url.expect("This is expected due to above filtering"),
file.path,
) {
eprintln!("{}", msg);
success = false;
}
Expand Down
41 changes: 33 additions & 8 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct DataOptions {
pub paths: Option<DataPaths>,
// Profiles are like HashMap<profile_name, HashMap<old_file_name, new_file_name>>
pub profiles: Option<HashMap<String, HashMap<PathBuf, PathBuf>>>,
pub remote: Option<HashMap<Url, PathBuf>>,
pub remote: Option<HashMap<String, String>>,
}

#[derive(Debug, Deserialize, Clone)]
Expand Down Expand Up @@ -101,7 +101,7 @@ pub fn parse_kerblam_toml(toml_file: impl AsRef<Path>) -> Result<KerblamTomlOpti

#[derive(Debug)]
pub struct RemoteFile {
pub url: Url,
pub url: Option<Url>,
pub path: PathBuf,
}

Expand All @@ -111,9 +111,29 @@ impl From<RemoteFile> for PathBuf {
}
}

impl From<RemoteFile> for Url {
fn from(val: RemoteFile) -> Self {
val.url
impl TryFrom<RemoteFile> for Url {
fn try_from(value: RemoteFile) -> Result<Self> {
value
.url
.ok_or(anyhow!("Cannot convert to URL - it's missing"))
}

type Error = anyhow::Error;
}

impl RemoteFile {
fn from_string_pair(value: (&str, &str), base_directory: PathBuf) -> Result<Self> {
if value.0 == "_" {
Ok(RemoteFile {
url: None,
path: base_directory.join(value.1),
})
} else {
Ok(RemoteFile {
url: Some(Url::try_from(value.0)?),
path: base_directory.join(value.1),
})
}
}
}

Expand Down Expand Up @@ -262,9 +282,14 @@ impl KerblamTomlOptions {
.and_then(|x| x.remote)
.map(|y| {
y.iter()
.map(|pairs| RemoteFile {
url: pairs.0.clone(),
path: root_data_dir.join(pairs.1),
// TODO: This fails horribly if the URL is not valid
// due to the .unwrap here. Pretty print a nice error?
.map(|(a, b)| {
RemoteFile::from_string_pair(
(a.as_str(), b.as_str()),
self.input_data_dir(),
)
.unwrap()
})
.collect()
})
Expand Down

0 comments on commit 5f2b144

Please sign in to comment.