Skip to content

Commit

Permalink
add generation of shell completion (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
fygge authored Aug 25, 2023
1 parent 6db9b4c commit a395ea7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
10 changes: 10 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 linkup-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ path = "src/main.rs"
[dependencies]
actix-web = "4.3.1"
clap = { version = "4.1.8", features = ["derive"] }
clap_complete = "4.4.0"
colored = "2"
ctrlc = { version = "3.0", features = ["termination"] }
daemonize = "0.5.0"
Expand Down
17 changes: 17 additions & 0 deletions linkup-cli/src/completion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::io::stdout;

use clap::{Command, CommandFactory};
use clap_complete::{generate, Generator, Shell};

use crate::{Cli, CliError};

pub fn completion(shell: &Option<Shell>) -> Result<(), CliError> {
if let Some(shell) = shell {
let mut cmd = Cli::command();
print_completions(shell, &mut cmd);
}
Ok(())
}
fn print_completions<G: Generator + Clone>(gen: &G, cmd: &mut Command) {
generate(gen.clone(), cmd, cmd.get_name().to_string(), &mut stdout());
}
23 changes: 21 additions & 2 deletions linkup-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::{env, fs, io::ErrorKind, path::PathBuf};

use clap::{Parser, Subcommand};
use clap_complete::Shell;
use thiserror::Error;

mod background_booting;
mod background_local_server;
mod background_tunnel;
mod completion;
mod local_config;
mod local_server;
mod remote_local;
Expand All @@ -15,6 +17,7 @@ mod start;
mod status;
mod stop;

use completion::completion;
use remote_local::{local, remote};
use reset::reset;
use start::start;
Expand Down Expand Up @@ -113,14 +116,24 @@ struct Cli {
enum Commands {
#[clap(about = "Start a new linkup session")]
Start {
#[arg(short, long)]
#[arg(
short,
long,
value_name = "CONFIG",
help = "Path to config file, overriding environment variable."
)]
config: Option<String>,
},
#[clap(about = "Stop a running linkup session")]
Stop {},
#[clap(about = "Reset a linkup session")]
Reset {
#[arg(short, long)]
#[arg(
short,
long,
value_name = "CONFIG",
help = "Path to config file, overriding environment variable."
)]
config: Option<String>,
},
#[clap(about = "Route session traffic to a local service")]
Expand All @@ -135,6 +148,11 @@ enum Commands {
#[arg(short, long)]
all: bool,
},
#[clap(about = "Generate completions for your shell")]
Completion {
#[arg(long, value_enum)]
shell: Option<Shell>,
},
}

fn main() -> Result<(), CliError> {
Expand All @@ -149,5 +167,6 @@ fn main() -> Result<(), CliError> {
Commands::Local { service_names } => local(service_names.clone()),
Commands::Remote { service_names } => remote(service_names.clone()),
Commands::Status { json, all } => status(*json, *all),
Commands::Completion { shell } => completion(shell),
}
}

0 comments on commit a395ea7

Please sign in to comment.