zenops 0.4.1

Declarative system configuration management for shell config and dotfiles.
Documentation
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
use std::path::Path;

use indexmap::IndexMap;
use smol_str::SmolStr;
use zenops_expand::{ExpandLookup, ExpandStr};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Shell {
    Bash,
    Zsh,
}

/// Operating systems a pkg may opt into supporting. Extend as new platforms are
/// added. Kept intentionally coarse; finer targeting (e.g. macOS Apple Silicon
/// vs Intel, specific Linux distros) is deferred until a real use case arrives.
#[derive(serde::Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Os {
    Linux,
    Macos,
}

impl Os {
    pub fn current() -> Option<Self> {
        match std::env::consts::OS {
            "linux" => Some(Self::Linux),
            "macos" => Some(Self::Macos),
            _ => None,
        }
    }
}

#[derive(serde::Deserialize, Debug, Clone, PartialEq, Default)]
#[serde(rename_all = "snake_case")]
pub(crate) enum PkgEnable {
    #[default]
    Detect,
    On,
    Disabled,
}

#[derive(serde::Deserialize, Debug, Clone, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum DetectStrategy {
    File { path: ExpandStr },
    Which { binary: ExpandStr },
}

impl DetectStrategy {
    /// Expand placeholders and check the resulting detect target. An unresolved
    /// placeholder (e.g. `${brew_prefix}` on a brew-less system) means the
    /// strategy is inapplicable on this host — return `false`.
    pub fn check(&self, home: &Path, lookup: &impl ExpandLookup) -> bool {
        match self {
            Self::File { path } => {
                let Ok(expanded) = path.expand_to_string(lookup) else {
                    return false;
                };
                let resolved = expanded.replacen('~', &home.to_string_lossy(), 1);
                Path::new(&resolved).exists()
            }
            Self::Which { binary } => match binary.expand_to_string(lookup) {
                Ok(b) => which_on_path(&b),
                Err(_) => false,
            },
        }
    }
}

impl std::fmt::Display for DetectStrategy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::File { path } => write!(f, "{}", path.as_template()),
            Self::Which { binary } => write!(f, "which {}", binary.as_template()),
        }
    }
}

pub(crate) fn which_on_path(binary: &str) -> bool {
    std::env::var("PATH")
        .unwrap_or_default()
        .split(':')
        .any(|dir| Path::new(dir).join(binary).is_file())
}

#[derive(serde::Deserialize, Debug, Clone, PartialEq, Default)]
#[serde(deny_unknown_fields)]
pub struct InstallHint {
    pub brew: BrewHint,
    // Future package managers should be added as additional REQUIRED fields here
    // (e.g. apt, pacman, yum, dnf, zypper, apk, pkg) and wired into
    // `pkg_manager::DetectedPackageManager::{packages_for, install_command}`.
    // Keeping them required guarantees cross-manager completeness at parse time.
}

#[derive(serde::Deserialize, Debug, Clone, PartialEq, Default)]
#[serde(deny_unknown_fields)]
pub struct BrewHint {
    pub packages: Vec<String>,
}

#[derive(serde::Deserialize, Debug, Clone, PartialEq)]
pub struct ShellInitAction {
    #[serde(default)]
    pub optional: bool,
    #[serde(flatten)]
    pub kind: ActionKind,
}

#[derive(serde::Deserialize, Debug, Clone, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ActionKind {
    Comment {
        text: ExpandStr,
    },
    Source {
        path: ExpandStr,
    },
    EvalOutput {
        command: Vec<ExpandStr>,
    },
    SourceOutput {
        command: Vec<ExpandStr>,
    },
    Export {
        name: ExpandStr,
        value: ExpandStr,
    },
    Line {
        line: ExpandStr,
    },
    /// Emit `export PATH="VALUE:$PATH"` using the current shell's PATH
    /// syntax. The renderer owns the delimiter, quoting, and `$PATH`
    /// position so non-POSIX shells can change it in one place.
    PathPrepend {
        value: ExpandStr,
    },
    /// Emit `export PATH="$PATH:VALUE"` using the current shell's PATH
    /// syntax. See `PathPrepend` for the "renderer owns the how" rationale.
    PathAppend {
        value: ExpandStr,
    },
}

