lanzaboote/rust/tool/src/cli.rs

102 lines
2.2 KiB
Rust
Raw Normal View History

2022-11-26 07:55:15 -06:00
use std::path::PathBuf;
2022-11-21 18:29:16 -06:00
2022-11-25 16:48:20 -06:00
use anyhow::{Context, Result};
2022-11-21 18:29:16 -06:00
use clap::{Parser, Subcommand};
2022-11-23 08:30:24 -06:00
use crate::install;
2022-11-26 16:19:08 -06:00
use crate::signature::KeyPair;
2022-11-21 18:29:16 -06:00
/// The default log level.
///
/// 2 corresponds to the level INFO.
const DEFAULT_LOG_LEVEL: usize = 2;
2022-11-21 18:29:16 -06:00
#[derive(Parser)]
pub struct Cli {
/// Silence all output
#[arg(short, long)]
quiet: bool,
/// Verbose mode (-v, -vv, etc.)
#[arg(short, long, action = clap::ArgAction::Count)]
verbose: u8,
2022-11-21 18:29:16 -06:00
#[clap(subcommand)]
2022-11-26 07:55:15 -06:00
commands: Commands,
2022-11-21 18:29:16 -06:00
}
#[derive(Subcommand)]
2022-11-26 07:55:15 -06:00
enum Commands {
Install(InstallCommand),
}
#[derive(Parser)]
struct InstallCommand {
/// Systemd path
#[arg(long)]
systemd: PathBuf,
/// Systemd-boot loader config
#[arg(long)]
systemd_boot_loader_config: PathBuf,
2022-11-26 17:12:00 -06:00
/// sbsign Public Key
2022-11-26 07:55:15 -06:00
#[arg(long)]
public_key: PathBuf,
2022-11-24 07:12:00 -06:00
2022-11-26 17:12:00 -06:00
/// sbsign Private Key
2022-11-26 07:55:15 -06:00
#[arg(long)]
private_key: PathBuf,
2022-11-24 07:12:00 -06:00
/// Configuration limit
#[arg(long, default_value_t = 1)]
configuration_limit: usize,
2022-11-26 17:12:00 -06:00
/// EFI system partition mountpoint (e.g. efiSysMountPoint)
esp: PathBuf,
/// List of generation links (e.g. /nix/var/nix/profiles/system-*-link)
2022-11-26 07:55:15 -06:00
generations: Vec<PathBuf>,
2022-11-21 18:29:16 -06:00
}
impl Cli {
pub fn call(self, module: &str) {
stderrlog::new()
.module(module)
.show_level(false)
.quiet(self.quiet)
.verbosity(DEFAULT_LOG_LEVEL + usize::from(self.verbose))
.init()
.expect("Failed to setup logger.");
if let Err(e) = self.commands.call() {
log::error!("{e:#}");
std::process::exit(1);
};
2022-11-21 18:29:16 -06:00
}
}
impl Commands {
pub fn call(self) -> Result<()> {
match self {
2022-11-26 07:55:15 -06:00
Commands::Install(args) => install(args),
2022-11-21 18:29:16 -06:00
}
}
}
2022-11-26 07:55:15 -06:00
fn install(args: InstallCommand) -> Result<()> {
2022-11-25 16:48:20 -06:00
let lanzaboote_stub =
std::env::var("LANZABOOTE_STUB").context("Failed to read LANZABOOTE_STUB env variable")?;
2022-11-24 07:12:00 -06:00
2022-11-26 16:19:08 -06:00
let key_pair = KeyPair::new(&args.public_key, &args.private_key);
2022-11-26 07:55:15 -06:00
install::Installer::new(
PathBuf::from(lanzaboote_stub),
args.systemd,
args.systemd_boot_loader_config,
2022-11-26 16:19:08 -06:00
key_pair,
args.configuration_limit,
args.esp,
2022-11-26 07:55:15 -06:00
args.generations,
2022-11-23 13:40:01 -06:00
)
2022-11-26 07:55:15 -06:00
.install()
2022-11-23 08:26:26 -06:00
}