Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cmd completion #113

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, prospectively a dynamic value hint would be useful.

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