use clap::{CommandFactory, Parser, Subcommand};
use clap_complete::Shell;
use std::path::PathBuf;
#[derive(Parser)]
#[command(name = "blackship")]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
pub struct Cli {
#[arg(short, long, default_value = "blackship.toml")]
pub config: PathBuf,
#[arg(short, long)]
pub verbose: bool,
#[command(subcommand)]
pub command: Commands,
}
#[derive(Subcommand)]
pub enum Commands {
Up {
jail: Option<String>,
#[arg(long, conflicts_with = "jail")]
all: bool,
#[arg(long)]
dry_run: bool,
},
Down {
jail: Option<String>,
#[arg(long, conflicts_with = "jail")]
all: bool,
#[arg(long)]
dry_run: bool,
},
Restart {
jail: Option<String>,
#[arg(long, conflicts_with = "jail")]
all: bool,
#[arg(long)]
dry_run: bool,
},
Ps {
#[arg(long)]
json: bool,
},
Check,
Setup,
Init {
#[arg(short, long, default_value = "Jailfile")]
file: PathBuf,
#[arg(short, long)]
release: Option<String>,
#[arg(long)]
toml: bool,
#[arg(short = 'y', long)]
force: bool,
},
Armada {
#[arg(short, long = "file", default_value = "blackship.toml")]
files: Vec<PathBuf>,
#[command(subcommand)]
action: ArmadaAction,
},
Exec {
jail: String,
#[arg(short, long, default_value = "root")]
user: String,
#[arg(last = true, required = true)]
command: Vec<String>,
},
Console {
jail: String,
#[arg(short, long, default_value = "root")]
user: String,
},
Bootstrap {
release: String,
#[arg(short, long)]
force: bool,
#[arg(short, long, value_delimiter = ',')]
archives: Option<Vec<String>>,
},
Releases {
#[command(subcommand)]
action: Option<ReleasesAction>,
#[arg(long)]
json: bool,
},
Network {
#[command(subcommand)]
action: NetworkAction,
},
Health {
jail: Option<String>,
#[arg(short, long)]
watch: bool,
#[arg(short, long, default_value = "5")]
interval: u64,
#[arg(long)]
json: bool,
},
Build {
#[arg(short, long, default_value = "Jailfile")]
file: PathBuf,
#[arg(short, long)]
name: Option<String>,
#[arg(long = "build-arg", value_parser = parse_key_val)]
build_args: Vec<(String, String)>,
#[arg(short, long)]
context: Option<PathBuf>,
#[arg(long)]
dry_run: bool,
},
Template {
#[command(subcommand)]
action: TemplateAction,
},
Expose {
jail: String,
#[arg(short = 'p', long)]
port: u16,
#[arg(short, long)]
internal: Option<u16>,
#[arg(long, default_value = "tcp")]
proto: String,
#[arg(short = 'I', long)]
bind_ip: Option<String>,
},
Ports {
jail: Option<String>,
},
Unexpose {
jail: String,
},
Cleanup {
jail: String,
#[arg(short, long)]
force: bool,
},
Export {
jail: String,
#[arg(short, long)]
output: Option<PathBuf>,
#[arg(long)]
zfs_send: bool,
},
Import {
file: PathBuf,
#[arg(short, long)]
name: Option<String>,
#[arg(long)]
force: bool,
},
Snapshot {
#[command(subcommand)]
action: SnapshotAction,
},
Clone {
source: String,
name: String,
},
Completion {
#[arg(value_enum)]
shell: Shell,
},
Supervise,
Logs {
jail: String,
#[arg(short = 'f', long)]
follow: bool,
#[arg(short = 'n', long, default_value = "100")]
lines: usize,
},
}
fn parse_key_val(s: &str) -> Result<(String, String), String> {
let pos = s
.find('=')
.ok_or_else(|| format!("invalid KEY=VALUE: no `=` found in `{s}`"))?;
Ok((s[..pos].to_string(), s[pos + 1..].to_string()))
}
#[derive(Subcommand)]
pub enum TemplateAction {
List,
Inspect {
template: String,
},
Validate {
#[arg(default_value = "Jailfile")]
file: PathBuf,
},
}
#[derive(Subcommand)]
pub enum ReleasesAction {
List,
Delete {
release: String,
},
Verify {
release: String,
},
}
#[derive(Subcommand)]
pub enum SnapshotAction {
Create {
jail: String,
name: Option<String>,
},
List {
jail: String,
#[arg(long)]
json: bool,
},
Rollback {
jail: String,
snapshot: String,
#[arg(short, long)]
force: bool,
},
Delete {
jail: String,
snapshot: String,
},
}
#[derive(Subcommand)]
pub enum ArmadaAction {
Init {
#[arg(short, long, default_value = "blackship.toml")]
file: PathBuf,
#[arg(short = 'y', long)]
force: bool,
},
Up {
#[arg(short, long)]
detach: bool,
jails: Vec<String>,
#[arg(long)]
build: bool,
#[arg(long)]
no_build: bool,
#[arg(long)]
dry_run: bool,
},
Down {
jails: Vec<String>,
#[arg(long)]
dry_run: bool,
},
Build {
jails: Vec<String>,
#[arg(long)]
dry_run: bool,
},
Ps {
#[arg(long)]
json: bool,
},
Config {
#[arg(long)]
show: bool,
},
}
#[derive(Subcommand)]
pub enum NetworkAction {
Create {
name: String,
#[arg(short, long)]
subnet: String,
#[arg(short, long)]
gateway: Option<String>,
#[arg(short, long, default_value = "blackship0")]
bridge: String,
},
Destroy {
name: String,
#[arg(short, long)]
force: bool,
},
List,
Attach {
jail: String,
network: String,
#[arg(short, long)]
ip: Option<String>,
},
Detach {
jail: String,
network: String,
},
}
impl Cli {
pub fn parse_args() -> Self {
Self::parse()
}
pub fn generate_completion(shell: Shell) {
let mut cmd = Self::command();
clap_complete::generate(shell, &mut cmd, "blackship", &mut std::io::stdout());
}
}