src/client/backup_repo.rs: implement FromStr trait
This commit is contained in:
parent
874acb7039
commit
edd3c8c605
@ -190,7 +190,7 @@ fn list_backups(
|
|||||||
) -> Result<Value, Error> {
|
) -> Result<Value, Error> {
|
||||||
|
|
||||||
let repo_url = tools::required_string_param(¶m, "repository")?;
|
let repo_url = tools::required_string_param(¶m, "repository")?;
|
||||||
let repo = BackupRepository::parse(repo_url)?;
|
let repo: BackupRepository = repo_url.parse()?;
|
||||||
|
|
||||||
let mut client = HttpClient::new(repo.host(), repo.user());
|
let mut client = HttpClient::new(repo.host(), repo.user());
|
||||||
|
|
||||||
@ -231,7 +231,7 @@ fn list_backup_groups(
|
|||||||
) -> Result<Value, Error> {
|
) -> Result<Value, Error> {
|
||||||
|
|
||||||
let repo_url = tools::required_string_param(¶m, "repository")?;
|
let repo_url = tools::required_string_param(¶m, "repository")?;
|
||||||
let repo = BackupRepository::parse(repo_url)?;
|
let repo: BackupRepository = repo_url.parse()?;
|
||||||
|
|
||||||
let mut client = HttpClient::new(repo.host(), repo.user());
|
let mut client = HttpClient::new(repo.host(), repo.user());
|
||||||
|
|
||||||
@ -288,7 +288,7 @@ fn list_snapshots(
|
|||||||
) -> Result<Value, Error> {
|
) -> Result<Value, Error> {
|
||||||
|
|
||||||
let repo_url = tools::required_string_param(¶m, "repository")?;
|
let repo_url = tools::required_string_param(¶m, "repository")?;
|
||||||
let repo = BackupRepository::parse(repo_url)?;
|
let repo: BackupRepository = repo_url.parse()?;
|
||||||
|
|
||||||
let path = tools::required_string_param(¶m, "group")?;
|
let path = tools::required_string_param(¶m, "group")?;
|
||||||
let group = BackupGroup::parse(path)?;
|
let group = BackupGroup::parse(path)?;
|
||||||
@ -336,7 +336,7 @@ fn forget_snapshots(
|
|||||||
) -> Result<Value, Error> {
|
) -> Result<Value, Error> {
|
||||||
|
|
||||||
let repo_url = tools::required_string_param(¶m, "repository")?;
|
let repo_url = tools::required_string_param(¶m, "repository")?;
|
||||||
let repo = BackupRepository::parse(repo_url)?;
|
let repo: BackupRepository = repo_url.parse()?;
|
||||||
|
|
||||||
let path = tools::required_string_param(¶m, "snapshot")?;
|
let path = tools::required_string_param(¶m, "snapshot")?;
|
||||||
let snapshot = BackupDir::parse(path)?;
|
let snapshot = BackupDir::parse(path)?;
|
||||||
@ -365,7 +365,7 @@ fn start_garbage_collection(
|
|||||||
) -> Result<Value, Error> {
|
) -> Result<Value, Error> {
|
||||||
|
|
||||||
let repo_url = tools::required_string_param(¶m, "repository")?;
|
let repo_url = tools::required_string_param(¶m, "repository")?;
|
||||||
let repo = BackupRepository::parse(repo_url)?;
|
let repo: BackupRepository = repo_url.parse()?;
|
||||||
|
|
||||||
let mut client = HttpClient::new(repo.host(), repo.user());
|
let mut client = HttpClient::new(repo.host(), repo.user());
|
||||||
|
|
||||||
@ -396,7 +396,7 @@ fn create_backup(
|
|||||||
|
|
||||||
let backupspec_list = tools::required_array_param(¶m, "backupspec")?;
|
let backupspec_list = tools::required_array_param(¶m, "backupspec")?;
|
||||||
|
|
||||||
let repo = BackupRepository::parse(repo_url)?;
|
let repo: BackupRepository = repo_url.parse()?;
|
||||||
|
|
||||||
let all_file_systems = param["all-file-systems"].as_bool().unwrap_or(false);
|
let all_file_systems = param["all-file-systems"].as_bool().unwrap_or(false);
|
||||||
|
|
||||||
@ -495,7 +495,7 @@ fn restore(
|
|||||||
) -> Result<Value, Error> {
|
) -> Result<Value, Error> {
|
||||||
|
|
||||||
let repo_url = tools::required_string_param(¶m, "repository")?;
|
let repo_url = tools::required_string_param(¶m, "repository")?;
|
||||||
let repo = BackupRepository::parse(repo_url)?;
|
let repo: BackupRepository = repo_url.parse()?;
|
||||||
|
|
||||||
let archive_name = tools::required_string_param(¶m, "archive-name")?;
|
let archive_name = tools::required_string_param(¶m, "archive-name")?;
|
||||||
|
|
||||||
@ -566,7 +566,7 @@ fn prune(
|
|||||||
) -> Result<Value, Error> {
|
) -> Result<Value, Error> {
|
||||||
|
|
||||||
let repo_url = tools::required_string_param(¶m, "repository")?;
|
let repo_url = tools::required_string_param(¶m, "repository")?;
|
||||||
let repo = BackupRepository::parse(repo_url)?;
|
let repo: BackupRepository = repo_url.parse()?;
|
||||||
|
|
||||||
let mut client = HttpClient::new(repo.host(), repo.user());
|
let mut client = HttpClient::new(repo.host(), repo.user());
|
||||||
|
|
||||||
|
@ -593,7 +593,7 @@ fn main() {
|
|||||||
// optional previous backup:
|
// optional previous backup:
|
||||||
let previous = args.next().map(|s| s.to_string());
|
let previous = args.next().map(|s| s.to_string());
|
||||||
|
|
||||||
let repo = match BackupRepository::parse(&repo) {
|
let repo: BackupRepository = match repo.parse() {
|
||||||
Ok(repo) => repo,
|
Ok(repo) => repo,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("error parsing repository: {}", e);
|
eprintln!("error parsing repository: {}", e);
|
||||||
|
@ -32,26 +32,6 @@ pub struct BackupRepository {
|
|||||||
|
|
||||||
impl BackupRepository {
|
impl BackupRepository {
|
||||||
|
|
||||||
/// Parse a repository URL.
|
|
||||||
///
|
|
||||||
/// 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)
|
|
||||||
.ok_or_else(|| format_err!("unable to parse repository url '{}'", url))?;
|
|
||||||
|
|
||||||
|
|
||||||
Ok(BackupRepository {
|
|
||||||
//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()),
|
|
||||||
store: cap[3].to_owned(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn user(&self) -> &str {
|
pub fn user(&self) -> &str {
|
||||||
if let Some(ref user) = self.user {
|
if let Some(ref user) = self.user {
|
||||||
return user;
|
return user;
|
||||||
@ -82,3 +62,25 @@ impl fmt::Display for BackupRepository {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl std::str::FromStr for BackupRepository {
|
||||||
|
type Err = Error;
|
||||||
|
|
||||||
|
/// Parse a repository URL.
|
||||||
|
///
|
||||||
|
/// 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`.
|
||||||
|
fn from_str(url: &str) -> Result<Self, Self::Err> {
|
||||||
|
|
||||||
|
let cap = BACKUP_REPO_URL_REGEX.captures(url)
|
||||||
|
.ok_or_else(|| format_err!("unable to parse repository url '{}'", url))?;
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
user: cap.get(1).map(|m| m.as_str().to_owned()),
|
||||||
|
host: cap.get(2).map(|m| m.as_str().to_owned()),
|
||||||
|
store: cap[3].to_owned(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user