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
use clap::Parser;
use url::Url;
pub const DEFAULT_PREFIX: &str = "oas";
#[derive(Parser, Debug, Clone)]
pub struct Config {
#[clap(long, env = "ELASTICSEARCH_URL")]
pub url: Option<String>,
#[clap(long, env = "ELASTICSEARCH_PREFIX")]
pub prefix: Option<String>,
}
impl Default for Config {
fn default() -> Self {
Self {
url: None,
prefix: None,
}
}
}
impl Config {
pub fn new(url: Option<String>) -> Self {
Self {
url,
prefix: None,
}
}
pub fn from_url_or_default(url: Option<&str>) -> anyhow::Result<Self> {
if let Some(url) = url {
Self::from_url(url)
} else {
Ok(Self::default())
}
}
pub fn from_url(url: &str) -> anyhow::Result<Self> {
let mut url: Url = url.parse()?;
let first_segment = url
.path_segments()
.map(|mut segments| segments.next().map(|s| s.to_string()))
.flatten();
let prefix = if let Some(first_segment) = first_segment {
first_segment
} else {
DEFAULT_PREFIX.to_string()
};
url.set_path("");
Ok(Self {
url: Some(url.to_string()),
prefix: Some(prefix),
})
}
pub fn with_default_url(prefix: String) -> Self {
Self {
url: None,
prefix: Some(prefix),
}
}
}