refactored actions to own files
This commit is contained in:
42
src/actions/download.rs
Normal file
42
src/actions/download.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use anyhow::anyhow;
|
||||
use log::error;
|
||||
|
||||
use crate::{
|
||||
nexus::{NXMUrl, download_nxm},
|
||||
types::RootConfig,
|
||||
unpacker::unpack,
|
||||
};
|
||||
|
||||
/// Handles a nexus mod url. Downloads, unpacks and adds the mod to the config
|
||||
pub fn handle_nxm(root_config: &mut RootConfig, raw_url: &str) -> anyhow::Result<()> {
|
||||
let Some(dl_location) = root_config.download_location() else {
|
||||
return Err(anyhow!("No download location set"));
|
||||
};
|
||||
|
||||
let Some(api_key) = root_config.nexus_api_key() else {
|
||||
return Err(anyhow!("No API key provided"));
|
||||
};
|
||||
|
||||
let Some(nxm_url) = NXMUrl::parse_url(raw_url) else {
|
||||
return Err(anyhow!("Failed to parse URL"));
|
||||
};
|
||||
|
||||
let (dl_file, mod_info) = download_nxm(api_key, &nxm_url, dl_location)?;
|
||||
|
||||
let mod_id = format!("{}-{}", mod_info.generate_id(), nxm_url.file);
|
||||
if root_config.game_by_id(&mod_id).is_some() {
|
||||
error!(
|
||||
"Generated mod id already exists. Pleas install downloaded mod manually. Downloaded at {}",
|
||||
&dl_file.to_string_lossy()
|
||||
);
|
||||
return Err(anyhow!("Mod with generated id already exists"));
|
||||
}
|
||||
|
||||
let new_mod = unpack(root_config, &mod_id, dl_file)?;
|
||||
|
||||
root_config.add_mod(&new_mod);
|
||||
|
||||
root_config.save_to_file()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user