metarepo-core 0.19.2

Core interfaces and types for the metarepo multi-project management tool
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
use anyhow::{anyhow, Result};
use console::style;
use dialoguer::{Confirm, Input, MultiSelect, Select};
use std::io::{self, IsTerminal};

/// Controls behavior when running in non-interactive mode
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NonInteractiveMode {
    /// Fail with an error when required input is missing
    Fail,
    /// Use sensible defaults for missing inputs (only for optional args)
    Defaults,
}

impl std::str::FromStr for NonInteractiveMode {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self> {
        match s.to_lowercase().as_str() {
            "fail" => Ok(NonInteractiveMode::Fail),
            "defaults" => Ok(NonInteractiveMode::Defaults),
            other => Err(anyhow!(
                "Invalid non-interactive mode: '{}'. Use 'fail' or 'defaults'",
                other
            )),
        }
    }
}

/// Detects if we're running in an interactive TTY
pub fn is_interactive() -> bool {
    io::stdin().is_terminal() && io::stdout().is_terminal()
}

/// Prompts for a required text input
///
/// # Arguments
/// * `prompt` - The prompt text to display
/// * `default` - Optional default value
/// * `allow_empty` - Whether to allow empty input
/// * `non_interactive` - How to behave when not in a TTY
///
/// # Examples
/// ```no_run
/// # use metarepo_core::interactive::*;
/// # fn main() -> anyhow::Result<()> {
/// let name = prompt_text(
///     "Project name",
///     None,
///     false,
///     NonInteractiveMode::Fail,
/// )?;
/// # Ok(())
/// # }
/// ```
pub fn prompt_text(
    prompt: &str,
    default: Option<&str>,
    allow_empty: bool,
    non_interactive: NonInteractiveMode,
) -> Result<String> {
    if !is_interactive() {
        return handle_non_interactive(non_interactive, prompt, default.map(|s| s.to_string()));
    }

    loop {
        let mut input = Input::<String>::new();
        input = input.with_prompt(format!("{}", style(format!("{}", prompt)).cyan()));

        if let Some(default_val) = default {
            input = input.default(default_val.to_string());
        }

        match input.interact_text() {
            Ok(value) => {
                if !allow_empty && value.trim().is_empty() {
                    eprintln!("{}", style("  ✗ Input cannot be empty").red());
                    continue;
                }
                return Ok(value);
            }
            Err(e) if is_eof_error(&e) => {
                // Handle Ctrl+D or EOF
                return Err(anyhow!("Cancelled by user"));
            }
            Err(e) => return Err(e.into()),
        }
    }
}

/// Prompts for a URL input with optional validation
///
/// # Arguments
/// * `prompt` - The prompt text
/// * `default` - Optional default value
/// * `required` - Whether the input is required
/// * `non_interactive` - How to behave when not in a TTY
pub fn prompt_url(
    prompt: &str,
    default: Option<&str>,
    required: bool,
    non_interactive: NonInteractiveMode,
) -> Result<Option<String>> {
    if !is_interactive() {
        let result =
            handle_non_interactive(non_interactive, prompt, default.map(|s| s.to_string()));
        match result {
            Ok(val) => {
                if val.is_empty() && !required {
                    return Ok(None);
                }
                Ok(Some(val))
            }
            Err(e) => Err(e),
        }
    } else {
        let label = if required {
            prompt.to_string()
        } else {
            format!("{} (optional)", prompt)
        };

        loop {
            let mut input = Input::<String>::new();
            input = input.with_prompt(format!("{}", style(format!("{}", label)).cyan()));

            if let Some(default_val) = default {
                input = input.default(default_val.to_string());
            }

            match input.interact_text() {
                Ok(value) => {
                    if value.trim().is_empty() {
                        if !required {
                            return Ok(None);
                        }
                        eprintln!("{}", style("  ✗ URL cannot be empty").red());
                        continue;
                    }

                    // Basic URL validation
                    if !is_valid_url(&value) {
                        eprintln!(
                            "{}",
                            style(
                                "  ✗ Invalid URL format. Expected http(s)://, git@, or file path"
                            )
                            .red()
                        );
                        continue;
                    }

                    return Ok(Some(value));
                }
                Err(e) if is_eof_error(&e) => {
                    return Err(anyhow!("Cancelled by user"));
                }
                Err(e) => return Err(e.into()),
            }
        }
    }
}

