yolk/
yolk_paths.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
use std::path::{Path, PathBuf};

use fs_err::PathExt;
use miette::{Context as _, IntoDiagnostic, Result};
use normalize_path::NormalizePath;

use crate::{eggs_config::EggConfig, util::PathExt as _};

const DEFAULT_YOLK_RHAI: &str = indoc::indoc! {r#"
    export let data = #{
        for_vcs: LOCAL,
        cool_setting: if SYSTEM.hostname == "foo" { 10 } else { 25 }
    };
    export let eggs = #{
        foo: #{ targets: "~/.config/your-application", enabled: false, templates: [] }
    };
"#};

pub struct YolkPaths {
    /// Path to the yolk directory.
    root_path: PathBuf,
    home: PathBuf,
}

pub fn default_yolk_dir() -> PathBuf {
    let standard_dir = dirs::config_dir()
        .expect("No config dir available")
        .join("yolk");
    if standard_dir.exists() {
        standard_dir
    } else {
        let config_dir = if let Ok(config_dir) = std::env::var("XDG_CONFIG_DIR") {
            PathBuf::from(config_dir)
        } else {
            dirs::home_dir()
                .expect("No home dir available")
                .join(".config")
        };
        config_dir.join("yolk")
    }
}

impl YolkPaths {
    pub fn new(path: PathBuf, home: PathBuf) -> Self {
        YolkPaths {
            root_path: path,
            home: home
                .canonical()
                .expect("Failed to canonicalize home directory"),
        }
    }

    pub fn from_env() -> Self {
        Self::new(
            default_yolk_dir(),
            dirs::home_dir().expect("No home dir available"),
        )
    }

    pub fn set_yolk_dir(&mut self, path: PathBuf) {
        self.root_path = path;
    }
    pub fn set_home_dir(&mut self, path: PathBuf) {
        self.home = path
            .canonical()
            .expect("Failed to canonicalize home directory");
    }

    #[allow(unused)]
    pub fn check(&self) -> Result<()> {
        miette::ensure!(
            self.root_path().exists(),
            "Yolk directory does not exist at {}",
            self.root_path().abbr()
        );
        miette::ensure!(
            self.yolk_rhai_path().exists(),
            "Yolk directory does not contain a yolk.rhai file at {}",
            self.yolk_rhai_path().abbr()
        );
        miette::ensure!(
            self.eggs_dir_path().exists(),
            "Yolk directory does not contain an eggs directory at {}",
            self.eggs_dir_path().abbr()
        );
        Ok(())
    }

    pub fn create(&self) -> Result<()> {
        let path = self.root_path();
        if path.exists()
            && path.is_dir()
            && fs_err::read_dir(path).into_diagnostic()?.next().is_some()
        {
            miette::bail!("Yolk directory already exists at {}", path.abbr());
        }
        fs_err::create_dir_all(path).into_diagnostic()?;
        fs_err::create_dir_all(self.eggs_dir_path()).into_diagnostic()?;
        fs_err::write(self.yolk_rhai_path(), DEFAULT_YOLK_RHAI).into_diagnostic()?;

        Ok(())
    }

    /// Start an invocation of the `git` command with the `--git-dir` and `--work-tree` set to the yolk git and root path.
    pub fn start_git_command_builder(&self) -> std::process::Command {
        let mut cmd = std::process::Command::new("git");
        cmd.current_dir(self.root_path()).args([
            "--git-dir",
            &self.yolk_default_git_path().to_string_lossy(),
            "--work-tree",
            &self.root_path().to_string_lossy(),
        ]);
        cmd
    }
    pub fn root_path(&self) -> &std::path::Path {
        &self.root_path
    }
    pub fn home_path(&self) -> &std::path::Path {
        &self.home
    }
    pub fn yolk_default_git_path(&self) -> PathBuf {
        self.root_path.join(".git")
    }

    /// Path to the `yolk.rhai` file
    pub fn yolk_rhai_path(&self) -> PathBuf {
        self.root_path.join("yolk.rhai")
    }

    /// Path to the `eggs` directory
    pub fn eggs_dir_path(&self) -> PathBuf {
        self.root_path.join("eggs")
    }

    pub fn egg_path(&self, egg_name: &str) -> PathBuf {
        self.eggs_dir_path().join(egg_name)
    }

    pub fn get_egg(&self, name: &str, config: EggConfig) -> Result<Egg> {
        Egg::open(self.home.clone(), self.egg_path(name), config)
    }

    pub fn previous_egg_deployment_locations_db_path(&self) -> PathBuf {
        self.root_path.join(".previous_deployment_targets")
    }

    pub fn previous_egg_deployment_locations_db(&self) -> Result<PreviousEggDeploymentLocationsDb> {
        PreviousEggDeploymentLocationsDb::open(self.root_path.join(".deployed_cache"))
    }
}

pub struct PreviousEggDeploymentLocationsDb {
    path: PathBuf,
}

impl PreviousEggDeploymentLocationsDb {
    fn open(path: PathBuf) -> Result<Self> {
        fs_err::create_dir_all(&path).into_diagnostic()?;
        Ok(Self { path })
    }

