]> git.proxmox.com Git - proxmox-backup.git/blame - src/client/backup_repo.rs
src/bin/proxmox-backup-client.rs: implement repository completions
[proxmox-backup.git] / src / client / backup_repo.rs
CommitLineData
151c6ce2
DM
1use failure::*;
2
ef2f2efb 3use crate::api_schema::*;
151c6ce2
DM
4
5use std::sync::Arc;
6use lazy_static::lazy_static;
7use regex::Regex;
8
9lazy_static! {
db4868ef 10 /// Regular expression to parse repository URLs
df67070b
WB
11 pub static ref BACKUP_REPO_URL_REGEX: Regex =
12 Regex::new(r"^(?:(?:([\w@]+)@)?([\w\-_.]+):)?(\w+)$").unwrap();
151c6ce2 13
db4868ef 14 /// API schema format definition for repository URLs
151c6ce2
DM
15 pub static ref BACKUP_REPO_URL: Arc<ApiStringFormat> =
16 ApiStringFormat::Pattern(&BACKUP_REPO_URL_REGEX).into();
17}
18
19/// Reference remote backup locations
20///
21
22#[derive(Debug)]
23pub struct BackupRepository {
24 /// The user name used for Authentication
d0a03d40 25 user: Option<String>,
151c6ce2 26 /// The host name or IP address
d0a03d40 27 host: Option<String>,
151c6ce2 28 /// The name of the datastore
d0a03d40 29 store: String,
151c6ce2
DM
30}
31
32impl BackupRepository {
33
db4868ef 34 /// Parse a repository URL.
151c6ce2
DM
35 ///
36 /// This parses strings like `user@host:datastore`. The `user` and
37 /// `host` parts are optional, where `host` defaults to the local
38 /// host, and `user` defaults to `root@pam`.
39 pub fn parse(url: &str) -> Result<Self, Error> {
40
41 let cap = BACKUP_REPO_URL_REGEX.captures(url)
184f17af 42 .ok_or_else(|| format_err!("unable to parse repository url '{}'", url))?;
151c6ce2 43
d0a03d40 44
151c6ce2 45 Ok(BackupRepository {
d0a03d40
DM
46 //user: cap.get(1).map_or("root@pam", |m| m.as_str()).to_owned(),
47 //host: cap.get(2).map_or("localhost", |m| m.as_str()).to_owned(),
48 user: cap.get(1).map(|m| m.as_str().to_owned()),
49 host: cap.get(2).map(|m| m.as_str().to_owned()),
151c6ce2
DM
50 store: cap[3].to_owned(),
51 })
52 }
d0a03d40
DM
53
54 pub fn user(&self) -> &str {
55 if let Some(ref user) = self.user {
56 return user;
57 }
58 "root@pam"
59 }
60
61 pub fn host(&self) -> &str {
62 if let Some(ref host) = self.host {
63 return host;
64 }
65 "localhost"
66 }
67
68 pub fn store(&self) -> &str {
69 &self.store
70 }
71
72 pub fn to_string(&self) -> String {
73
74 if let Some(ref user) = self.user {
75 return format!("{}@{}:{}", user, self.host(), self.store);
76 } else if let Some(ref host) = self.host {
77 return format!("{}:{}", host, self.store);
78 }
79 self.store.to_owned()
80 }
81
151c6ce2 82}