Skip to content

Commit

Permalink
Add initial completions support
Browse files Browse the repository at this point in the history
  • Loading branch information
blaggacao committed Aug 11, 2021
1 parent d721743 commit d7a9a5f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
12 changes: 12 additions & 0 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 @@ edition = "2018"

[dependencies]
clap = "3.0.0-beta.2"
clap_generate = "3.0.0-beta.2"
flexi_logger = "0.16"
fork = "0.1"
futures-util = "0.3.6"
Expand Down
42 changes: 38 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use std::collections::HashMap;
use std::io::{stdin, stdout, Write};

use clap::{Clap, ArgMatches, FromArgMatches};
use clap::{App, ArgEnum, Clap, IntoApp, ValueHint, ArgMatches, FromArgMatches};
use clap_generate::generators::{Bash, Elvish, Fish, PowerShell, Zsh};
use clap_generate::{generate, Generator};

use crate as deploy;

Expand All @@ -18,16 +20,31 @@ use std::process::Stdio;
use thiserror::Error;
use tokio::process::Command;


#[derive(ArgEnum, Debug, Clone, PartialEq)]
pub enum GeneratorChoice {
Bash,
Elvish,
Fish,
#[clap(name = "powershell")]
PowerShell,
Zsh,
}

/// Simple Rust rewrite of a simple Nix Flake deployment tool
#[derive(Clap, Debug, Clone)]
#[clap(version = "1.0", author = "Serokell <https://serokell.io/>")]
#[clap(name = "deploy", version = "1.0", author = "Serokell <https://serokell.io/>")]
pub struct Opts {
/// If provided, outputs the completion file for given shell
#[clap(long = "generate", arg_enum)]
generator: Option<GeneratorChoice>,

/// The flake to deploy
#[clap(group = "deploy")]
#[clap(group = "deploy", value_hint = ValueHint::DirPath)]
target: Option<String>,

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

fn print_completions<G: Generator>(app: &mut App) {
generate::<G, _>(app, app.get_name().to_string(), &mut stdout());
}

/// Returns if the available Nix installation supports flakes
async fn test_flake_support() -> Result<bool, std::io::Error> {
debug!("Checking for flake support");
Expand Down Expand Up @@ -619,6 +640,19 @@ pub async fn run(args: Option<&ArgMatches>) -> Result<(), RunError> {
deploy::LoggerType::Deploy,
)?;

if let Some(generator) = opts.generator {
let mut app = Opts::into_app();
info!("Generating completion file for {:?}...", generator);
match generator {
GeneratorChoice::Bash => print_completions::<Bash>(&mut app),
GeneratorChoice::Elvish => print_completions::<Elvish>(&mut app),
GeneratorChoice::Fish => print_completions::<Fish>(&mut app),
GeneratorChoice::PowerShell => print_completions::<PowerShell>(&mut app),
GeneratorChoice::Zsh => print_completions::<Zsh>(&mut app),
};
return Ok(())
}

let deploys = opts
.clone()
.targets
Expand Down

0 comments on commit d7a9a5f

Please sign in to comment.