#[derive(serde::Deserialize, Debug, Clone, PartialEq, Default)]
#[serde(default)]
pub(crate) struct PerShellActions {
    pub bash: Vec<ShellInitAction>,
    pub zsh: Vec<ShellInitAction>,
}

impl PerShellActions {
    pub fn for_shell(&self, shell: Shell) -> &[ShellInitAction] {
        match shell {
            Shell::Bash => &self.bash,
            Shell::Zsh => &self.zsh,
        }
    }
}

#[derive(serde::Deserialize, Debug, Clone, PartialEq, Default)]
#[serde(default)]
pub(crate) struct PkgShellConfig {
    pub env_init: PerShellActions,
    pub login_init: PerShellActions,
    pub interactive_init: PerShellActions,
}

#[derive(serde::Deserialize, Debug, Clone, PartialEq, Default)]
pub struct PkgConfig {
    #[serde(default)]
    pub(super) enable: PkgEnable,
    #[serde(default)]
    pub(super) detect: Vec<DetectStrategy>,
    #[serde(default)]
    pub(super) inputs: IndexMap<SmolStr, SmolStr>,
    /// When non-empty, the pkg is only considered installed on the listed
    /// operating systems — an empty list means "any OS".
    #[serde(default)]
    pub(super) supported_os: Vec<Os>,
    #[serde(default)]
    pub description: Option<String>,
    pub install_hint: InstallHint,
    #[serde(default)]
    pub(crate) shell: PkgShellConfig,
}

impl PkgConfig {
    pub fn is_installed(&self, home: &Path, system_inputs: &IndexMap<SmolStr, SmolStr>) -> bool {
        if !self.supports_current_os() {
            return false;
        }
        match self.enable {
            PkgEnable::On => true,
            PkgEnable::Detect => {
                let lookup = [&self.inputs, system_inputs];
                self.detect.iter().any(|s| s.check(home, &lookup))
            }
            PkgEnable::Disabled => false,
        }
    }

    pub fn is_disabled(&self) -> bool {
        matches!(self.enable, PkgEnable::Disabled)
    }

    /// First detect strategy that matches, if any — used for debuggable output.
    pub fn matched_detect(
        &self,
        home: &Path,
        system_inputs: &IndexMap<SmolStr, SmolStr>,
    ) -> Option<&DetectStrategy> {
        if !self.supports_current_os() {
            return None;
        }
        match self.enable {
            PkgEnable::Detect => {
                let lookup = [&self.inputs, system_inputs];
                self.detect.iter().find(|s| s.check(home, &lookup))
            }
            PkgEnable::On | PkgEnable::Disabled => None,
        }
    }

    fn supports_current_os(&self) -> bool {
        self.supported_os.is_empty()
            || Os::current().is_some_and(|os| self.supported_os.contains(&os))
    }

