mod auth;
mod batch;
mod commands;
mod output;
mod pool;
mod target;
use std::path::PathBuf;
use anyhow::Result;
use clap::{Parser, Subcommand};
use crate::auth::AuthArgs;
use crate::target::Target;
#[derive(Parser)]
#[command(
name = "smb2",
version,
about = "List, move, and delete files on an SMB share without mounting it",
after_help = "Targets look like //host/share/path. Passwords come from --password-command, \
SMB2_PASS, or a prompt."
)]
struct Cli {
#[command(flatten)]
auth: AuthArgs,
#[arg(short = 'j', long, default_value_t = 8, global = true)]
concurrency: usize,
#[arg(long, global = true)]
json: bool,
#[arg(long, global = true)]
dry_run: bool,
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
Ls {
target: String,
#[arg(short = 'l', long)]
long: bool,
#[arg(short = 'R', long)]
recursive: bool,
},
Stat {
targets: Vec<String>,
#[command(flatten)]
input: batch::BatchInput,
},
Mkdir {
targets: Vec<String>,
#[command(flatten)]
input: batch::BatchInput,
#[arg(short = 'p', long)]
parents: bool,
},
Rm {
targets: Vec<String>,
#[command(flatten)]
input: batch::BatchInput,
},
Rmdir {
targets: Vec<String>,
#[command(flatten)]
input: batch::BatchInput,
},
Mv {
targets: Vec<String>,
#[arg(long, value_name = "FILE")]
from_file: Option<String>,
},
Cat { target: String },
Get {
target: String,
destination: Option<String>,
},
Put {
source: PathBuf,
target: String,
},
Df { target: String },
Shares {
host: String,
},
}
#[tokio::main]
async fn main() -> Result<()> {
env_logger::init();
let cli = Cli::parse();
let credentials = cli.auth.resolve()?;
let concurrency = cli.concurrency.max(1);
match &cli.command {
Commands::Ls {
target,
long,
recursive,
} => {
let target = Target::parse(target)?;
commands::meta::ls(&target, &credentials, *long, *recursive, cli.json).await
}
Commands::Stat { targets, input } => {
let (base, paths) = input.collect(targets)?;
commands::meta::stat(&base, &credentials, paths, concurrency, cli.json).await
}
Commands::Mkdir {
targets,
input,
parents,
} => {
commands::mutate::mkdir(
targets,
input,
&credentials,
concurrency,
*parents,
cli.dry_run,
)
.await
}
Commands::Rm { targets, input } => {
commands::mutate::rm(targets, input, &credentials, concurrency, cli.dry_run).await
}
Commands::Rmdir { targets, input } => {
commands::mutate::rmdir(targets, input, &credentials, concurrency, cli.dry_run).await
}
Commands::Mv { targets, from_file } => {
commands::mutate::mv(
targets,
from_file.as_deref(),
&credentials,
concurrency,
cli.dry_run,
)
.await
}
Commands::Cat { target } => {
commands::transfer::cat(&Target::parse(target)?, &credentials).await
}
Commands::Get {
target,
destination,
} => {
commands::transfer::get(
&Target::parse(target)?,
&credentials,
destination.as_deref(),
)
.await
}
Commands::Put { source, target } => {
commands::transfer::put(source, &Target::parse(target)?, &credentials).await
}
Commands::Df { target } => {
commands::meta::df(&Target::parse(target)?, &credentials, cli.json).await
}
Commands::Shares { host } => {
let (host, port) = parse_host(host);
commands::meta::shares(&host, port, &credentials, cli.json).await
}
}
}
fn parse_host(input: &str) -> (String, u16) {
let trimmed = input
.trim_start_matches("smb://")
.trim_start_matches("//")
.trim_end_matches('/');
match trimmed.rsplit_once(':') {
Some((host, port)) => (host.to_string(), port.parse().unwrap_or(445)),
None => (trimmed.to_string(), 445),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn verifies_the_cli_definition() {
use clap::CommandFactory;
Cli::command().debug_assert();
}
#[test]
fn parses_host_forms() {
assert_eq!(parse_host("raspi"), ("raspi".to_string(), 445));
assert_eq!(parse_host("//raspi/"), ("raspi".to_string(), 445));
assert_eq!(parse_host("raspi:4450"), ("raspi".to_string(), 4450));
}
}