-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptions.rs
155 lines (135 loc) · 4.37 KB
/
options.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use clap::{Arg, Command};
use std::path::PathBuf;
use crate::sync::{HttpsUri, RsyncUri};
pub struct Options {
pub source: PathBuf,
pub target: PathBuf,
pub rsync: RsyncUri,
pub https: HttpsUri,
pub clean: bool,
pub max_deltas: usize,
}
impl Options {
pub fn from_strs(
source: &str,
target: &str,
rsync: &str,
https: &str,
clean: bool,
max_deltas: &str,
) -> Result<Self, Error> {
let source = PathBuf::from(source);
let target = PathBuf::from(target);
let rsync =
RsyncUri::base_uri(rsync).map_err(|_| Error::RsyncBaseUri(rsync.to_string()))?;
let https =
HttpsUri::base_uri(https).map_err(|_| Error::HttpsBaseUri(https.to_string()))?;
let max_deltas = max_deltas
.parse::<usize>()
.map_err(|_| Error::CannotParseNumber(max_deltas.to_string()))?;
if !source.is_dir() {
Err(Error::cannot_read(source))
} else if !target.is_dir() {
Err(Error::cannot_read(target))
} else {
Ok(Options {
source,
target,
rsync,
https,
clean,
max_deltas,
})
}
}
pub fn from_args() -> Result<Options, Error> {
let matches = Command::new("rrdpit")
.version(env!("CARGO_PKG_VERSION"))
.about("Dist to RPKI RRDP")
.arg(
Arg::new("source")
.long("source")
.value_name("dir")
.help("source directory")
.required(true),
)
.arg(
Arg::new("target")
.long("target")
.value_name("dir")
.help("target directory")
.required(true),
)
.arg(
Arg::new("rsync")
.long("rsync")
.value_name("uri")
.help("base rsync uri")
.required(true),
)
.arg(
Arg::new("https")
.long("https")
.value_name("uri")
.help("base rrdp uri")
.required(true),
)
.arg(
Arg::new("clean")
.help("Clean up target dir (handle with care!)")
.required(false),
)
.arg(
Arg::new("max_deltas")
.long("max_deltas")
.value_name("number")
.help("Limit the maximum number of deltas kept. Default: 25. Minimum: 1")
.required(false),
)
.get_matches();
let source = matches.get_one::<String>("source").unwrap();
let target = matches.get_one::<String>("target").unwrap();
let rsync = matches.get_one::<String>("rsync").unwrap();
let https = matches.get_one::<String>("https").unwrap();
let max_deltas_default = "25".to_string();
let max_deltas = matches.get_one::<String>("max_deltas").unwrap_or(&max_deltas_default);
let clean = matches.contains_id("clean");
Self::from_strs(source, target, rsync, https, clean, max_deltas)
}
}
//------------ Error ---------------------------------------------------------
#[derive(Debug, Display)]
pub enum Error {
#[display(fmt = "Not a directory: {}", _0)]
CannotRead(String),
#[display(fmt = "Not a directory: {}", _0)]
RsyncBaseUri(String),
#[display(fmt = "Not a directory: {}", _0)]
HttpsBaseUri(String),
#[display(fmt = "Cannot parse number: {}", _0)]
CannotParseNumber(String),
#[display(fmt = "max_deltas must be at least 1")]
MaxDeltasMustBeOneOrHigher,
}
impl Error {
fn cannot_read(source: PathBuf) -> Self {
Error::CannotRead(source.to_string_lossy().to_string())
}
}
//------------ Tests ---------------------------------------------------------
#[cfg(test)]
pub mod tests {
use super::*;
#[test]
fn parse_arguments() {
Options::from_strs(
"./test-resources/source-1",
"./test-work",
"rsync://localhost/repo/",
"https://localhost/repo/",
false,
&"25",
)
.unwrap();
}
}