typos in docs & arg formatting

This commit is contained in:
17ms 2023-05-27 22:09:48 +03:00
parent 5ae514fea6
commit ba0678c149
2 changed files with 57 additions and 34 deletions

View File

@ -2,73 +2,96 @@ use std::{error::Error, net::SocketAddr, path::PathBuf};
use clap::{command, ArgGroup, Parser, Subcommand}; use clap::{command, ArgGroup, Parser, Subcommand};
use contego::parser::{addr_parser, dirpath_parser, filepath_parser}; use contego::{
client::Client,
parser::{addr_parser, dirpath_parser, filepath_parser},
server::Server,
util::{filepaths, metadata, Ip},
};
use env_logger::Env;
use tokio::sync::mpsc;
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
#[command(about, version)] #[command(about, version)]
struct Cli { struct Cli {
#[command(subcommand)] #[command(subcommand)]
command: Commands, command: Commands,
/// Suspend all output expect errors
#[clap(long, default_value_t = false)]
quiet: bool,
} }
#[derive(Debug, Subcommand)] #[derive(Debug, Subcommand)]
enum Commands { enum Commands {
/// Host fileserver instance by providing JSON file with paths or list of paths
#[clap(group(ArgGroup::new("input").required(true).args(&["infile", "files"])))] #[clap(group(ArgGroup::new("input").required(true).args(&["infile", "files"])))]
Host { Host {
/// Access key
#[clap(short = 'k', long)]
key: String,
/// Path to a source file (alternative to --files)
#[clap(short = 'i', long, value_parser = filepath_parser, conflicts_with = "files", group = "input")]
source: Option<PathBuf>,
/// Paths to shareable files (alternative to --source)
#[clap(short = 'f', long, num_args = 1.., value_parser = filepath_parser, conflicts_with = "infile", group = "input")]
files: Option<Vec<PathBuf>>,
/// Host port /// Host port
#[clap(short = 'p', long, default_value_t = 8080)] #[clap(short = 'p', long, default_value_t = 8080)]
port: u16, port: u16,
/// Use IPv6 instead of IPv4 /// IPv6 instead of IPv4
#[clap(short = '6', long, default_value_t = false)] #[clap(short = '6', long, default_value_t = false)]
ipv6: bool, ipv6: bool,
/// Path to the inputfile /// Transmit chunksize in bytes
#[clap(short = 'i', long, value_parser = filepath_parser, conflicts_with = "files", group = "input")]
infile: Option<PathBuf>,
/// Paths to the files
#[clap(short = 'f', long, num_args = 1.., value_parser = filepath_parser, conflicts_with = "infile", group = "input")]
files: Option<Vec<PathBuf>>,
/// Outgoing traffic chunksize in bytes
#[clap(short = 'c', long, default_value_t = 8192)] #[clap(short = 'c', long, default_value_t = 8192)]
chunksize: usize, chunksize: usize,
/// Host the files locally /// Host locally
#[clap(short = 'l', long, default_value_t = false)] #[clap(short = 'l', long, default_value_t = false)]
local: bool, local: bool,
}, },
/// Connect to hosted server by providing address, output folder and access key
Connect { Connect {
/// IP address of the server (IPv4 or IPv6) /// IP address of the instance
#[clap(short = 'a', long, value_parser = addr_parser)] #[clap(short = 'a', long, value_parser = addr_parser)]
addr: SocketAddr, addr: SocketAddr,
/// Path to the output folder /// Path to an output folder
#[clap(short = 'o', long, value_parser = dirpath_parser)] #[clap(short = 'o', long, value_parser = dirpath_parser)]
out: PathBuf, out: PathBuf,
/// Access key for the fileserver /// Access key
#[clap(short = 'k', long)] #[clap(short = 'k', long)]
key: String, key: String,
}, },
} }
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> { async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
// TODO: init logger with default level set to 'info' env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let _cli = Cli::parse(); let cli = Cli::parse();
//match cli.command { match cli.command {
// Commands::Host { Commands::Host {
// port, port,
// ipv6, ipv6,
// infile, source,
// files, files,
// chunksize, chunksize,
// local, local,
// } => {} key,
// Commands::Connect { addr, out, key } => {} } => {
//}; let files = filepaths(source, files)?;
let (metadata, index) = metadata(&files).await?;
let addr = match (local, ipv6) {
(true, _) => Ip::Local.fetch(port)?,
(false, true) => Ip::V6.fetch(port)?,
(false, false) => Ip::V4.fetch(port)?,
};
// TODO: handle shutdown signal
let (_tx, rx) = mpsc::channel::<()>(1);
let server = Server::new(addr, key, chunksize, metadata, index);
server.start(rx).await?;
}
Commands::Connect { addr, out, key } => {
let client = Client::new(addr, key, out);
client.connection().await?;
}
};
Ok(()) Ok(())
} }

View File

@ -50,10 +50,10 @@ impl FileInfo {
} }
} }
fn filepaths( pub fn filepaths(
infile: Option<PathBuf>, infile: Option<PathBuf>,
files: Option<Vec<PathBuf>>, files: Option<Vec<PathBuf>>,
) -> Result<Vec<PathBuf>, Box<dyn Error>> { ) -> Result<Vec<PathBuf>, Box<dyn Error + Send + Sync>> {
info!("Collecting filepaths"); info!("Collecting filepaths");
let mut filepaths = Vec::new(); let mut filepaths = Vec::new();