use std::path::PathBuf;
use clap::{Args, Parser, Subcommand};
#[derive(Debug, Parser)]
#[command(about = "A cashu mint written in rust", author = env!("CARGO_PKG_AUTHORS"), version = env!("CARGO_PKG_VERSION"))]
pub struct CLIArgs {
#[arg(
short,
long,
help = "Use the <directory> as the location of the database",
required = false
)]
pub work_dir: Option<PathBuf>,
#[cfg(feature = "sqlcipher")]
#[arg(
short,
long,
global = true,
help = "Database password for SQLCipher (required when opening an encrypted database)"
)]
pub password: Option<String>,
#[arg(
short,
long,
global = true,
help = "Legacy startup flag; use `config init` or `config apply` instead",
required = false
)]
pub config: Option<PathBuf>,
#[arg(
long,
global = true,
help = "Legacy seed file; accepted only by `config migrate`",
required = false
)]
pub seed_file: Option<PathBuf>,
#[arg(
long,
help = "Enable logging output",
required = false,
action = clap::ArgAction::SetTrue,
default_value = "true"
)]
pub enable_logging: bool,
#[command(subcommand)]
pub command: Option<Commands>,
}
#[derive(Debug, Subcommand)]
pub enum Commands {
Config(ConfigArgs),
}
#[derive(Debug, Args)]
pub struct ConfigArgs {
#[command(subcommand)]
pub command: ConfigCommands,
}
#[derive(Debug, Subcommand)]
pub enum ConfigCommands {
Migrate(MigrateConfigArgs),
Init(ConfigFileArgs),
Validate(ConfigFileArgs),
Apply(ApplyConfigArgs),
Rollback,
Show,
Export(ExportConfigArgs),
}
#[derive(Debug, Args)]
pub struct MigrateConfigArgs {
#[arg(long)]
pub file: PathBuf,
#[arg(long)]
pub output: PathBuf,
#[arg(long)]
pub secrets_dir: Option<PathBuf>,
#[arg(long)]
pub force: bool,
}
#[derive(Debug, Args)]
pub struct ConfigFileArgs {
#[arg(long)]
pub file: PathBuf,
}
#[derive(Debug, Args)]
pub struct ExportConfigArgs {
#[arg(long)]
pub file: PathBuf,
#[arg(long)]
pub force: bool,
}
#[derive(Debug, Args)]
pub struct ApplyConfigArgs {
#[arg(long)]
pub file: PathBuf,
#[arg(long)]
pub validate_only: bool,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_configuration_commands() {
for command in ["init", "validate"] {
CLIArgs::try_parse_from(["cdk-mintd", "config", command, "--file", "/tmp/mint.toml"])
.expect("configuration command should parse");
}
let args =
CLIArgs::try_parse_from(["cdk-mintd", "config", "export", "--file", "/tmp/mint.toml"])
.expect("configuration export should parse");
assert!(matches!(
args.command,
Some(Commands::Config(ConfigArgs {
command: ConfigCommands::Export(ExportConfigArgs { force: false, .. }),
}))
));
let args = CLIArgs::try_parse_from([
"cdk-mintd",
"config",
"export",
"--file",
"/tmp/mint.toml",
"--force",
])
.expect("configuration export should parse");
assert!(matches!(
args.command,
Some(Commands::Config(ConfigArgs {
command: ConfigCommands::Export(ExportConfigArgs { force: true, .. }),
}))
));
CLIArgs::try_parse_from([
"cdk-mintd",
"config",
"apply",
"--file",
"/tmp/mint.toml",
"--validate-only",
])
.expect("configuration apply should parse");
CLIArgs::try_parse_from(["cdk-mintd", "config", "show"])
.expect("configuration show should parse");
CLIArgs::try_parse_from(["cdk-mintd", "config", "rollback"])
.expect("configuration rollback should parse");
let args = CLIArgs::try_parse_from([
"cdk-mintd",
"config",
"migrate",
"--file",
"/tmp/legacy.toml",
"--output",
"/tmp/migrated.toml",
"--secrets-dir",
"/tmp/mint-secrets",
])
.expect("configuration migration should parse");
assert!(matches!(
args.command,
Some(Commands::Config(ConfigArgs {
command: ConfigCommands::Migrate(MigrateConfigArgs { force: false, .. }),
}))
));
let args = CLIArgs::try_parse_from([
"cdk-mintd",
"--seed-file",
"/tmp/seed.txt",
"config",
"migrate",
"--file",
"/tmp/legacy.toml",
"--output",
"/tmp/migrated.toml",
])
.expect("legacy seed-file migration should parse");
assert_eq!(args.seed_file, Some(PathBuf::from("/tmp/seed.txt")));
}
#[test]
fn no_subcommand_still_parses_daemon_startup() {
let args = CLIArgs::try_parse_from(["cdk-mintd"]).expect("daemon arguments should parse");
assert!(args.command.is_none());
}
}