31 lines
1.0 KiB
Rust
31 lines
1.0 KiB
Rust
// SPDX-FileCopyrightText: 2026 Constantin Bauer
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
use std::io::{self, Write};
|
|
|
|
use clap::ArgMatches;
|
|
|
|
pub fn confirm(message: &str) -> Result<bool, io::Error> {
|
|
print!("{}? [y/N] ", message);
|
|
io::stdout().flush()?;
|
|
let mut input = String::new();
|
|
io::stdin().read_line(&mut input)?;
|
|
Ok(input.trim().eq_ignore_ascii_case("y"))
|
|
}
|
|
|
|
pub fn required_arg<'a>(matches: &'a ArgMatches, name: &str) -> Result<&'a str, io::Error> {
|
|
matches
|
|
.get_one::<String>(name)
|
|
.map(String::as_str)
|
|
.ok_or_else(|| io::Error::other(format!("missing required argument '{name}'")))
|
|
}
|
|
|
|
// A directory without instance.json is not an instance.
|
|
pub fn require_instance(instances_dir: &std::path::Path, name: &str) -> Result<(), io::Error> {
|
|
crate::instance::manager::validate_name(name).map_err(io::Error::other)?;
|
|
if !instances_dir.join(name).join("instance.json").exists() {
|
|
return Err(io::Error::other(format!("Instance '{name}' not found")));
|
|
}
|
|
Ok(())
|
|
}
|