    pub fn egg_data_path(&self, egg_name: &str) -> PathBuf {
        self.path.join(egg_name)
    }

    pub fn read(&self, egg_name: &str) -> Result<Vec<PathBuf>> {
        let cache_path = self.egg_data_path(egg_name);
        if cache_path.exists() {
            Ok(fs_err::read_to_string(cache_path)
                .into_diagnostic()?
                .lines()
                .map(PathBuf::from)
                .collect())
        } else {
            Ok(Vec::new())
        }
    }

    pub fn write(&self, egg_name: &str, symlinks: &[PathBuf]) -> Result<()> {
        let cache_path = self.egg_data_path(egg_name);
        let content = symlinks
            .iter()
            .map(|x| x.to_string_lossy())
            .collect::<Vec<_>>()
            .join("\n");
        fs_err::write(cache_path, content)
            .into_diagnostic()
            .with_context(|| format!("Failed to update egg deployment cache for egg {egg_name}"))
    }
}

#[derive(Debug)]
pub struct Egg {
    egg_dir: PathBuf,
    config: EggConfig,
    home_path: PathBuf,
}

impl Egg {
    pub fn open(home: PathBuf, egg_path: PathBuf, config: EggConfig) -> Result<Self> {
        miette::ensure!(
            egg_path.is_dir(),
            "No egg at {} does not exist",
            egg_path.abbr(),
        );
        Ok(Self {
            home_path: home.canonical()?,
            egg_dir: egg_path.canonical()?,
            config,
        })
    }

    #[allow(unused)]
    pub fn path(&self) -> &Path {
        &self.egg_dir
    }

    /// Check if the egg is _fully_ deployed (-> All contained entries have corresponding symlinks)
    #[tracing::instrument(skip_all, fields(egg.name = self.name()))]
    pub fn is_deployed(&self) -> Result<bool> {
        for x in self.find_deployed_symlinks()? {
            if x.context("Got error while iterating through deployed files or egg")?
                .is_err()
            {
                return Ok(false);
            }
        }
        Ok(true)
    }

    pub fn name(&self) -> &str {
        self.egg_dir
            .file_name()
            .unwrap_or_default()
            .to_str()
            .unwrap_or_default()
    }

    /// Iterate over the deployed symlinks of this egg.
    ///
    /// See [`TraverseDeployment`] for more information.
    #[tracing::instrument(skip_all)]
    pub fn find_deployed_symlinks(&self) -> Result<TraverseDeployment> {
        let targets = self
            .config
            .targets_expanded(&self.home_path, self.path())
            .context("Failed to expand targets map")?;
        Ok(TraverseDeployment::new(targets))
    }

    /// Find the first deployed symlink of a deployment.
    /// Note that this is not sufficient to check if the egg is fully deployed.
    #[tracing::instrument(skip_all)]
    pub fn find_first_deployed_symlink(&self) -> Result<Option<PathBuf>> {
        match self.find_deployed_symlinks()?.next() {
            Some(Ok(Ok(x))) => Ok(Some(x)),
            Some(Ok(Err(_))) => Ok(None),
            Some(Err(x)) => Err(x),
            None => Ok(None),
        }
    }

    pub fn config(&self) -> &EggConfig {
        &self.config
    }

    /// Get a mutable reference to the egg configuration. Deliberately only available for tests.
    #[cfg(test)]
    pub fn config_mut(&mut self) -> &mut EggConfig {
        &mut self.config
    }
}

/// An iterator that traverses a deployed egg and yields paths to all symlinks of the deployment.
///
/// Returns
/// - `Ok(Ok(path))` for a symlink that is correctly deployed,
/// - `Ok(Err(path_in_egg))` for a path inside an egg that does not have a corresponding deployed symlink
/// - `Err(err)` if there is an error
/// - `None` if the traversal is finished
pub struct TraverseDeployment {
    stack: Vec<(PathBuf, PathBuf)>,
}
impl TraverseDeployment {
    fn new(stack: impl IntoIterator<Item = (PathBuf, PathBuf)>) -> Self {
        let stack: Vec<_> = stack.into_iter().collect();
        Self { stack }
    }
}

impl Iterator for TraverseDeployment {
    type Item = miette::Result<Result<PathBuf, PathBuf>>;
    fn next(&mut self) -> Option<miette::Result<Result<PathBuf, PathBuf>>> {
        let (in_egg, link) = self.stack.pop()?;
        let in_egg = in_egg.normalize();
        let link = link.normalize();

        tracing::trace!(
            stack = ?self
                .stack
                .iter()
                .map(|x| (x.0.abbr(), x.1.abbr()))
                .collect::<Vec<_>>(),
            "checking for deployment {} -> {}.",
            link.abbr(),
            in_egg.abbr(),
        );

        if link.is_symlink() {
            match (
                in_egg.canonical(),
                link.fs_err_read_link().into_diagnostic(),
            ) {
                (Ok(in_egg), Ok(link)) if in_egg.normalize() == link.normalize() => {
                    Some(Ok(Ok(link)))
                }
                (Ok(in_egg), Ok(_)) => Some(Ok(Err(in_egg))),
                (Err(e), _) | (_, Err(e)) => Some(Err(e)),
            }
        } else if link.is_dir() && in_egg.is_dir() {
            cov_mark::hit!(traverse_dir_recursive);
            for in_egg_entry in fs_err::read_dir(&in_egg).ok()? {
                let in_egg_entry = match in_egg_entry {
                    Ok(x) => x,
                    Err(e) => return Some(Err(miette::miette!(e))),
                };
                let link_entry = link.join(in_egg_entry.file_name());
                self.stack.push((in_egg_entry.path(), link_entry));
            }
            return self.next();
        } else {
            return Some(Ok(Err(in_egg)));
        }
    }
}

