yconn 1.12.0

SSH connection manager for teams and DevOps environments
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
// src/commands/remove.rs
// Handler for `yconn remove <name>` — remove a connection, prompting for
// layer if ambiguous.

use std::io::{self, BufRead, Write};
use std::path::Path;

use anyhow::{anyhow, bail, Context, Result};

use crate::cli::LayerArg;
use crate::config::{Layer, LoadedConfig};
use crate::display::Renderer;

// ─── Public entry point ───────────────────────────────────────────────────────

pub fn run(
    cfg: &LoadedConfig,
    renderer: &Renderer,
    name: &str,
    layer: Option<LayerArg>,
) -> Result<()> {
    let stdin = io::stdin();
    let stdout = io::stdout();
    run_impl(
        cfg,
        renderer,
        name,
        layer,
        &mut stdin.lock(),
        &mut stdout.lock(),
    )
}

// ─── Testable impl ────────────────────────────────────────────────────────────

pub(crate) fn run_impl(
    cfg: &LoadedConfig,
    _renderer: &Renderer,
    name: &str,
    layer: Option<LayerArg>,
    input: &mut dyn BufRead,
    output: &mut dyn Write,
) -> Result<()> {
    // Collect all occurrences of the named connection across all layers.
    let all: Vec<_> = cfg
        .all_connections
        .iter()
        .filter(|c| c.name == name)
        .collect();

    if all.is_empty() {
        bail!("no connection named '{name}'");
    }

    // If --layer is given, restrict to that layer only.
    let target = if let Some(layer_arg) = layer {
        let target_layer = layer_arg_to_layer(layer_arg);
        all.iter()
            .find(|c| c.layer == target_layer)
            .copied()
            .ok_or_else(|| {
                anyhow!(
                    "no connection named '{name}' in the {} layer",
                    target_layer.label()
                )
            })?
    } else if all.len() == 1 {
        all[0]
    } else {
        // Ambiguous: multiple layers define this name — ask the user.
        prompt_layer_choice(name, &all, input, output)?
    };

    remove_from_file(&target.source_path, name)?;

    writeln!(
        output,
        "Removed '{name}' from {}",
        target.source_path.display()
    )?;

    Ok(())
}

// ─── Layer conversion ─────────────────────────────────────────────────────────

fn layer_arg_to_layer(arg: LayerArg) -> Layer {
    match arg {
        LayerArg::Project => Layer::Project,
        LayerArg::User => Layer::User,
        LayerArg::System => Layer::System,
    }
}

// ─── Ambiguity prompt ─────────────────────────────────────────────────────────

fn prompt_layer_choice<'a>(
    name: &str,
    options: &[&'a crate::config::Connection],
    input: &mut dyn BufRead,
    output: &mut dyn Write,
) -> Result<&'a crate::config::Connection> {
    writeln!(
        output,
        "'{name}' exists in multiple layers. Which one do you want to remove?"
    )?;
    for (i, c) in options.iter().enumerate() {
        writeln!(
            output,
            "  [{}] {} ({})",
            i + 1,
            c.layer.label(),
            c.source_path.display()
        )?;
    }

    loop {
        write!(output, "  Enter number [1-{}]: ", options.len())?;
        output.flush()?;

        let mut line = String::new();
        input.read_line(&mut line)?;
        let trimmed = line.trim();

        if trimmed.is_empty() {
            bail!("aborted");
        }

        match trimmed.parse::<usize>() {
            Ok(n) if n >= 1 && n <= options.len() => return Ok(options[n - 1]),
            _ => writeln!(
                output,
                "  Please enter a number between 1 and {}",
                options.len()
            )?,
        }
    }
}

// ─── YAML surgery ────────────────────────────────────────────────────────────

/// Remove the named connection block from the YAML file at `path`.
///
/// The strategy: collect all lines; find the `  <name>:` key line; remove it
/// and all subsequent lines that are indented more deeply (i.e. belong to the
/// same mapping value), stopping at the next same-or-lower-indentation line.
fn remove_from_file(path: &Path, name: &str) -> Result<()> {
    let content = std::fs::read_to_string(path)
        .with_context(|| format!("failed to read {}", path.display()))?;

    let updated = remove_entry(&content, name)
        .ok_or_else(|| anyhow!("connection '{name}' not found in {}", path.display()))?;

    std::fs::write(path, updated).with_context(|| format!("failed to write {}", path.display()))?;

    Ok(())
}

