use std::path::Path;
use anyhow::{Context, Result, bail};
use crate::{
config::Config,
constants::{DB_REPO_URL, OWML_DOCS_URL, WEBSITE_URL},
db::{LocalDatabase, RemoteDatabase},
mods::local::UnsafeLocalMod,
};
pub fn open_shortcut(identifier: &str, conf: &Config, local_db: &LocalDatabase) -> Result<()> {
let target = match identifier {
"db" => DB_REPO_URL,
"website" => WEBSITE_URL,
"owml_docs" => OWML_DOCS_URL,
"owml" => &conf.owml_path,
_ => "",
};
if target.is_empty() {
let path: &str = local_db
.get_mod_unsafe(identifier)
.map(|m| match m {
UnsafeLocalMod::Invalid(m) => &m.mod_path,
UnsafeLocalMod::Valid(m) => &m.mod_path,
})
.with_context(|| format!("Mod {identifier} not found"))?;
opener::open(path)?;
} else {
opener::open(target)?;
}
Ok(())
}
pub fn open_readme(unique_name: &str, db: &RemoteDatabase) -> Result<()> {
let remote_mod = db
.get_mod(unique_name)
.with_context(|| format!("Mod {unique_name} not found"))?;
let slug = &remote_mod.slug;
opener::open(format!("{WEBSITE_URL}/mods/{slug}/"))?;
Ok(())
}
pub fn open_github(unique_name: &str, db: &RemoteDatabase) -> Result<()> {
let remote_mod = db
.get_mod(unique_name)
.with_context(|| format!("Mod {unique_name} not found"))?;
let repo = &remote_mod.repo; opener::open(repo)?;
Ok(())
}
pub fn open_owml_logs(config: &Config) -> Result<()> {
let path = Path::new(&config.owml_path).join("Logs");
if path.exists() {
opener::open(path).context("Failed to open OWML logs folder")
} else {
bail!("No Logs from OWML yet");
}
}