2019-02-14 10:11:39 +00:00
|
|
|
use failure::*;
|
|
|
|
|
2019-02-17 09:16:33 +00:00
|
|
|
use crate::api_schema::*;
|
2019-02-14 10:11:39 +00:00
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use regex::Regex;
|
2019-03-13 08:57:36 +00:00
|
|
|
use std::fmt;
|
2019-02-14 10:11:39 +00:00
|
|
|
|
|
|
|
lazy_static! {
|
2019-02-14 10:17:08 +00:00
|
|
|
/// Regular expression to parse repository URLs
|
2019-02-28 14:10:47 +00:00
|
|
|
pub static ref BACKUP_REPO_URL_REGEX: Regex =
|
|
|
|
Regex::new(r"^(?:(?:([\w@]+)@)?([\w\-_.]+):)?(\w+)$").unwrap();
|
2019-02-14 10:11:39 +00:00
|
|
|
|
2019-02-14 10:17:08 +00:00
|
|
|
/// API schema format definition for repository URLs
|
2019-02-14 10:11:39 +00:00
|
|
|
pub static ref BACKUP_REPO_URL: Arc<ApiStringFormat> =
|
|
|
|
ApiStringFormat::Pattern(&BACKUP_REPO_URL_REGEX).into();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reference remote backup locations
|
|
|
|
///
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct BackupRepository {
|
|
|
|
/// The user name used for Authentication
|
2019-03-13 08:47:12 +00:00
|
|
|
user: Option<String>,
|
2019-02-14 10:11:39 +00:00
|
|
|
/// The host name or IP address
|
2019-03-13 08:47:12 +00:00
|
|
|
host: Option<String>,
|
2019-02-14 10:11:39 +00:00
|
|
|
/// The name of the datastore
|
2019-03-13 08:47:12 +00:00
|
|
|
store: String,
|
2019-02-14 10:11:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BackupRepository {
|
|
|
|
|
2019-02-14 10:17:08 +00:00
|
|
|
/// Parse a repository URL.
|
2019-02-14 10:11:39 +00:00
|
|
|
///
|
|
|
|
/// This parses strings like `user@host:datastore`. The `user` and
|
|
|
|
/// `host` parts are optional, where `host` defaults to the local
|
|
|
|
/// host, and `user` defaults to `root@pam`.
|
|
|
|
pub fn parse(url: &str) -> Result<Self, Error> {
|
|
|
|
|
|
|
|
let cap = BACKUP_REPO_URL_REGEX.captures(url)
|
2019-03-02 15:20:50 +00:00
|
|
|
.ok_or_else(|| format_err!("unable to parse repository url '{}'", url))?;
|
2019-02-14 10:11:39 +00:00
|
|
|
|
2019-03-13 08:47:12 +00:00
|
|
|
|
2019-02-14 10:11:39 +00:00
|
|
|
Ok(BackupRepository {
|
2019-03-13 08:47:12 +00:00
|
|
|
//user: cap.get(1).map_or("root@pam", |m| m.as_str()).to_owned(),
|
|
|
|
//host: cap.get(2).map_or("localhost", |m| m.as_str()).to_owned(),
|
|
|
|
user: cap.get(1).map(|m| m.as_str().to_owned()),
|
|
|
|
host: cap.get(2).map(|m| m.as_str().to_owned()),
|
2019-02-14 10:11:39 +00:00
|
|
|
store: cap[3].to_owned(),
|
|
|
|
})
|
|
|
|
}
|
2019-03-13 08:47:12 +00:00
|
|
|
|
|
|
|
pub fn user(&self) -> &str {
|
|
|
|
if let Some(ref user) = self.user {
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
"root@pam"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn host(&self) -> &str {
|
|
|
|
if let Some(ref host) = self.host {
|
|
|
|
return host;
|
|
|
|
}
|
|
|
|
"localhost"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn store(&self) -> &str {
|
|
|
|
&self.store
|
|
|
|
}
|
2019-03-13 08:57:36 +00:00
|
|
|
}
|
2019-03-13 08:47:12 +00:00
|
|
|
|
2019-03-13 08:57:36 +00:00
|
|
|
impl fmt::Display for BackupRepository {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
if let Some(ref user) = self.user {
|
|
|
|
write!(f, "{}@{}:{}", user, self.host(), self.store)
|
|
|
|
} else if let Some(ref host) = self.host {
|
|
|
|
write!(f, "{}:{}", host, self.store)
|
|
|
|
} else {
|
|
|
|
write!(f, "{}", self.store)
|
|
|
|
}
|
2019-03-13 08:47:12 +00:00
|
|
|
}
|
2019-02-14 10:11:39 +00:00
|
|
|
}
|