/// Remove the `  <name>:` block from YAML text, returning the updated string,
/// or `None` if the key was not found.
pub(crate) fn remove_entry(content: &str, name: &str) -> Option<String> {
    let key_line = format!("  {name}:");
    let lines: Vec<&str> = content.lines().collect();

    // Find the index of the key line.
    let start = lines
        .iter()
        .position(|l| *l == key_line || l.starts_with(&format!("{key_line} ")))?;

    // Find the end: the first subsequent line that is NOT more deeply indented
    // than the key (i.e. indentation <= 2 spaces, or empty line followed by
    // something at <= 2 spaces).
    let end = lines[start + 1..]
        .iter()
        .position(|l| {
            if l.is_empty() {
                false // blank lines inside the block are fine
            } else {
                // Count leading spaces.
                let indent = l.len() - l.trim_start().len();
                indent <= 2
            }
        })
        .map(|rel| start + 1 + rel)
        .unwrap_or(lines.len());

    // Also trim any trailing blank lines immediately before `end` that belong
    // to the removed block.
    let mut real_end = end;
    while real_end > start + 1 && lines[real_end - 1].trim().is_empty() {
        real_end -= 1;
    }

    let mut result: Vec<&str> = Vec::new();
    result.extend_from_slice(&lines[..start]);
    result.extend_from_slice(&lines[real_end..]);

    // Preserve the original trailing newline behaviour.
    let trailing_newline = content.ends_with('\n');
    let joined = result.join("\n");
    Some(if trailing_newline {
        format!("{joined}\n")
    } else {
        joined
    })
}