/// Prompts for a yes/no confirmation
///
/// # Arguments
/// * `prompt` - The prompt text
/// * `default` - Default value if user just presses enter
/// * `non_interactive` - How to behave when not in a TTY
pub fn prompt_confirm(
    prompt: &str,
    default: bool,
    non_interactive: NonInteractiveMode,
) -> Result<bool> {
    if !is_interactive() {
        match non_interactive {
            NonInteractiveMode::Fail => {
                Err(anyhow!(
                    "Interactive confirmation required for: '{}'. Use --non-interactive=defaults or provide --force",
                    prompt
                ))
            }
            NonInteractiveMode::Defaults => Ok(default),
        }
    } else {
        let confirm = Confirm::new()
            .with_prompt(format!("{}", style(format!("{}", prompt)).cyan()))
            .default(default)
            .interact_opt()?;

        match confirm {
            Some(value) => Ok(value),
            None => Err(anyhow!("Cancelled by user")),
        }
    }
}

/// Prompts for a single selection from a list
///
/// # Arguments
/// * `prompt` - The prompt text
/// * `items` - List of items to choose from
/// * `default_index` - Index of the default selection
/// * `non_interactive` - How to behave when not in a TTY
pub fn prompt_select<S: Into<String>>(
    prompt: &str,
    items: Vec<S>,
    default_index: Option<usize>,
    non_interactive: NonInteractiveMode,
) -> Result<String> {
    let items: Vec<String> = items.into_iter().map(|s| s.into()).collect();

    if !is_interactive() {
        return handle_non_interactive_select(non_interactive, prompt, &items, default_index);
    }

    if items.is_empty() {
        return Err(anyhow!("No items to select from"));
    }

    let select = Select::new();
    let select = select.with_prompt(format!("{}", style(format!("{}", prompt)).cyan()));

    let mut select_with_items = select;
    for item in &items {
        select_with_items = select_with_items.item(item);
    }

    if let Some(idx) = default_index {
        if idx < items.len() {
            select_with_items = select_with_items.default(idx);
        }
    }

    match select_with_items.interact_opt()? {
        Some(idx) => Ok(items[idx].clone()),
        None => Err(anyhow!("Cancelled by user")),
    }
}

/// Prompts for multiple selections from a list
///
/// # Arguments
/// * `prompt` - The prompt text
/// * `items` - List of items to choose from
/// * `defaults` - Indices of default selections
/// * `non_interactive` - How to behave when not in a TTY
pub fn prompt_multiselect<S: Into<String>>(
    prompt: &str,
    items: Vec<S>,
    defaults: Vec<usize>,
    non_interactive: NonInteractiveMode,
) -> Result<Vec<String>> {
    let items: Vec<String> = items.into_iter().map(|s| s.into()).collect();

    if !is_interactive() {
        return handle_non_interactive_multiselect(non_interactive, prompt, &items, defaults);
    }

    if items.is_empty() {
        return Err(anyhow!("No items to select from"));
    }

    let select = MultiSelect::new();
    let select = select.with_prompt(format!("{}", style(format!("{}", prompt)).cyan()));

    let mut select_with_items = select;
    for item in &items {
        select_with_items = select_with_items.item(item);
    }

    for idx in defaults {
        if idx < items.len() {
            select_with_items = select_with_items.item_checked(idx, true);
        }
    }

    match select_with_items.interact_opt()? {
        Some(indices) => {
            if indices.is_empty() {
                Err(anyhow!("At least one item must be selected"))
            } else {
                Ok(indices.into_iter().map(|i| items[i].clone()).collect())
            }
        }
        None => Err(anyhow!("Cancelled by user")),
    }
}

// ============================================================================
// Private helper functions
// ============================================================================

/// Handles input when not in interactive mode
fn handle_non_interactive(
    mode: NonInteractiveMode,
    prompt: &str,
    default: Option<String>,
) -> Result<String> {
    match mode {
        NonInteractiveMode::Fail => {
            Err(anyhow!(
                "Interactive input required for '{}' and no default provided. Use --non-interactive=defaults or provide the value explicitly",
                prompt
            ))
        }
        NonInteractiveMode::Defaults => {
            default.ok_or_else(|| anyhow!(
                "No default value available for '{}' in non-interactive mode",
                prompt
            ))
        }
    }
}

