Skip to content

Commit

Permalink
feat: add completions (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Jul 6, 2022
1 parent 140a360 commit 76e967f
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 14 deletions.
26 changes: 18 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ keywords = ["static", "file", "server", "webdav", "cli"]

[dependencies]
clap = { version = "3", default-features = false, features = ["std", "wrap_help"] }
clap_complete = "3"
chrono = "0.4"
tokio = { version = "1", features = ["rt-multi-thread", "macros", "fs", "io-util", "signal"]}
tokio-util = { version = "0.7", features = ["io-util"] }
Expand Down
16 changes: 12 additions & 4 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::{AppSettings, Arg, ArgMatches, Command};
use clap::{value_parser, AppSettings, Arg, ArgMatches, Command};
use clap_complete::{generate, Generator, Shell};
#[cfg(feature = "tls")]
use rustls::{Certificate, PrivateKey};
use std::env;
Expand All @@ -11,7 +12,7 @@ use crate::auth::AuthMethod;
use crate::tls::{load_certs, load_private_key};
use crate::BoxResult;

fn app() -> Command<'static> {
pub fn build_cli() -> Command<'static> {
let app = Command::new(env!("CARGO_CRATE_NAME"))
.version(env!("CARGO_PKG_VERSION"))
.author(env!("CARGO_PKG_AUTHORS"))
Expand Down Expand Up @@ -118,6 +119,13 @@ fn app() -> Command<'static> {
Arg::new("render-spa")
.long("render-spa")
.help("Serve SPA(Single Page Application)"),
)
.arg(
Arg::new("completions")
.long("completions")
.value_name("shell")
.value_parser(value_parser!(Shell))
.help("Print shell completion script for <shell>"),
);

#[cfg(feature = "tls")]
Expand All @@ -138,8 +146,8 @@ fn app() -> Command<'static> {
app
}

pub fn matches() -> ArgMatches {
app().get_matches()
pub fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout());
}

#[derive(Debug)]
Expand Down
12 changes: 10 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod utils;
#[macro_use]
extern crate log;

use crate::args::{matches, Args};
use crate::args::{build_cli, print_completions, Args};
use crate::server::{Request, Server};
#[cfg(feature = "tls")]
use crate::tls::{TlsAcceptor, TlsStream};
Expand All @@ -19,6 +19,7 @@ use std::net::{IpAddr, SocketAddr, TcpListener as StdTcpListener};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

use clap_complete::Shell;
use futures::future::join_all;
use tokio::net::TcpListener;
use tokio::task::JoinHandle;
Expand All @@ -37,7 +38,14 @@ async fn main() {

async fn run() -> BoxResult<()> {
logger::init().map_err(|e| format!("Failed to init logger, {}", e))?;
let args = Args::parse(matches())?;
let cmd = build_cli();
let matches = cmd.get_matches();
if let Some(generator) = matches.get_one::<Shell>("completions") {
let mut cmd = build_cli();
print_completions(*generator, &mut cmd);
return Ok(());
}
let args = Args::parse(matches)?;
let args = Arc::new(args);
let running = Arc::new(AtomicBool::new(true));
let handles = serve(args.clone(), running.clone())?;
Expand Down
2 changes: 2 additions & 0 deletions tests/args.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Run file server with different args

mod fixtures;
mod utils;

Expand Down
32 changes: 32 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//! Run cli with different args, not starting a server

mod fixtures;

use assert_cmd::prelude::*;
use clap::ValueEnum;
use clap_complete::Shell;
use fixtures::Error;
use std::process::Command;

#[test]
/// Show help and exit.
fn help_shows() -> Result<(), Error> {
Command::cargo_bin("dufs")?.arg("-h").assert().success();

Ok(())
}

#[test]
/// Print completions and exit.
fn print_completions() -> Result<(), Error> {
// let shell_enums = EnumValueParser::<Shell>::new();
for shell in Shell::value_variants() {
Command::cargo_bin("dufs")?
.arg("--completions")
.arg(shell.to_string())
.assert()
.success();
}

Ok(())
}

0 comments on commit 76e967f

Please sign in to comment.