2022-05-26 17:27:53 +00:00
|
|
|
from borgmatic.commands import arguments
|
2023-04-28 02:44:11 +00:00
|
|
|
import shlex
|
2022-05-26 17:27:53 +00:00
|
|
|
|
2023-04-28 02:31:42 +00:00
|
|
|
def upgrade_message(language: str, upgrade_command: str, completion_file: str):
|
|
|
|
return f'''
|
|
|
|
Your {language} completions script is from a different version of borgmatic than is
|
2022-05-26 17:27:53 +00:00
|
|
|
currently installed. Please upgrade your script so your completions match the
|
|
|
|
command-line flags in your installed borgmatic! Try this to upgrade:
|
|
|
|
|
2023-04-28 02:31:42 +00:00
|
|
|
{upgrade_command}
|
|
|
|
source {completion_file}
|
2022-05-26 17:27:53 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
def parser_flags(parser):
|
|
|
|
'''
|
|
|
|
Given an argparse.ArgumentParser instance, return its argument flags in a space-separated
|
|
|
|
string.
|
|
|
|
'''
|
|
|
|
return ' '.join(option for action in parser._actions for option in action.option_strings)
|
|
|
|
|
|
|
|
|
|
|
|
def bash_completion():
|
|
|
|
'''
|
|
|
|
Return a bash completion script for the borgmatic command. Produce this by introspecting
|
|
|
|
borgmatic's command-line argument parsers.
|
|
|
|
'''
|
|
|
|
top_level_parser, subparsers = arguments.make_parsers()
|
|
|
|
global_flags = parser_flags(top_level_parser)
|
|
|
|
actions = ' '.join(subparsers.choices.keys())
|
|
|
|
|
|
|
|
# Avert your eyes.
|
|
|
|
return '\n'.join(
|
|
|
|
(
|
|
|
|
'check_version() {',
|
2022-05-28 22:27:11 +00:00
|
|
|
' local this_script="$(cat "$BASH_SOURCE" 2> /dev/null)"',
|
|
|
|
' local installed_script="$(borgmatic --bash-completion 2> /dev/null)"',
|
|
|
|
' if [ "$this_script" != "$installed_script" ] && [ "$installed_script" != "" ];'
|
2023-04-28 02:31:42 +00:00
|
|
|
' then cat << EOF\n{}\nEOF'.format(upgrade_message('bash', 'sudo sh -c "borgmatic --bash-completion > $BASH_SOURCE"', '$BASH_SOURCE')),
|
2022-05-26 17:27:53 +00:00
|
|
|
' fi',
|
|
|
|
'}',
|
|
|
|
'complete_borgmatic() {',
|
|
|
|
)
|
|
|
|
+ tuple(
|
|
|
|
''' if [[ " ${COMP_WORDS[*]} " =~ " %s " ]]; then
|
|
|
|
COMPREPLY=($(compgen -W "%s %s %s" -- "${COMP_WORDS[COMP_CWORD]}"))
|
|
|
|
return 0
|
|
|
|
fi'''
|
|
|
|
% (action, parser_flags(subparser), actions, global_flags)
|
|
|
|
for action, subparser in subparsers.choices.items()
|
|
|
|
)
|
|
|
|
+ (
|
2023-03-24 06:11:14 +00:00
|
|
|
' COMPREPLY=($(compgen -W "%s %s" -- "${COMP_WORDS[COMP_CWORD]}"))' # noqa: FS003
|
2022-05-26 17:27:53 +00:00
|
|
|
% (actions, global_flags),
|
|
|
|
' (check_version &)',
|
|
|
|
'}',
|
2022-05-29 17:49:33 +00:00
|
|
|
'\ncomplete -o bashdefault -o default -F complete_borgmatic borgmatic',
|
2022-05-26 17:27:53 +00:00
|
|
|
)
|
|
|
|
)
|
2023-04-28 01:46:13 +00:00
|
|
|
|
|
|
|
def fish_completion():
|
|
|
|
'''
|
|
|
|
Return a fish completion script for the borgmatic command. Produce this by introspecting
|
|
|
|
borgmatic's command-line argument parsers.
|
|
|
|
'''
|
|
|
|
top_level_parser, subparsers = arguments.make_parsers()
|
|
|
|
|
|
|
|
# Avert your eyes.
|
|
|
|
return '\n'.join(
|
|
|
|
(
|
|
|
|
'function __borgmatic_check_version',
|
2023-04-28 03:05:17 +00:00
|
|
|
' set this_filename (status current-filename)',
|
|
|
|
' set this_script (cat $this_filename 2> /dev/null)',
|
2023-04-28 02:31:42 +00:00
|
|
|
' set installed_script (borgmatic --fish-completion 2> /dev/null)',
|
2023-04-28 01:46:13 +00:00
|
|
|
' if [ "$this_script" != "$installed_script" ] && [ "$installed_script" != "" ]',
|
2023-04-28 03:05:17 +00:00
|
|
|
' echo "{}"'.format(upgrade_message('fish', 'borgmatic --fish-completion | sudo tee $this_filename', '$this_filename')),
|
2023-04-28 01:46:13 +00:00
|
|
|
' end',
|
|
|
|
'end',
|
2023-04-28 02:58:22 +00:00
|
|
|
'__borgmatic_check_version &',
|
2023-04-28 02:44:11 +00:00
|
|
|
) + tuple(
|
2023-04-28 02:58:22 +00:00
|
|
|
'''complete -c borgmatic -a '%s' -d %s -f'''
|
2023-04-28 04:31:53 +00:00
|
|
|
% (actionStr, shlex.quote(subparser.description))
|
|
|
|
for actionStr, subparser in subparsers.choices.items()
|
2023-04-28 02:58:22 +00:00
|
|
|
) + tuple(
|
|
|
|
'''complete -c borgmatic -a '%s' -d %s -f'''
|
|
|
|
% (option, shlex.quote(action.help))
|
|
|
|
for action in top_level_parser._actions
|
|
|
|
for option in action.option_strings
|
2023-04-28 04:31:53 +00:00
|
|
|
) + tuple(
|
|
|
|
'''complete -c borgmatic -a '%s' -d %s -f -n "__fish_seen_subcommand_from %s"'''
|
|
|
|
% (option, shlex.quote(action.help), actionStr)
|
|
|
|
for actionStr, subparser in subparsers.choices.items()
|
|
|
|
for action in subparser._actions
|
|
|
|
for option in action.option_strings
|
2023-04-28 02:44:11 +00:00
|
|
|
)
|
|
|
|
)
|