Skip to content

Commit

Permalink
Add completions sub-command to generate shell completion scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
citrus-it committed Mar 12, 2024
1 parent 7726222 commit 9e7c633
Show file tree
Hide file tree
Showing 4 changed files with 96 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 cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ async-trait = { workspace = true }
base64 = { workspace = true }
chrono = { workspace = true }
clap = { workspace = true }
clap_complete = "4.5.1"
dialoguer = { workspace = true }
dirs = { workspace = true }
env_logger = { workspace = true }
Expand Down
83 changes: 83 additions & 0 deletions cli/src/cmd_completion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

// Copyright 2024 Oxide Computer Company

use crate::RunnableCmd;
use anyhow::Result;
use async_trait::async_trait;
use clap::Parser;
use clap_complete::{generate, Shell};
use std::io;

/// Generate shell completion scripts for Oxide CLI commands.
///
/// This command generates scripts for various shells that can be used to
/// enable completion.
///
/// >>> bash
///
/// Add this to your `~/.bash_profile`:
///
/// eval "$(oxide completion -s bash)"
///
/// >>> zsh
///
/// Generate an `_oxide` completion script and put it somewhere in your
/// `$fpath`, for example:
///
/// oxide completion -s zsh > ~/.zfunc/_oxide
///
/// and check that you have the following lines in your `~/.zshrc`:
///
/// autoload -U compinit
/// compinit -i
///
/// >>> fish
///
/// Generate an `oxide.fish` completion script:
///
/// oxide completion -s fish > ~/.config/fish/completions/oxide.fish
///
/// >>> PowerShell
///
/// Open your profile script with:
///
/// mkdir -Path (Split-Path -Parent $profile)
/// notepad $profile
///
/// Add the following line and save the file:
///
/// Invoke-Expression -Command $(oxide completion -s powershell | Out-String)
///
/// >>> Elvish
///
/// Generate an `oxide.elv` completion script and put it in a module search
/// directory, for example:
///
/// oxide completion -s elvish > ~/.local/share/elvish/lib/oxide.elv
///
/// and import this by adding the following to `~/.config/elvish/rc.elv`
///
/// use oxide
#[derive(Parser, Debug, Clone)]
#[command(verbatim_doc_comment)]
#[command(name = "generate-completions")]
pub struct CmdCompletion {
/// Type of the shell
#[clap(short, long)]
shell: Shell,
}

#[async_trait]
impl RunnableCmd for CmdCompletion {
async fn run(&self, _ctx: &oxide::context::Context) -> Result<()> {
let cli = crate::make_cli();
let mut cmd = cli.command_take();
let name = cmd.get_name().to_string();
generate(self.shell, &mut cmd, name, &mut io::stdout());

Ok(())
}
}
2 changes: 2 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use oxide::types::{IdpMetadataSource, IpRange, Ipv4Range, Ipv6Range};
mod cli_builder;
mod cmd_api;
mod cmd_auth;
mod cmd_completion;
mod cmd_disk;
mod cmd_docs;
mod cmd_instance;
Expand Down Expand Up @@ -47,6 +48,7 @@ pub fn make_cli() -> NewCli<'static> {
.add_custom::<cmd_disk::CmdDiskImport>("disk import")
.add_custom::<cmd_instance::CmdInstanceSerial>("instance serial")
.add_custom::<cmd_instance::CmdInstanceFromImage>("instance from-image")
.add_custom::<cmd_completion::CmdCompletion>("completion")
}

#[tokio::main]
Expand Down

0 comments on commit 9e7c633

Please sign in to comment.