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

Overhaul shell completion docs #3961

Merged
merged 1 commit into from
Feb 6, 2024
Merged
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
105 changes: 84 additions & 21 deletions docs/shell-completion.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,108 @@
# Enabling Shell Completion

Generate the k0s completion script using the `k0s completion <shell_name>` command, for Bash, Zsh, fish, or PowerShell.
## Introduction

Sourcing the completion script in your shell enables k0s autocompletion.
Shell completion enhances the user experience by providing auto-completion for
commands in the terminal. K0s supports shell completion for the following
shells:

## Bash
- `bash`, [GNU Bash](https://www.gnu.org/software/bash/)
- `zsh`, [the Z-shell](https://www.zsh.org/)
- `fish`, [the friendly interactive shell]((https://fishshell.com/))
- `powershell`, [Microsoft PowerShell](https://learn.microsoft.com/powershell/)

```shell
echo 'source <(k0s completion bash)' >>~/.bashrc
## General Usage

To generate a completion script for your shell, use the following command: `k0s
completion <shell_name>`. Sourcing the completion script in your shell enables
k0s autocompletion.

## bash

One-shot usage: `source <(k0s completion bash)`.

This is a recipe to load completions for each new shell. Adjust to your personal
needs:

```bash
mkdir ~/.bash_completion.d
k0s completion bash >~/.bash_completion.d/k0s

cat <<'EOF' >~/.bashrc
Copy link
Contributor

Choose a reason for hiding this comment

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

how about something like this?

grep -q .bash_completion.d .bashrc || cat <<'EOF'

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried to find some references about the idiomatic way of including user-managed bash shell completions, but I didn't really find anything. The bash_completion.d stuff seems to be used, but there doesn't seem to be "the way of doing it". That's why I tried to give an example with the caveat "adjust to your personal needs". Checking if the folder has already been set up in .bashrc or some other place is not a piece of cake. That's why I opted for not doing it.

If you think checking would be better, then I'd probably change this to some descriptive block where to put the snippet, similar to the examples for zsh.

WDYT?

for compFile in ~/.bash_completion.d/*; do
[ ! -f "$compFile" ] || source -- "$compFile"
done
unset compFile
EOF
```

To load completions for each session, execute once:
Then restart the shell or source `~/.bashrc`.

```shell
k0s completion bash > /etc/bash_completion.d/k0s
## zsh

One-shot usage: `source <(k0s completion bash)`.

Following a recipe to load completions for each new shell. Adjust to your
personal needs. If shell completion is not already enabled in your zsh
environment you will need to enable it:

```zsh
echo "autoload -Uz compinit; compinit" >>~/.zshrc
```

## Zsh
Place the completion script in a custom `site-functions` folder:

If shell completion is not already enabled in Zsh environment you will need to enable it:
```zsh
mkdir -p -- ~/.local/share/zsh/site-functions
k0s completion zsh >~/.local/share/zsh/site-functions/_k0s
```

```shell
echo "autoload -U compinit; compinit" >> ~/.zshrc
Edit `~/.zshrc` and add the line `fpath+=(~/.local/share/zsh/site-functions)`
somewhere before `compinit` is called. After that, restart the shell.

When using [Oh My ZSH!], you can create a [custom plugin]:

```zsh
mkdir -- "$ZSH_CUSTOM/plugins/k0s"
cat <<'EOF' >"$ZSH_CUSTOM/plugins/k0s/k0s.plugin.zsh"
k0s completion zsh >| "$ZSH_CACHE_DIR/completions/_k0s" &|
EOF
omz plugin enable k0s
```

To load completions for each session, execute once:
Then restart the shell.

[Oh My ZSH!]: https://ohmyz.sh/
[custom plugin]: https://github.com/ohmyzsh/ohmyzsh/wiki/Customization#overriding-and-adding-plugins

## fish

One-shot usage: `k0s completion fish | source`.

This is a recipe to load completions for each new shell. Adjust to your personal
needs:

```shell
k0s completion zsh > "${fpath[1]}/_k0s"
mkdir -p -- "${XDG_CONFIG_HOME:-$HOME/.config}/fish/completions"
k0s completion fish >"${XDG_CONFIG_HOME:-$HOME/.config}/fish/completions/k0s.fish"
```

**Note**: You must start a new shell for the setup to take effect.
Then restart the shell.

## Fish
## powershell

```shell
k0s completion fish | source
Save the completion script into a file:

```powershell
k0s completion powershell > C:\path\to\k0s.ps1
```

To load completions for each session, execute once:
You can import it like so:

```shell
k0s completion fish > ~/.config/fish/completions/k0s.fish
```powershell
Import-Module C:\path\to\k0s.ps1
```

To automatically load the module for each new shell session, add the above line
to your shell profile. You can find the path to your profile via `Write-Output
$profile`.
Loading