use crate::backend::SelfHosted;
use crate::config::{Backend, Registry};
use crate::error::{Error, Result};
use crate::progress::Progress;
use crate::transfer::{BackendStatus, Transfer};
use crate::transfer::{Fetched, GetRequest, Share, ShareInfo, ShareRequest};
use std::path::PathBuf;
pub fn resolve(reg: &Registry) -> Result<Box<dyn Transfer>> {
let active = reg.active_backend()?;
match active.kind.as_str() {
"self-hosted" => Ok(Box::new(SelfHosted::from_backend(active)?)),
kind => resolve_plugin(kind, active),
}
}
fn resolve_plugin(kind: &str, active: &Backend) -> Result<Box<dyn Transfer>> {
let plugin = plugins_dir()?.join(format!("dove-{kind}"));
if plugin.exists() {
Ok(Box::new(PluginTransfer {
path: plugin,
backend: active.name.clone(),
}))
} else {
Err(Error::Config(format!(
"this backend needs the {kind} plugin — run `dove install {kind}`"
)))
}
}
fn plugins_dir() -> Result<PathBuf> {
if let Ok(p) = std::env::var("DOVE_PLUGINS") {
if !p.is_empty() {
return Ok(PathBuf::from(p));
}
}
if let Ok(x) = std::env::var("XDG_CONFIG_HOME") {
if !x.is_empty() {
return Ok(PathBuf::from(x).join("dove/plugins"));
}
}
let home = std::env::var("HOME").map_err(|_| Error::Config("HOME is not set".into()))?;
Ok(PathBuf::from(home).join(".config/dove/plugins"))
}
#[derive(Debug)]
struct PluginTransfer {
#[allow(dead_code)]
path: PathBuf,
#[allow(dead_code)]
backend: String,
}
impl Transfer for PluginTransfer {
fn share(&self, _req: ShareRequest, _progress: &dyn Progress) -> Result<Share> {
Err(Error::Other("plugin dispatch not yet implemented".into()))
}
fn get(&self, _req: GetRequest, _progress: &dyn Progress) -> Result<Fetched> {
Err(Error::Other("plugin dispatch not yet implemented".into()))
}
fn list(&self) -> Result<Vec<ShareInfo>> {
Err(Error::Other("plugin dispatch not yet implemented".into()))
}
fn revoke(&self, _id: &str) -> Result<()> {
Err(Error::Other("plugin dispatch not yet implemented".into()))
}
fn status(&self) -> Result<BackendStatus> {
Err(Error::Other("plugin dispatch not yet implemented".into()))
}
}