    pub(crate) fn inputs(&self) -> &IndexMap<SmolStr, SmolStr> {
        &self.inputs
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn inputs(pairs: &[(&str, &str)]) -> IndexMap<SmolStr, SmolStr> {
        pairs
            .iter()
            .map(|(k, v)| (SmolStr::new(k), SmolStr::new(v)))
            .collect()
    }

    fn system_empty() -> IndexMap<SmolStr, SmolStr> {
        IndexMap::new()
    }

    #[test]
    fn install_hint_round_trips_from_toml() {
        let pkg: PkgConfig = toml::from_str(
            r#"
            description = "fuzzy finder"
            [install_hint.brew]
            packages = ["sk"]
            "#,
        )
        .unwrap();
        assert_eq!(pkg.description.as_deref(), Some("fuzzy finder"));
        assert_eq!(pkg.install_hint.brew.packages, vec!["sk".to_string()]);
    }

    #[test]
    fn install_hint_is_required_in_toml() {
        let err =
            toml::from_str::<PkgConfig>(r#"description = "missing install_hint""#).unwrap_err();
        assert!(
            err.to_string().contains("install_hint"),
            "expected error to mention install_hint, got: {err}"
        );
    }

    #[test]
    fn detect_strategy_with_unresolved_input_reports_not_installed() {
        let pkg: PkgConfig = toml::from_str(
            r#"
            enable = "detect"
            [install_hint.brew]
            packages = []
            [[detect]]
            type = "file"
            path = "${brew_prefix}/opt/x"
            "#,
        )
        .unwrap();
        let tmp = tempfile::tempdir().unwrap();
        assert!(!pkg.is_installed(tmp.path(), &system_empty()));
        assert!(pkg.matched_detect(tmp.path(), &system_empty()).is_none());
    }

    #[test]
    fn detect_strategy_resolves_system_input_and_checks_file() {
        let tmp = tempfile::tempdir().unwrap();
        let opt = tmp.path().join("opt/x");
        std::fs::create_dir_all(&opt).unwrap();
        let pkg: PkgConfig = toml::from_str(
            r#"
            enable = "detect"
            [install_hint.brew]
            packages = []
            [[detect]]
            type = "file"
            path = "${root}/opt/x"
            "#,
        )
        .unwrap();
        let sys = inputs(&[("root", tmp.path().to_str().unwrap())]);
        assert!(pkg.is_installed(tmp.path(), &sys));
    }

    #[test]
    fn pkg_inputs_shadow_system_inputs_at_detect() {
        let tmp = tempfile::tempdir().unwrap();
        let marker_a = tmp.path().join("a");
        std::fs::write(&marker_a, "").unwrap();
        let toml_src = format!(
            r#"
            enable = "detect"
            [install_hint.brew]
            packages = []
            [inputs]
            name = "a"
            [[detect]]
            type = "file"
            path = "{}/${{name}}"
            "#,
            tmp.path().display()
        );
        let pkg: PkgConfig = toml::from_str(&toml_src).unwrap();
        let sys = inputs(&[("name", "b")]);
        assert!(pkg.is_installed(tmp.path(), &sys));
    }

    #[test]
    fn disabled_pkg_is_never_installed() {
        let pkg: PkgConfig = toml::from_str(
            r#"
            enable = "disabled"
            [install_hint.brew]
            packages = []
            "#,
        )
        .unwrap();
        let tmp = tempfile::tempdir().unwrap();
        assert!(!pkg.is_installed(tmp.path(), &system_empty()));
        assert!(pkg.is_disabled());
    }

    #[test]
    fn supported_os_gates_installation() {
        // Pick the OS that is not current so the pkg must be filtered out.
        let other = match Os::current().expect("tests run on supported OS") {
            Os::Linux => "macos",
            Os::Macos => "linux",
        };
        let toml_src = format!(
            r#"
            enable = "on"
            supported_os = ["{other}"]
            [install_hint.brew]
            packages = []
            "#
        );
        let pkg: PkgConfig = toml::from_str(&toml_src).unwrap();
        let tmp = tempfile::tempdir().unwrap();
        assert!(!pkg.is_installed(tmp.path(), &system_empty()));
        assert!(pkg.matched_detect(tmp.path(), &system_empty()).is_none());
    }

    #[test]
    fn supported_os_allows_installation_when_current_os_listed() {
        let current = match Os::current().expect("tests run on supported OS") {
            Os::Linux => "linux",
            Os::Macos => "macos",
        };
        let toml_src = format!(
            r#"
            enable = "on"
            supported_os = ["{current}"]
            [install_hint.brew]
            packages = []
            "#
        );
        let pkg: PkgConfig = toml::from_str(&toml_src).unwrap();
        let tmp = tempfile::tempdir().unwrap();
        assert!(pkg.is_installed(tmp.path(), &system_empty()));
    }

    #[test]
    fn empty_supported_os_means_any_os() {
        let pkg: PkgConfig = toml::from_str(
            r#"
            enable = "on"
            [install_hint.brew]
            packages = []
            "#,
        )
        .unwrap();
        let tmp = tempfile::tempdir().unwrap();
        assert!(pkg.is_installed(tmp.path(), &system_empty()));
    }

    #[test]
    fn path_action_kinds_round_trip_from_toml() {
        let pkg: PkgConfig = toml::from_str(
            r#"
            enable = "on"
            [install_hint.brew]
            packages = []
            [[shell.env_init.bash]]
            type = "path_prepend"
            value = "/opt/foo/bin"
            [[shell.env_init.bash]]
            type = "path_append"
            value = "/opt/bar/bin"
            "#,
        )
        .unwrap();
        let actions = &pkg.shell.env_init.for_shell(Shell::Bash);
        assert_eq!(actions.len(), 2);
        assert!(matches!(actions[0].kind, ActionKind::PathPrepend { .. }));
        assert!(matches!(actions[1].kind, ActionKind::PathAppend { .. }));
    }
}