Skip to content

Commit 0b7fc45

Browse files
committed
Add initial completions support
1 parent fc7ab52 commit 0b7fc45

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

Cargo.lock

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ edition = "2018"
1212

1313
[dependencies]
1414
clap = "3.0.0-beta.2"
15+
clap_generate = "3.0.0-beta.2"
1516
flexi_logger = "0.16"
1617
fork = "0.1"
1718
futures-util = "0.3.6"

src/cli.rs

+38-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
use std::collections::HashMap;
77
use std::io::{stdin, stdout, Write};
88

9-
use clap::{Clap, ArgMatches, FromArgMatches};
9+
use clap::{App, ArgEnum, Clap, IntoApp, ValueHint, ArgMatches, FromArgMatches};
10+
use clap_generate::generators::{Bash, Elvish, Fish, PowerShell, Zsh};
11+
use clap_generate::{generate, Generator};
1012

1113
use crate as deploy;
1214

@@ -18,16 +20,31 @@ use std::process::Stdio;
1820
use thiserror::Error;
1921
use tokio::process::Command;
2022

23+
24+
#[derive(ArgEnum, Debug, Clone, PartialEq)]
25+
pub enum GeneratorChoice {
26+
Bash,
27+
Elvish,
28+
Fish,
29+
#[clap(name = "powershell")]
30+
PowerShell,
31+
Zsh,
32+
}
33+
2134
/// Simple Rust rewrite of a simple Nix Flake deployment tool
2235
#[derive(Clap, Debug, Clone)]
23-
#[clap(version = "1.0", author = "Serokell <https://serokell.io/>")]
36+
#[clap(name = "deploy", version = "1.0", author = "Serokell <https://serokell.io/>")]
2437
pub struct Opts {
38+
/// If provided, outputs the completion file for given shell
39+
#[clap(long = "generate", arg_enum)]
40+
generator: Option<GeneratorChoice>,
41+
2542
/// The flake to deploy
26-
#[clap(group = "deploy")]
43+
#[clap(group = "deploy", value_hint = ValueHint::DirPath)]
2744
target: Option<String>,
2845

2946
/// A list of flakes to deploy alternatively
30-
#[clap(long, group = "deploy")]
47+
#[clap(long, group = "deploy", value_hint = ValueHint::DirPath)]
3148
targets: Option<Vec<String>>,
3249
/// Check signatures when using `nix copy`
3350
#[clap(short, long)]
@@ -91,6 +108,10 @@ pub struct Opts {
91108
rollback_succeeded: Option<bool>,
92109
}
93110

111+
fn print_completions<G: Generator>(app: &mut App) {
112+
generate::<G, _>(app, app.get_name().to_string(), &mut stdout());
113+
}
114+
94115
/// Returns if the available Nix installation supports flakes
95116
async fn test_flake_support() -> Result<bool, std::io::Error> {
96117
debug!("Checking for flake support");
@@ -619,6 +640,19 @@ pub async fn run(args: Option<&ArgMatches>) -> Result<(), RunError> {
619640
deploy::LoggerType::Deploy,
620641
)?;
621642

643+
if let Some(generator) = opts.generator {
644+
let mut app = Opts::into_app();
645+
info!("Generating completion file for {:?}...", generator);
646+
match generator {
647+
GeneratorChoice::Bash => print_completions::<Bash>(&mut app),
648+
GeneratorChoice::Elvish => print_completions::<Elvish>(&mut app),
649+
GeneratorChoice::Fish => print_completions::<Fish>(&mut app),
650+
GeneratorChoice::PowerShell => print_completions::<PowerShell>(&mut app),
651+
GeneratorChoice::Zsh => print_completions::<Zsh>(&mut app),
652+
};
653+
return Ok(())
654+
}
655+
622656
fn maybe_default_target(target: Option<String>) -> Vec<String> {
623657
match (target, std::env::var("DEPLOY_RS_DEFAULT_FLAKE_ROOT"), std::env::var("DEPLOY_RS_DEFAULT_NODE")) {
624658
(None, _, _) => vec![".".to_string()],

0 commit comments

Comments
 (0)