-
Notifications
You must be signed in to change notification settings - Fork 14
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 completions sub-command to generate shell completion scripts #592
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
#[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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beautiful comment, might need formatting tweaks depending on how it renders on the docs site. I believe we treat this as markdown. Testing now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. Working on a PR to this PR since my iteration cycle alone is a lot faster than me suggesting changes to you and you pushing commits and me testing them.