Skip to content

Commit

Permalink
feat: shell completions (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
XyLyXyRR authored Oct 21, 2022
1 parent 7c05afc commit 9c83baf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 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 cargo-shuttle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ cargo-edit = { version = "0.10.4", features = ["cli"] }
cargo_metadata = "0.15.0"
chrono = "0.4.22"
clap = { version = "3.2.17", features = ["derive", "env"] }
clap_complete = "3.2.5"
crossbeam-channel = "0.5.6"
crossterm = "0.25.0"
dirs = "4.0.0"
Expand Down
10 changes: 10 additions & 0 deletions cargo-shuttle/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
};

use clap::Parser;
use clap_complete::Shell;
use shuttle_common::project::ProjectName;
use uuid::Uuid;

Expand Down Expand Up @@ -56,6 +57,15 @@ pub enum Command {
Deployment(DeploymentCommand),
/// create a new shuttle service
Init(InitArgs),
/// generate shell completions
Generate {
/// which shell
#[clap(short, long, env, default_value_t = Shell::Bash)]
shell: Shell,
/// output to file or stdout by default
#[clap(short, long, env)]
output: Option<PathBuf>,
},
/// view the status of a shuttle service
Status,
/// view the logs of a deployment in this shuttle service
Expand Down
14 changes: 14 additions & 0 deletions cargo-shuttle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use cargo::core::resolver::CliFeatures;
use cargo::core::Workspace;
use cargo::ops::{PackageOpts, Packages};
use cargo_metadata::Message;
use clap::CommandFactory;
use clap_complete::{generate, Shell};
use config::RequestContext;
use crossterm::style::Stylize;
use factory::LocalFactory;
Expand Down Expand Up @@ -61,6 +63,7 @@ impl Shuttle {

match args.cmd {
Command::Init(init_args) => self.init(init_args).await,
Command::Generate { shell, output } => self.complete(shell, output).await,
Command::Login(login_args) => self.login(login_args).await,
Command::Run(run_args) => self.local_run(run_args).await,
need_client => {
Expand Down Expand Up @@ -175,6 +178,17 @@ impl Shuttle {
Ok(())
}

async fn complete(&self, shell: Shell, output: Option<PathBuf>) -> Result<()> {
let name = env!("CARGO_PKG_NAME");
let mut app = Command::command();
match output {
Some(v) => generate(shell, &mut app, name, &mut File::create(v)?),
None => generate(shell, &mut app, name, &mut stdout()),
};

Ok(())
}

async fn status(&self, client: &Client) -> Result<()> {
let summary = client.get_service_summary(self.ctx.project_name()).await?;

Expand Down

0 comments on commit 9c83baf

Please sign in to comment.