use std::{num::NonZeroU32, path::PathBuf};
use clap::{Args, Parser, Subcommand};
use crate::logs::LogMode;
#[derive(Debug, Parser)]
#[command(version)]
pub(crate) struct Cli {
#[arg(
short,
long,
env = "DANCE_INVENTORY",
default_value = "inventory.toml",
global = true
)]
pub(crate) inventory: PathBuf,
#[arg(
short,
long,
env = "DANCE_LOGS",
value_enum,
default_value = "on-error",
global = true
)]
pub(crate) logs: LogMode,
#[arg(short = 'L', long, env = "DANCE_LOGS_DIR", global = true)]
pub(crate) logs_dir: Option<PathBuf>,
#[command(subcommand)]
pub(crate) command: Command,
}
#[derive(Debug, Subcommand)]
pub(crate) enum Command {
Status(WorkflowArgs),
Deploy(DeployArgs),
Verify(VerifyArgs),
}
#[derive(Args, Debug)]
pub(crate) struct WorkflowArgs {
#[arg(short, long)]
pub(crate) update: bool,
#[arg(value_name = "ID")]
pub(crate) ids: Vec<String>,
}
#[derive(Args, Debug)]
pub(crate) struct VerifyArgs {
#[arg(value_name = "ID")]
pub(crate) ids: Vec<String>,
}
#[derive(Args, Debug)]
pub(crate) struct DeployArgs {
#[command(flatten)]
pub(crate) workflow: WorkflowArgs,
#[arg(short, long, requires = "update")]
pub(crate) commit: bool,
#[arg(short = 'g', long, conflicts_with_all = ["update", "commit"])]
pub(crate) git: bool,
#[arg(short, long)]
pub(crate) yes: bool,
#[arg(short, long)]
pub(crate) skip_verifier: bool,
#[arg(short = 'R', long)]
pub(crate) no_rollback: bool,
#[arg(short, long, default_value = "2")]
pub(crate) keep_generations: NonZeroU32,
#[arg(short = 'G', long, conflicts_with = "keep_generations")]
pub(crate) no_gc: bool,
}
impl DeployArgs {
pub(crate) fn update_flakes(&self) -> bool {
self.workflow.update || self.git
}
pub(crate) fn commit_flakes(&self) -> bool {
self.commit || self.git
}
}
#[cfg(test)]
mod tests {
use clap::Parser;
use super::{Cli, Command};
#[test]
fn committing_requires_an_update() {
assert!(Cli::try_parse_from(["dance", "deploy", "--commit"]).is_err());
let cli = Cli::try_parse_from(["dance", "deploy", "--update", "--commit"])
.expect("commit with update should parse");
assert!(matches!(cli.command, Command::Deploy(args) if args.commit));
}
#[test]
fn git_enables_update_and_commit() {
let cli =
Cli::try_parse_from(["dance", "deploy", "--git"]).expect("Git shorthand should parse");
let Command::Deploy(args) = cli.command else {
panic!("deploy command should parse");
};
assert!(args.update_flakes());
assert!(args.commit_flakes());
assert!(Cli::try_parse_from(["dance", "deploy", "--git", "--update"]).is_err());
assert!(Cli::try_parse_from(["dance", "deploy", "--git", "--commit"]).is_err());
}
}