// ─── Tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    use crate::config;
    use crate::display::Renderer;

    fn write_yaml(dir: &std::path::Path, name: &str, content: &str) {
        fs::write(dir.join(name), content).unwrap();
    }

    fn no_color() -> Renderer {
        Renderer::new(false)
    }

    fn load(
        cwd: &std::path::Path,
        user: Option<&std::path::Path>,
        sys: &std::path::Path,
    ) -> config::LoadedConfig {
        config::load_impl(cwd, Some("connections"), false, user, sys).unwrap()
    }

    fn run_with_input(
        cfg: &config::LoadedConfig,
        name: &str,
        layer: Option<LayerArg>,
        answers: &[&str],
    ) -> Result<String> {
        let input_str = answers.join("\n") + "\n";
        let mut input = input_str.as_bytes();
        let mut output = Vec::new();
        run_impl(cfg, &no_color(), name, layer, &mut input, &mut output)?;
        Ok(String::from_utf8(output).unwrap())
    }

    // ── remove_entry helper ───────────────────────────────────────────────────

    #[test]
    fn test_remove_entry_single_connection() {
        let content = "version: 1\n\nconnections:\n  srv:\n    host: h\n    user: u\n    auth:\n      type: key\n      key: ~/.ssh/id_rsa\n    description: d\n";
        let result = remove_entry(content, "srv").unwrap();
        assert!(!result.contains("srv:"));
        assert!(result.contains("connections:"));
    }

    #[test]
    fn test_remove_entry_leaves_other_connections() {
        let content = "connections:\n  alpha:\n    host: a\n    user: u\n    auth:\n      type: key\n      key: ~/.ssh/id_rsa\n    description: d\n  beta:\n    host: b\n    user: u\n    auth:\n      type: key\n      key: ~/.ssh/id_rsa\n    description: d\n";
        let result = remove_entry(content, "alpha").unwrap();
        assert!(!result.contains("alpha:"));
        assert!(result.contains("beta:"));
    }

    #[test]
    fn test_remove_entry_returns_none_when_not_found() {
        let content = "connections:\n  other:\n    host: h\n";
        assert!(remove_entry(content, "missing").is_none());
    }

    // ── run_impl: basic remove ─────────────────────────────────────────────────

    #[test]
    fn test_remove_single_layer_no_prompt() {
        let dir = TempDir::new().unwrap();
        let yconn = dir.path().join(".yconn");
        fs::create_dir_all(&yconn).unwrap();
        write_yaml(
            &yconn,
            "connections.yaml",
            "version: 1\n\nconnections:\n  srv:\n    host: h\n    user: u\n    auth:\n      type: key\n      key: ~/.ssh/id_rsa\n    description: d\n",
        );
        let sys = TempDir::new().unwrap();
        let cfg = load(dir.path(), None, sys.path());
        // Confirm srv exists.
        assert!(cfg.find("srv").is_some());

        // Run remove with no layer flag and no interactive prompt needed (only one match).
        run_with_input(&cfg, "srv", None, &[]).unwrap();

        // Verify the file was updated.
        let content = fs::read_to_string(yconn.join("connections.yaml")).unwrap();
        assert!(!content.contains("  srv:"));
    }

    #[test]
    fn test_remove_unknown_name_returns_error() {
        let cwd = TempDir::new().unwrap();
        let empty = TempDir::new().unwrap();
        let cfg = load(cwd.path(), None, empty.path());

        let err = run_with_input(&cfg, "no-such", None, &[]).unwrap_err();
        assert!(err.to_string().contains("no-such"));
    }

    // ── --layer flag ──────────────────────────────────────────────────────────

    #[test]
    fn test_remove_with_layer_flag_targets_correct_layer() {
        let root = TempDir::new().unwrap();
        let yconn = root.path().join(".yconn");
        fs::create_dir_all(&yconn).unwrap();
        write_yaml(
            &yconn,
            "connections.yaml",
            "connections:\n  srv:\n    host: proj\n    user: u\n    auth:\n      type: key\n      key: ~/.ssh/id_rsa\n    description: d\n",
        );

        let sys = TempDir::new().unwrap();
        write_yaml(
            sys.path(),
            "connections.yaml",
            "connections:\n  srv:\n    host: sys\n    user: u\n    auth:\n      type: key\n      key: ~/.ssh/id_rsa\n    description: d\n",
        );

        let cfg = load(root.path(), None, sys.path());

        // Remove from system layer only.
        run_with_input(&cfg, "srv", Some(LayerArg::System), &[]).unwrap();

        // Project file unchanged.
        let proj_content = fs::read_to_string(yconn.join("connections.yaml")).unwrap();
        assert!(proj_content.contains("srv:"));

        // System file updated.
        let sys_content = fs::read_to_string(sys.path().join("connections.yaml")).unwrap();
        assert!(!sys_content.contains("  srv:"));
    }

    // ── layer_arg_to_layer ────────────────────────────────────────────────────

    #[test]
    fn test_layer_arg_to_layer_all_variants() {
        assert!(matches!(
            layer_arg_to_layer(LayerArg::System),
            Layer::System
        ));
        assert!(matches!(layer_arg_to_layer(LayerArg::User), Layer::User));
        assert!(matches!(
            layer_arg_to_layer(LayerArg::Project),
            Layer::Project
        ));
    }

    // ── ambiguous prompt ──────────────────────────────────────────────────────

    #[test]
    fn test_remove_ambiguous_prompts_user_and_removes_chosen() {
        let root = TempDir::new().unwrap();
        let yconn = root.path().join(".yconn");
        fs::create_dir_all(&yconn).unwrap();
        write_yaml(
            &yconn,
            "connections.yaml",
            "connections:\n  srv:\n    host: proj\n    user: u\n    auth:\n      type: key\n      key: ~/.ssh/id_rsa\n    description: d\n",
        );

        let sys = TempDir::new().unwrap();
        write_yaml(
            sys.path(),
            "connections.yaml",
            "connections:\n  srv:\n    host: sys\n    user: u\n    auth:\n      type: key\n      key: ~/.ssh/id_rsa\n    description: d\n",
        );

        let cfg = load(root.path(), None, sys.path());
        // Two occurrences → should prompt. Choose option 1 (project layer).
        run_with_input(&cfg, "srv", None, &["1"]).unwrap();

        // The project file should have srv removed.
        let proj_content = fs::read_to_string(yconn.join("connections.yaml")).unwrap();
        assert!(!proj_content.contains("  srv:"));

        // The system file should be untouched.
        let sys_content = fs::read_to_string(sys.path().join("connections.yaml")).unwrap();
        assert!(sys_content.contains("srv:"));
    }

    #[test]
    fn test_remove_ambiguous_empty_input_aborts() {
        let root = TempDir::new().unwrap();
        let yconn = root.path().join(".yconn");
        fs::create_dir_all(&yconn).unwrap();
        write_yaml(
            &yconn,
            "connections.yaml",
            "connections:\n  srv:\n    host: proj\n    user: u\n    auth:\n      type: key\n      key: ~/.ssh/id_rsa\n    description: d\n",
        );
        let sys = TempDir::new().unwrap();
        write_yaml(
            sys.path(),
            "connections.yaml",
            "connections:\n  srv:\n    host: sys\n    user: u\n    auth:\n      type: key\n      key: ~/.ssh/id_rsa\n    description: d\n",
        );
        let cfg = load(root.path(), None, sys.path());

        let err = run_with_input(&cfg, "srv", None, &[""]).unwrap_err();
        assert!(err.to_string().contains("aborted"));
    }
}