docs: improve documentation of settings using a subcommand
This commit is contained in:
1 parent
4e943f578a
commit
65ccaebc1c
17 files changed
+514
-223
No files matched your search
+80
-14
@@ -7,10 +7,16 @@ mod run;
|
||||
mod sysroot;
|
||||
mod systemd;
|
||||
|
||||
use std::{fs, io::ErrorKind, path::PathBuf, process::ExitCode, time::Duration};
|
||||
use std::{
|
||||
error::Error,
|
||||
io::ErrorKind,
|
||||
path::{Path, PathBuf},
|
||||
process::ExitCode,
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use bootvine::config::{Config, Timing};
|
||||
use clap::Parser;
|
||||
use bootvine::config::{Config, ConfigError, Timing, default_config_table, read_config_file};
|
||||
use clap::{Parser, Subcommand, ValueEnum};
|
||||
use jiff::fmt::friendly::SpanParser;
|
||||
|
||||
use crate::{
|
||||
@@ -20,8 +26,13 @@ use crate::{
|
||||
};
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(about = "Live dependency tree of systemd jobs while the system boots")]
|
||||
#[command(
|
||||
about = "Live dependency tree of systemd jobs while the system boots",
|
||||
args_conflicts_with_subcommands = true
|
||||
)]
|
||||
struct Cli {
|
||||
#[command(subcommand)]
|
||||
command: Option<Command>,
|
||||
#[arg(long, help = "Watch the user service manager instead of PID 1")]
|
||||
user: bool,
|
||||
#[arg(
|
||||
@@ -38,27 +49,82 @@ struct Cli {
|
||||
long,
|
||||
value_parser = |raw: &str| SpanParser::new().parse_unsigned_duration(raw),
|
||||
help = "How long the final frame stays on screen, e.g. 1s or 500ms; overrides \
|
||||
timing.keep_final_frame_for"
|
||||
timing.keep-final-frame-for"
|
||||
)]
|
||||
hold: Option<Duration>,
|
||||
#[arg(
|
||||
long,
|
||||
default_value = "/etc/bootvine.toml",
|
||||
help = "TOML file with [palette] and [timing] tables"
|
||||
help = "TOML file with [colors] and [timing] tables; see `bootvine defaults`"
|
||||
)]
|
||||
config: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Command {
|
||||
#[command(about = "Print every setting with its default value")]
|
||||
Defaults {
|
||||
#[arg(long, value_enum, default_value_t = DefaultsFormat::Toml)]
|
||||
format: DefaultsFormat,
|
||||
},
|
||||
#[command(about = "Exit with an error if a config file is unreadable or invalid")]
|
||||
Check { path: PathBuf },
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, ValueEnum)]
|
||||
enum DefaultsFormat {
|
||||
Toml,
|
||||
Nix,
|
||||
}
|
||||
|
||||
fn main() -> ExitCode {
|
||||
let cli = Cli::parse();
|
||||
let config = match fs::read_to_string(&cli.config) {
|
||||
Ok(text) => toml::from_str(&text).unwrap_or_else(|error| {
|
||||
eprintln!("bootvine: ignoring {}: {error}", cli.config.display());
|
||||
Config::default()
|
||||
}),
|
||||
Err(error) if error.kind() == ErrorKind::NotFound => Config::default(),
|
||||
match cli.command {
|
||||
Some(Command::Defaults { format }) => print_defaults(format),
|
||||
Some(Command::Check { ref path }) => check_config_file(path),
|
||||
None => watch_boot(cli),
|
||||
}
|
||||
}
|
||||
|
||||
fn render_defaults(format: DefaultsFormat) -> Result<String, Box<dyn Error>> {
|
||||
let defaults = default_config_table()?;
|
||||
Ok(match format {
|
||||
DefaultsFormat::Toml => toml::to_string_pretty(&defaults)?,
|
||||
DefaultsFormat::Nix => ser_nix::to_string(&defaults)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn print_defaults(format: DefaultsFormat) -> ExitCode {
|
||||
match render_defaults(format) {
|
||||
Ok(text) => {
|
||||
println!("{}", text.trim_end());
|
||||
ExitCode::SUCCESS
|
||||
}
|
||||
Err(error) => {
|
||||
eprintln!("bootvine: cannot read {}: {error}", cli.config.display());
|
||||
eprintln!("bootvine: {error}");
|
||||
ExitCode::FAILURE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn check_config_file(path: &Path) -> ExitCode {
|
||||
match read_config_file(path) {
|
||||
Ok(_) => ExitCode::SUCCESS,
|
||||
Err(error) => {
|
||||
eprintln!("bootvine: {}: {error}", path.display());
|
||||
ExitCode::FAILURE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn watch_boot(cli: Cli) -> ExitCode {
|
||||
let config = match read_config_file(&cli.config) {
|
||||
Ok(config) => config,
|
||||
Err(ConfigError::Unreadable(error)) if error.kind() == ErrorKind::NotFound => {
|
||||
Config::default()
|
||||
}
|
||||
Err(error) => {
|
||||
eprintln!("bootvine: ignoring {}: {error}", cli.config.display());
|
||||
Config::default()
|
||||
}
|
||||
};
|
||||
@@ -72,7 +138,7 @@ fn main() -> ExitCode {
|
||||
keep_final_frame_for: cli.hold.unwrap_or(config.timing.keep_final_frame_for),
|
||||
..config.timing
|
||||
},
|
||||
palette: config.palette,
|
||||
colors: config.colors,
|
||||
};
|
||||
match run(&settings) {
|
||||
Ok(()) => ExitCode::SUCCESS,
|
||||
|
||||
Reference in new issue
Block a user