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

feat: fuelup completions #293

Merged
merged 6 commits into from
Nov 29, 2022
Merged
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
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.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ path = "src/main.rs"
[dependencies]
anyhow = "1"
clap = { version = "3.2", features = ["cargo", "derive", "env"] }
clap_complete = "3.2"
component = { path = "component" }
dirs = "4"
flate2 = "1"
Expand Down
108 changes: 108 additions & 0 deletions docs/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,111 @@
_fuelup_ has a [TOML](https://github.com/toml-lang/toml) settings file at
`.fuelup/settings.toml`. The schema for this file is not part of the public
interface for _fuelup_ - the fuelup CLI should be used to query and set settings.

## Generate Shell Completions

Enable tab completion for Bash, Fish, Zsh, or PowerShell. The script prints output on `stdout`,
allowing one to re-direct the output to the file of their choosing. Where you place the file will
depend on which shell, and which operating system you are using. Your particular configuration may
also determine where these scripts need to be placed.

Here are some common set ups for the supported shells under Unix and similar operating systems
(such as GNU/Linux). For these settings to take effect, you may have to log out and log back in to
your shell session.

### BASH

Completion files are commonly stored in `/etc/bash_completion.d/` for system-wide commands, but can
be stored in `~/.local/share/bash-completion/completions` for user-specific commands.

```sh
mkdir -p ~/.local/share/bash-completion/completions
fuelup completions --shell=bash >> ~/.local/share/bash-completion/completions/fuelup
```

## BASH (macOS/Homebrew)

Homebrew stores bash completion files within the Homebrew directory. With the `bash-completion` brew
formula installed.

```sh
mkdir -p $(brew --prefix)/etc/bash_completion.d
fuelup completions --shell=bash > $(brew --prefix)/etc/bash_completion.d/fuelup.bash-completion
```

### FISH

Fish completion files are commonly stored in `$HOME/.config/fish/completions`.

```sh
mkdir -p ~/.config/fish/completions
fuelup completions --shell=fish > ~/.config/fish/completions/fuelup.fish
```

### ZSH

ZSH completions are commonly stored in any directory listed in your `$fpath` variable. To use these
completions, you must either add the generated script to one of those directories, or add your own
to this list.

Adding a custom directory is often the safest bet if you are unsure of which directory to use. First
create the directory; for this example we'll create a hidden directory inside our `$HOME` directory:

```sh
mkdir ~/.zfunc
```

Then add the following lines to your `.zshrc` just before `compinit`:

```sh
fpath+=~/.zfunc
```

Now you can install the completions script using the following command:

```sh
fuelup completions --shell=zsh > ~/.zfunc/_fuelup
```

### POWERSHELL

The powershell completion scripts require PowerShell v5.0+ (which comes with Windows 10, but can be
downloaded separately for windows 7 or 8.1).

First, check if a profile has already been set

```sh
Test-Path $profile
```

If the above command returns `False` run the following

```sh
New-Item -path $profile -type file -force
```

Now open the file provided by `$profile` (if you used the `New-Item` command it will be
`${env:USERPROFILE}\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1`).

Next, we either save the completions file into our profile, or into a separate file and source it
inside our profile. To save the completions into our profile simply use

```sh
fuelup completions --shell=powershell >> ${env:USERPROFILE}\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
```

### ELVISH

Elvish completions are commonly stored in [`epm`](https://elv.sh/ref/epm.html#the-epm-managed-directory)-managed
directories.

```sh
fuelup completions --shell=elvish > ~/.local/share/elvish/lib/fuelup.elv
```

Then in [`rc.elv`](https://elv.sh/ref/command.html#rc-file), add the following line to activate the
generated completions.

```sh
use fuelup
```
1 change: 1 addition & 0 deletions docs/src/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
| `fuelup check` | Checks for updates to distributable toolchains |
| `fuelup show` | Shows the active toolchain and installed toolchains, as well as the host and fuelup home |
| `fuelup toolchain help` | Shows the `help` page for a subcommand (like `toolchain`) |
| `fuelup completions --shell=zsh` | Generate shell completions for ZSH |

[forc]: https://github.com/FuelLabs/sway/tree/master/forc
[fuel-core]: https://github.com/FuelLabs/fuel-core
2 changes: 1 addition & 1 deletion fuelup-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ main() {
ignore rmdir "$_dir"

printf '\n'
printf '%s\n' "fuelup ${_fuelup_version} has been installed in $FUELUP_DIR/bin. To fetch the latest toolchain containing the forc and fuel-core binaries, run 'fuelup toolchain install latest'." 1>&2
printf '%s\n' "fuelup ${_fuelup_version} has been installed in $FUELUP_DIR/bin. To fetch the latest toolchain containing the forc and fuel-core binaries, run 'fuelup toolchain install latest'. To generate completions for your shell, run 'fuelup completions --shell=SHELL'." 1>&2
Copy link
Contributor

Choose a reason for hiding this comment

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

To generate completions for your shell, run 'fuelup completions --shell=SHELL

It would be great if this instruction, along with a short description, is also included within the fuelup book, perhaps under the Configuration chapter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

didn't realise that the book is also in this repo
will add some instructions


if [ "$allow_modify" = "yes" ]; then
if echo "$PATH" | grep -q "$FUELUP_DIR/bin"; then
Expand Down
21 changes: 21 additions & 0 deletions src/commands/completions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use anyhow::Result;
use clap::Parser;
use clap_complete::Shell;

use crate::ops::fuelup_completions;

/// Generate tab-completion scripts for your shell
#[derive(Debug, Parser)]
pub struct CompletionsCommand {
/// Specify shell to enable tab-completion for
///
/// [possible values: zsh, bash, fish, powershell, elvish]
#[clap(short = 'S', long)]
pub shell: Shell,
}

pub fn exec(command: CompletionsCommand) -> Result<()> {
fuelup_completions::completions(command)?;

Ok(())
}
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod check;
pub mod completions;
pub mod component;
pub mod default;
pub mod fuelup;
Expand Down
8 changes: 6 additions & 2 deletions src/fuelup_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use anyhow::Result;
use clap::Parser;

use crate::commands::show::ShowCommand;
use crate::commands::{check, component, default, fuelup, show, toolchain, update};
use crate::commands::{check, completions, component, default, fuelup, show, toolchain, update};

use crate::commands::check::CheckCommand;
use crate::commands::completions::CompletionsCommand;
use crate::commands::component::ComponentCommand;
use crate::commands::default::DefaultCommand;
use crate::commands::fuelup::FuelupCommand;
Expand All @@ -13,7 +14,7 @@ use crate::commands::update::UpdateCommand;

#[derive(Debug, Parser)]
#[clap(name = "fuelup", about = "Fuel Toolchain Manager", version)]
struct Cli {
pub struct Cli {
#[clap(subcommand)]
command: Commands,
}
Expand All @@ -22,6 +23,8 @@ struct Cli {
enum Commands {
/// Check for updates to Fuel toolchains and fuelup
Check(CheckCommand),
/// Generate shell completions
Completions(CompletionsCommand),
/// Add or remove components from the currently active toolchain
#[clap(subcommand)]
Component(ComponentCommand),
Expand All @@ -44,6 +47,7 @@ pub fn fuelup_cli() -> Result<()> {

match cli.command {
Commands::Check(command) => check::exec(command),
Commands::Completions(command) => completions::exec(command),
Commands::Component(command) => component::exec(command),
Commands::Default_(command) => default::exec(command),
Commands::Fuelup(command) => match command {
Expand Down
11 changes: 11 additions & 0 deletions src/ops/fuelup_completions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use crate::commands::completions::CompletionsCommand;
use anyhow::Result;
use clap::CommandFactory;
use clap_complete::generate;

pub fn completions(command: CompletionsCommand) -> Result<()> {
let mut cmd = super::super::fuelup_cli::Cli::command();
let bin_name = cmd.get_name().to_string();
generate(command.shell, &mut cmd, bin_name, &mut std::io::stdout());
Ok(())
}
1 change: 1 addition & 0 deletions src/ops/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod fuelup_check;
pub mod fuelup_completions;
pub mod fuelup_component;
pub mod fuelup_default;
pub mod fuelup_self;
Expand Down
14 changes: 14 additions & 0 deletions tests/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,3 +536,17 @@ You may create a custom toolchain using 'fuelup toolchain new <toolchain>'.
})?;
Ok(())
}

#[test]
fn fuelup_completions() -> Result<()> {
testcfg::setup(FuelupState::LatestToolchainInstalled, &|cfg| {
let shells = ["zsh", "bash", "fish", "powershell", "elvish"];
for shell in shells {
let output = cfg.fuelup(&["completions", "--shell", shell]);

assert!(output.status.success());
}
})?;

Ok(())
}