-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathdeploy.rs
382 lines (328 loc) · 11.5 KB
/
deploy.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
use anyhow::{Context, Result};
use handlebars::Handlebars;
use std::collections::BTreeMap;
use std::io::{self, Read};
use crate::actions::Action;
use crate::args::Options;
use crate::config;
use crate::display_error;
use crate::file_state::{file_state_from_configuration, FileState};
use crate::handlebars_helpers;
use crate::hooks;
/// Returns true if an error was printed
pub fn deploy(opt: &Options) -> Result<bool> {
let mut patch = None;
if opt.patch {
debug!("Reading manual patch from stdin...");
let mut patch_str = String::new();
io::stdin()
.read_to_string(&mut patch_str)
.context("read patch from stdin")?;
patch = Some(toml::from_str(&patch_str).context("parse patch into package")?);
}
trace!("Manual patch: {:#?}", patch);
let config = config::load_configuration(&opt.local_config, &opt.global_config, patch)
.context("get a configuration")?;
let mut cache = if let Some(cache) = config::load_cache(&opt.cache_file)? {
cache
} else {
warn!("Cache file not found. Assuming cache is empty.");
config::Cache::default()
};
let state = file_state_from_configuration(&config, &cache, &opt.cache_directory)
.context("get file state")?;
trace!("File state: {:#?}", state);
let config::Configuration {
files,
mut variables,
helpers,
packages,
} = config;
debug!("Creating Handlebars instance...");
let mut handlebars = Handlebars::new();
handlebars.register_escape_fn(|s| s.to_string()); // Disable html-escaping
handlebars.set_strict_mode(true); // Report missing variables as errors
handlebars_helpers::register_rust_helpers(&mut handlebars);
handlebars_helpers::register_script_helpers(&mut handlebars, &helpers);
handlebars_helpers::add_dotter_variable(&mut variables, &files, &packages);
trace!("Handlebars instance: {:#?}", handlebars);
debug!("Running pre-deploy hook");
if opt.act {
hooks::run_hook(
&opt.pre_deploy,
&opt.cache_directory,
&handlebars,
&variables,
)
.context("run pre-deploy hook")?;
}
let mut suggest_force = false;
let mut error_occurred = false;
let plan = plan_deploy(state);
let mut fs = crate::filesystem::RealFilesystem::new(opt.interactive);
for action in plan {
match action.run(&mut fs, opt, &handlebars, &variables) {
Ok(true) => action.affect_cache(&mut cache),
Ok(false) => {
suggest_force = true;
}
Err(e) => {
error_occurred = true;
display_error(e);
}
}
}
trace!("Actual symlinks: {:#?}", cache.symlinks);
trace!("Actual templates: {:#?}", cache.templates);
if suggest_force {
error!("Some files were skipped. To ignore errors and overwrite unexpected target files, use the --force flag.");
error_occurred = true;
}
if opt.act {
config::save_cache(&opt.cache_file, cache)?;
}
debug!("Running post-deploy hook");
if opt.act {
hooks::run_hook(
&opt.post_deploy,
&opt.cache_directory,
&handlebars,
&variables,
)
.context("run post-deploy hook")?;
}
Ok(error_occurred)
}
pub fn undeploy(opt: Options) -> Result<bool> {
let config = config::load_configuration(&opt.local_config, &opt.global_config, None)
.context("get a configuration")?;
let mut cache = config::load_cache(&opt.cache_file)?
.context("load cache: Cannot undeploy without a cache.")?;
// Used just to transform them into Description structs
let state = FileState::new(
BTreeMap::default(),
BTreeMap::default(),
cache.symlinks.clone(),
cache.templates.clone(),
opt.cache_directory.clone(),
);
trace!("File state: {:#?}", state);
let config::Configuration {
files,
mut variables,
helpers,
packages,
} = config;
debug!("Creating Handlebars instance...");
let mut handlebars = Handlebars::new();
handlebars.register_escape_fn(|s| s.to_string()); // Disable html-escaping
handlebars.set_strict_mode(true); // Report missing variables as errors
handlebars_helpers::register_rust_helpers(&mut handlebars);
handlebars_helpers::register_script_helpers(&mut handlebars, &helpers);
handlebars_helpers::add_dotter_variable(&mut variables, &files, &packages);
trace!("Handlebars instance: {:#?}", handlebars);
debug!("Running pre-undeploy hook");
if opt.act {
hooks::run_hook(
&opt.pre_undeploy,
&opt.cache_directory,
&handlebars,
&variables,
)
.context("run pre-undeploy hook")?;
}
let mut suggest_force = false;
let mut error_occurred = false;
let plan = plan_deploy(state);
let mut fs = crate::filesystem::RealFilesystem::new(opt.interactive);
for action in plan {
match action.run(&mut fs, &opt, &handlebars, &variables) {
Ok(true) => action.affect_cache(&mut cache),
Ok(false) => {
suggest_force = true;
}
Err(e) => {
error_occurred = true;
display_error(e);
}
}
}
if suggest_force {
error!("Some files were skipped. To ignore errors and overwrite unexpected target files, use the --force flag.");
error_occurred = true;
}
if opt.act {
// Should be empty if everything went well, but if some things were skipped this contains
// them.
config::save_cache(&opt.cache_file, cache)?;
}
debug!("Running post-undeploy hook");
if opt.act {
hooks::run_hook(
&opt.post_undeploy,
&opt.cache_directory,
&handlebars,
&variables,
)
.context("run post-undeploy hook")?;
}
Ok(error_occurred)
}
pub fn plan_deploy(state: FileState) -> Vec<Action> {
let mut actions = Vec::new();
let FileState {
desired_symlinks,
desired_templates,
existing_symlinks,
existing_templates,
} = state;
for deleted_symlink in existing_symlinks.difference(&desired_symlinks) {
actions.push(Action::DeleteSymlink(deleted_symlink.clone()));
}
for deleted_template in existing_templates.difference(&desired_templates) {
actions.push(Action::DeleteTemplate(deleted_template.clone()));
}
for created_symlink in desired_symlinks.difference(&existing_symlinks) {
actions.push(Action::CreateSymlink(created_symlink.clone()));
}
for created_template in desired_templates.difference(&existing_templates) {
actions.push(Action::CreateTemplate(created_template.clone()));
}
for updated_symlink in desired_symlinks.intersection(&existing_symlinks) {
actions.push(Action::UpdateSymlink(updated_symlink.clone()));
}
for updated_template in desired_templates.intersection(&existing_templates) {
actions.push(Action::UpdateTemplate(updated_template.clone()));
}
actions
}
#[cfg(test)]
mod test {
use crate::{
config::{SymbolicTarget, TemplateTarget},
filesystem::SymlinkComparison,
};
use crate::{
file_state::{SymlinkDescription, TemplateDescription},
filesystem::TemplateComparison,
};
use std::{
collections::BTreeSet,
path::{Path, PathBuf},
};
use super::*;
use mockall::predicate::*;
#[test]
fn initial_deploy() {
// File state
let a = SymlinkDescription {
source: "a_in".into(),
target: SymbolicTarget {
target: "a_out".into(),
owner: None,
},
};
let b = TemplateDescription {
source: "b_in".into(),
target: TemplateTarget {
target: "b_out".into(),
owner: None,
append: None,
prepend: None,
},
cache: "cache/b_cache".into(),
};
let file_state = FileState {
desired_symlinks: maplit::btreeset! {
a.clone()
},
desired_templates: maplit::btreeset! {
b.clone()
},
existing_symlinks: BTreeSet::new(),
existing_templates: BTreeSet::new(),
};
// Plan
let actions = plan_deploy(file_state);
assert_eq!(
actions,
[Action::CreateSymlink(a), Action::CreateTemplate(b)]
);
// Setup
let mut fs = crate::filesystem::MockFilesystem::new();
let mut seq = mockall::Sequence::new();
let options = Options::default();
let handlebars = handlebars::Handlebars::new();
let variables = Default::default();
fn path_eq(expected: &str) -> impl Fn(&Path) -> bool {
let expected = PathBuf::from(expected);
move |actual| actual == expected
}
// Action 1
fs.expect_compare_symlink()
.times(1)
.with(function(path_eq("a_in")), function(path_eq("a_out")))
.in_sequence(&mut seq)
.returning(|_, _| Ok(SymlinkComparison::OnlySourceExists));
fs.expect_create_dir_all()
.times(1)
.with(function(path_eq("")), eq(None)) // parent of a_out
.in_sequence(&mut seq)
.returning(|_, _| Ok(()));
fs.expect_make_symlink()
.times(1)
.with(
function(path_eq("a_out")),
function(path_eq("a_in")),
eq(None),
)
.in_sequence(&mut seq)
.returning(|_, _, _| Ok(()));
actions[0]
.run(&mut fs, &options, &handlebars, &variables)
.unwrap();
fs.checkpoint();
// Action 2
fs.expect_compare_template()
.times(1)
.with(
function(path_eq("b_out")),
function(path_eq("cache/b_cache")),
)
.in_sequence(&mut seq)
.returning(|_, _| Ok(TemplateComparison::BothMissing));
fs.expect_create_dir_all()
.times(1)
.with(function(path_eq("")), eq(None)) // parent of b_out
.in_sequence(&mut seq)
.returning(|_, _| Ok(()));
fs.expect_read_to_string()
.times(1)
.with(function(path_eq("b_in")))
.in_sequence(&mut seq)
.returning(|_| Ok("".into()));
fs.expect_create_dir_all()
.times(1)
.with(function(path_eq("cache")), eq(None))
.in_sequence(&mut seq)
.returning(|_, _| Ok(()));
fs.expect_write()
.times(1)
.with(function(path_eq("cache/b_cache")), eq(String::from("")))
.in_sequence(&mut seq)
.returning(|_, _| Ok(()));
fs.expect_copy_file()
.times(1)
.with(function(path_eq("cache/b_cache")), function(path_eq("b_out")), eq(None))
.in_sequence(&mut seq)
.returning(|_, _, _| Ok(()));
fs.expect_copy_permissions()
.times(1)
.with(function(path_eq("b_in")), function(path_eq("b_out")), eq(None))
.in_sequence(&mut seq)
.returning(|_, _, _| Ok(()));
actions[1]
.run(&mut fs, &options, &handlebars, &variables)
.unwrap();
}
}