4

The canonical-livepatch snap does not feature autocompletion for its subcommands. As per the help page, the valid subcommands are:

$ canonical-livepatch help
...
COMMANDS:
   config - configure livepatching on the machine
   disable - disable livepatching on the machine
   enable - enable livepatching on the machine
   help - display help
   kernel-upgrade-required - indicate whether a kernel upgrade is required
   refresh - immediately download and apply any available livepatch
   status - show kernel's livepatch status

Is there an easy way to add autocompletion for the subcommands in the canonical-livepatch snap that works in Bash, without having to modify the snap itself?

A little detail is that the autocompletion should also work for the snap alias livepatch I have for the command.

$ snap aliases
Command              Alias      Notes
canonical-livepatch  livepatch  manual
lxd.lxc              lxc        -

1 Answer 1

4

This can be done by creating the following autocompletion script (I've called it livepatch-completion):

# canonical-livepatch completion script

_livepatch_completion()
{
  # Only autocomplete for first subcommand
  if [ "${#COMP_WORDS[@]}" != "2" ]; then
    return
  fi

  # Define the list of words that will autocomplete and display on double-tab
  COMPREPLY=($(compgen -W "config disable enable help kernel-upgrade-required refresh status" "${COMP_WORDS[1]}"))
}

# Initialize the completion function for both the original command and the alias livepatch    
complete -F _livepatch_completion canonical-livepatch livepatch

Place the script anywhere you like (for instance ~/.bash_completion).

Source the autocompletion script by adding this line to your ~/.bashrc (or any other file that is sourced when you start the terminal):

source ~/.bash_completion/livepatch-completion

This may not be the most advanced solution, but it works for simply autocompleting the subcommands for any other command, including snaps and aliases.

4
  • 2
    I never realized it didn't work with autocompletion, as I always just typed in the status command, etc. But now I know, and this script worked great. Thank you! +1 :)
    – Terrance
    Mar 27, 2022 at 0:13
  • ~/.bash_completion should be a file, it is sourced by /usr/share/bash-completion/bash_completion, so no need to source it.
    – user986805
    Mar 27, 2022 at 17:27
  • if you like to autoload a user completion file instead, much like /etc/bash_completion.d you can use the $HOME/.local/share/bash-completion/completions directory.
    – user986805
    Mar 27, 2022 at 17:37
  • Thanks for the insights. I'm not going to edit the answer further, since I believe you can put the script wherever you want and source it. However, you're more than welcome to edit if you believe another approach is more correct. Thanks! 👍 Mar 27, 2022 at 20:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .