-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathfs.rs
220 lines (193 loc) · 6.91 KB
/
fs.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/// mirrord file operations support 2 modes of configuration:
///
/// 1. [`FsUserConfig::Simple`]: controls only the option for enabling read-only, read-write,
/// or disable file operations;
///
/// 2. [`FsUserConfig::Advanced`]: All of the above, plus allows setting up
/// [`mirrord_layer::file::filter::FileFilter`] to control which files should be opened
/// locally or remotely.
use serde::Deserialize;
pub use self::{advanced::*, mode::*};
use crate::{
config::{from_env::FromEnv, source::MirrordConfigSource, ConfigError, MirrordConfig},
util::MirrordToggleableConfig,
};
pub mod advanced;
pub mod mode;
/// Changes file operations behavior based on user configuration.
///
/// Defaults to [`FsUserConfig::Simple`], with [`FsModeConfig::Read`].
#[derive(Deserialize, PartialEq, Eq, Clone, Debug)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(untagged, rename_all = "lowercase")]
pub enum FsUserConfig {
/// Basic configuration that controls the env vars `MIRRORD_FILE_OPS` and
/// `MIRRORD_FILE_RO_OPS` (default).
Simple(FsModeConfig),
/// Allows the user to specify both [`FsModeConfig`] (as above), and configuration for the
/// `MIRRORD_FILE_FILTER_INCLUDE` and `MIRRORD_FILE_FILTER_EXCLUDE` env vars.
Advanced(AdvancedFsUserConfig),
}
impl Default for FsUserConfig {
fn default() -> Self {
FsUserConfig::Simple(FsModeConfig::Read)
}
}
impl MirrordConfig for FsUserConfig {
type Generated = FsConfig;
fn generate_config(self) -> Result<Self::Generated, ConfigError> {
let config = match self {
FsUserConfig::Simple(mode) => FsConfig {
mode: mode.generate_config()?,
include: FromEnv::new("MIRRORD_FILE_FILTER_INCLUDE").source_value(),
exclude: FromEnv::new("MIRRORD_FILE_FILTER_EXCLUDE").source_value(),
},
FsUserConfig::Advanced(advanced) => advanced.generate_config()?,
};
Ok(config)
}
}
impl MirrordToggleableConfig for FsUserConfig {
fn disabled_config() -> Result<Self::Generated, ConfigError> {
let mode = FsModeConfig::disabled_config()?;
let include = FromEnv::new("MIRRORD_FILE_FILTER_INCLUDE").source_value();
let exclude = FromEnv::new("MIRRORD_FILE_FILTER_EXCLUDE").source_value();
Ok(FsConfig {
mode,
include,
exclude,
})
}
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use super::*;
use crate::{
config::MirrordConfig,
util::{testing::with_env_vars, VecOrSingle},
};
#[rstest]
fn test_fs_config_default() {
let expect = FsConfig {
mode: FsModeConfig::Read,
..Default::default()
};
with_env_vars(
vec![
("MIRRORD_FILE_RO_OPS", Some("true")),
("MIRRORD_FILE_OPS", None),
("MIRRORD_FILE_FILTER_INCLUDE", None),
("MIRRORD_FILE_FILTER_EXCLUDE", None),
],
|| {
let fs_config = FsUserConfig::default().generate_config().unwrap();
assert_eq!(fs_config, expect);
},
);
}
#[rstest]
fn test_fs_config_read_only_include() {
let expect = FsConfig {
mode: FsModeConfig::Read,
include: Some(VecOrSingle::Single(".*".to_string())),
exclude: None,
};
with_env_vars(
vec![
("MIRRORD_FILE_RO_OPS", Some("true")),
("MIRRORD_FILE_OPS", None),
("MIRRORD_FILE_FILTER_INCLUDE", Some(".*")),
("MIRRORD_FILE_FILTER_EXCLUDE", None),
],
|| {
let fs_config = FsUserConfig::Advanced(AdvancedFsUserConfig {
mode: FsModeConfig::Read,
include: Some(VecOrSingle::Single(".*".to_string())),
exclude: None,
})
.generate_config()
.unwrap();
assert_eq!(fs_config, expect);
},
);
}
#[rstest]
fn test_fs_config_read_only_exclude() {
let expect = FsConfig {
mode: FsModeConfig::Read,
include: None,
exclude: Some(VecOrSingle::Single(".*".to_string())),
};
with_env_vars(
vec![
("MIRRORD_FILE_RO_OPS", Some("true")),
("MIRRORD_FILE_OPS", None),
("MIRRORD_FILE_FILTER_INCLUDE", None),
("MIRRORD_FILE_FILTER_EXCLUDE", Some(".*")),
],
|| {
let fs_config = FsUserConfig::Advanced(AdvancedFsUserConfig {
mode: FsModeConfig::Read,
include: None,
exclude: Some(VecOrSingle::Single(".*".to_string())),
})
.generate_config()
.unwrap();
assert_eq!(fs_config, expect);
},
);
}
#[rstest]
fn test_fs_config_read_only_include_exclude() {
let expect = FsConfig {
mode: FsModeConfig::Read,
include: Some(VecOrSingle::Single(".*".to_string())),
exclude: Some(VecOrSingle::Single(".*".to_string())),
};
with_env_vars(
vec![
("MIRRORD_FILE_RO_OPS", Some("true")),
("MIRRORD_FILE_OPS", None),
("MIRRORD_FILE_FILTER_INCLUDE", Some(".*")),
("MIRRORD_FILE_FILTER_EXCLUDE", Some(".*")),
],
|| {
let fs_config = FsUserConfig::Advanced(AdvancedFsUserConfig {
mode: FsModeConfig::Read,
include: Some(VecOrSingle::Single(".*".to_string())),
exclude: Some(VecOrSingle::Single(".*".to_string())),
})
.generate_config()
.unwrap();
assert_eq!(fs_config, expect);
},
);
}
#[rstest]
fn test_fs_config_read_write_include_exclude() {
let expect = FsConfig {
mode: FsModeConfig::Write,
include: Some(VecOrSingle::Single(".*".to_string())),
exclude: Some(VecOrSingle::Single(".*".to_string())),
};
with_env_vars(
vec![
("MIRRORD_FILE_RO_OPS", None),
("MIRRORD_FILE_OPS", Some("true")),
("MIRRORD_FILE_FILTER_INCLUDE", Some(".*")),
("MIRRORD_FILE_FILTER_EXCLUDE", Some(".*")),
],
|| {
let fs_config = FsUserConfig::Advanced(AdvancedFsUserConfig {
mode: FsModeConfig::Write,
include: Some(VecOrSingle::Single(".*".to_string())),
exclude: Some(VecOrSingle::Single(".*".to_string())),
})
.generate_config()
.unwrap();
assert_eq!(fs_config, expect);
},
);
}
}