/// Handles selection when not in interactive mode
fn handle_non_interactive_select(
    mode: NonInteractiveMode,
    prompt: &str,
    items: &[String],
    default: Option<usize>,
) -> Result<String> {
    match mode {
        NonInteractiveMode::Fail => {
            Err(anyhow!(
                "Interactive selection required for '{}'. Use --non-interactive=defaults or provide the value explicitly",
                prompt
            ))
        }
        NonInteractiveMode::Defaults => {
            let idx = default.ok_or_else(|| anyhow!(
                "No default selection available for '{}' in non-interactive mode",
                prompt
            ))?;
            Ok(items
                .get(idx)
                .ok_or_else(|| anyhow!("Default index {} out of range", idx))?
                .clone())
        }
    }
}

/// Handles multi-select when not in interactive mode
fn handle_non_interactive_multiselect(
    mode: NonInteractiveMode,
    prompt: &str,
    items: &[String],
    defaults: Vec<usize>,
) -> Result<Vec<String>> {
    match mode {
        NonInteractiveMode::Fail => {
            Err(anyhow!(
                "Interactive selection required for '{}'. Use --non-interactive=defaults or provide values explicitly",
                prompt
            ))
        }
        NonInteractiveMode::Defaults => {
            if defaults.is_empty() {
                Err(anyhow!(
                    "No default selection available for '{}' in non-interactive mode",
                    prompt
                ))
            } else {
                Ok(defaults
                    .iter()
                    .map(|idx| {
                        items
                            .get(*idx)
                            .ok_or_else(|| anyhow!("Default index {} out of range", idx)).cloned()
                    })
                    .collect::<Result<Vec<_>>>()?)
            }
        }
    }
}

/// Validates a URL format (basic check)
fn is_valid_url(url: &str) -> bool {
    let url = url.trim();
    url.starts_with("http://")
        || url.starts_with("https://")
        || url.starts_with("git@")
        || url.starts_with("./")
        || url.starts_with("../")
        || url.starts_with("/")
        || !url.contains(' ')
}

/// Checks if an error is due to EOF (Ctrl+D)
fn is_eof_error(error: &dyn std::error::Error) -> bool {
    error.to_string().contains("EOF")
        || error.to_string().contains("end of file")
        || error.to_string().contains("Ctrl+D")
}

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

    #[test]
    fn test_non_interactive_mode_parsing() {
        assert_eq!(
            "fail".parse::<NonInteractiveMode>().unwrap(),
            NonInteractiveMode::Fail
        );
        assert_eq!(
            "defaults".parse::<NonInteractiveMode>().unwrap(),
            NonInteractiveMode::Defaults
        );
        assert_eq!(
            "FAIL".parse::<NonInteractiveMode>().unwrap(),
            NonInteractiveMode::Fail
        );
        assert!("invalid".parse::<NonInteractiveMode>().is_err());
    }

    #[test]
    fn test_valid_url() {
        assert!(is_valid_url("https://github.com/user/repo.git"));
        assert!(is_valid_url("http://example.com"));
        assert!(is_valid_url("git@github.com:user/repo.git"));
        assert!(is_valid_url("./local/path"));
        assert!(is_valid_url("../relative/path"));
        assert!(is_valid_url("/absolute/path"));
        assert!(!is_valid_url("invalid url with spaces"));
    }

    #[test]
    fn test_handle_non_interactive_fail() {
        let result = handle_non_interactive(NonInteractiveMode::Fail, "test", None);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Interactive input required"));
    }

    #[test]
    fn test_handle_non_interactive_defaults() {
        let result = handle_non_interactive(
            NonInteractiveMode::Defaults,
            "test",
            Some("default_value".to_string()),
        );
        assert_eq!(result.unwrap(), "default_value");
    }

    #[test]
    fn test_handle_non_interactive_defaults_no_default() {
        let result = handle_non_interactive(NonInteractiveMode::Defaults, "test", None);
        assert!(result.is_err());
    }
}