#[cfg(test)]
mod test {
    use crate::{
        util::test_util::{setup_and_init_test_yolk, TestResult},
        yolk_paths::{Egg, DEFAULT_YOLK_RHAI},
    };
    use assert_fs::{
        assert::PathAssert,
        prelude::{FileWriteStr, PathChild, PathCreateDir},
        TempDir,
    };
    use miette::IntoDiagnostic;
    use predicates::path::exists;
    use test_log::test;

    use crate::eggs_config::EggConfig;

    use super::YolkPaths;

    #[test]
    pub fn test_setup() {
        let root = assert_fs::TempDir::new().unwrap();
        let yolk_paths = YolkPaths::new(root.child("yolk").to_path_buf(), root.to_path_buf());
        assert!(yolk_paths.check().is_err());
        yolk_paths.create().unwrap();
        assert!(yolk_paths.check().is_ok());
        root.child("yolk/eggs").assert(exists());
        root.child("yolk/yolk.rhai").assert(DEFAULT_YOLK_RHAI);
    }

    #[test]
    pub fn test_is_deployed_2() -> TestResult {
        cov_mark::check_count!(traverse_dir_recursive, 0);
        let (home, yolk, eggs) = setup_and_init_test_yolk()?;
        eggs.child("foo/foo.toml").write_str("")?;
        eggs.child("foo/thing/thing.toml").write_str("")?;
        let egg = Egg::open(
            home.to_path_buf(),
            eggs.child("foo").to_path_buf(),
            EggConfig::default().with_target("foo.toml", home.child("foo.toml")),
        )?;
        yolk.sync_egg_deployment(&egg)?;
        assert!(egg.is_deployed()?);
        Ok(())
    }

    #[test]
    pub fn test_is_deployed_single_dir() -> TestResult {
        cov_mark::check_count!(traverse_dir_recursive, 0);
        let (home, yolk, eggs) = setup_and_init_test_yolk()?;

        let test_egg_dir = eggs.child("test_egg");
        test_egg_dir.child("foo").create_dir_all()?;
        test_egg_dir.child("foo/bar").write_str("")?;
        let egg = Egg::open(
            home.to_path_buf(),
            test_egg_dir.to_path_buf(),
            EggConfig::new_merge(".", home.child("target")),
        )?;
        assert!(!(egg.is_deployed()?));
        yolk.sync_egg_deployment(&egg)?;
        assert!(egg.is_deployed()?);
        fs_err::remove_file(home.child("target"))?;
        assert!(!(egg.is_deployed()?));
        Ok(())
    }

    #[test]
    pub fn test_is_deployed() -> TestResult {
        cov_mark::check!(traverse_dir_recursive);
        let (home, yolk, eggs) = setup_and_init_test_yolk()?;
        let test_egg_dir = eggs.child("test_egg");

        home.child("content/dir_old").create_dir_all()?;
        home.child("content/dir_old/file_old").write_str("")?;
        test_egg_dir.child("content/file").write_str("")?;
        test_egg_dir.child("content/dir1").create_dir_all()?;
        test_egg_dir.child("content/dir2/dir1").create_dir_all()?;
        test_egg_dir.child("content/dir2/file1").write_str("")?;
        test_egg_dir.child("content/dir_old/file1").write_str("")?;
        test_egg_dir.child("content/dir_old/dir1").write_str("")?;
        test_egg_dir.child("content/dir3").create_dir_all()?;
        test_egg_dir.child("content/dir3/file1").write_str("")?;
        test_egg_dir.child("content/dir4/dir1").create_dir_all()?;

        let egg = Egg::open(
            home.to_path_buf(),
            test_egg_dir.to_path_buf(),
            EggConfig::new_merge(".", &home),
        )?;
        assert!(!(egg.is_deployed()?));
        yolk.sync_egg_deployment(&egg)?;
        assert!(egg.is_deployed()?);
        fs_err::remove_file(home.child("content/dir_old/file1"))?;
        assert!(!(egg.is_deployed()?));
        Ok(())
    }

    #[test]
    pub fn test_default_script() -> TestResult {
        let root = TempDir::new().into_diagnostic()?;
        let yolk_paths = YolkPaths::new(root.child("yolk").to_path_buf(), root.to_path_buf());
        yolk_paths.create().unwrap();
        let yolk = crate::yolk::Yolk::new(yolk_paths);
        _ = yolk.prepare_eval_ctx_for_templates(crate::yolk::EvalMode::Local)?;
        Ok(())
    }
}