From 3550be63f2e5cb556a0c080ca58b62836f052d84 Mon Sep 17 00:00:00 2001 From: Christophe Vuillot Date: Mon, 11 Mar 2019 14:53:40 +0100 Subject: [PATCH 1/4] Added a python script (yunohost_completion.py) which generates a bash completion file for the yunohost command based on yunohost.yml, in data/actionsmap Added the output of the script in data/bash-completion.d/yunohost_completion This is probably not the correct place for the script and the generation should be done at some other time and place also. --- data/actionsmap/yunohost_completion.py | 84 ++++++++++++++++++++++ data/bash-completion.d/yunohost_completion | 77 ++++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 data/actionsmap/yunohost_completion.py create mode 100644 data/bash-completion.d/yunohost_completion diff --git a/data/actionsmap/yunohost_completion.py b/data/actionsmap/yunohost_completion.py new file mode 100644 index 000000000..9b5472837 --- /dev/null +++ b/data/actionsmap/yunohost_completion.py @@ -0,0 +1,84 @@ +""" +Simple automated generation of a bash_completion file +for yunohost command from the actionsmap. + +Generates a bash completion file assuming the structure +`yunohost domain action` +adds `--help` at the end if one presses [tab] again. + +author: Christophe Vuillot +""" +import yaml + +ACTIONSMAP_FILE = 'yunohost.yml' +BASH_COMPLETION_FILE = '../bash-completion.d/yunohost_completion' + +with open(ACTIONSMAP_FILE, 'r') as stream: + + # Getting the dictionary containning what actions are possible per domain + OPTION_TREE = yaml.load(stream) + DOMAINS = [str for str in OPTION_TREE.keys() if not str.startswith('_')] + DOMAINS_STR = '"{}"'.format(' '.join(DOMAINS)) + ACTIONS_DICT = {} + for domain in DOMAINS: + ACTIONS = [str for str in OPTION_TREE[domain]['actions'].keys() + if not str.startswith('_')] + ACTIONS_STR = '"{}"'.format(' '.join(ACTIONS)) + ACTIONS_DICT[domain] = ACTIONS_STR + + with open(BASH_COMPLETION_FILE, 'w') as generated_file: + + # header of the file + generated_file.write('#\n') + generated_file.write('# completion for yunohost\n') + generated_file.write('# automatically generated from the actionsmap\n') + generated_file.write('#\n\n') + + # Start of the completion function + generated_file.write('_yunohost_completion()\n') + generated_file.write('{\n') + + # Defining local variable for previously and currently typed words + generated_file.write('\tlocal cur prev opts narg\n') + generated_file.write('\tCOMPREPLY=()\n\n') + generated_file.write('\t# the number of words already typed\n') + generated_file.write('\tnarg=${#COMP_WORDS[@]}\n\n') + generated_file.write('\t# the current word being typed\n') + generated_file.write('\tcur="${COMP_WORDS[COMP_CWORD]}"\n\n') + generated_file.write('\t# the last typed word\n') + generated_file.write('\tprev="${COMP_WORDS[COMP_CWORD-1]}"\n\n') + + # If one is currently typing a domain then match with the domain list + generated_file.write('\t# If one is currently typing a domain,\n') + generated_file.write('\t# match with domains\n') + generated_file.write('\tif [[ $narg == 2 ]]; then\n') + generated_file.write('\t\topts={}\n'.format(DOMAINS_STR)) + generated_file.write('\tfi\n\n') + + # If one is currently typing an action then match with the action list + # of the previously typed domain + generated_file.write('\t# If one already typed a domain,\n') + generated_file.write('\t# match the actions of that domain\n') + generated_file.write('\tif [[ $narg == 3 ]]; then\n') + for domain in DOMAINS: + generated_file.write('\t\tif [[ $prev == "{}" ]]; then\n'.format(domain)) + generated_file.write('\t\t\topts={}\n'.format(ACTIONS_DICT[domain])) + generated_file.write('\t\tfi\n') + generated_file.write('\tfi\n\n') + + # If both domain and action have been typed or the domain + # was not recognized propose --help (only once) + generated_file.write('\t# If no options were found propose --help\n') + generated_file.write('\tif [ -z "$opts" ]; then\n') + generated_file.write('\t\tif [[ $prev != "--help" ]]; then\n') + generated_file.write('\t\t\topts=( --help )\n') + generated_file.write('\t\tfi\n') + generated_file.write('\tfi\n') + + # generate the completion list from the possible options + generated_file.write('\tCOMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )\n') + generated_file.write('\treturn 0\n') + generated_file.write('}\n\n') + + # Add the function to bash completion + generated_file.write('complete -F _yunohost_completion yunohost') diff --git a/data/bash-completion.d/yunohost_completion b/data/bash-completion.d/yunohost_completion new file mode 100644 index 000000000..715073475 --- /dev/null +++ b/data/bash-completion.d/yunohost_completion @@ -0,0 +1,77 @@ +# +# completion for yunohost +# automatically generated from the actionsmap +# + +_yunohost_completion() +{ + local cur prev opts narg + COMPREPLY=() + + # the number of words already typed + narg=${#COMP_WORDS[@]} + + # the current word being typed + cur="${COMP_WORDS[COMP_CWORD]}" + + # the last typed word + prev="${COMP_WORDS[COMP_CWORD-1]}" + + # If one is currently typing a domain, + # match with domains + if [[ $narg == 2 ]]; then + opts="user domain log service settings firewall backup app hook dyndns tools monitor" + fi + + # If one already typed a domain, + # match the actions of that domain + if [[ $narg == 3 ]]; then + if [[ $prev == "user" ]]; then + opts="info create list update delete" + fi + if [[ $prev == "domain" ]]; then + opts="cert-install cert-status list remove url-available add dns-conf cert-renew" + fi + if [[ $prev == "log" ]]; then + opts="list display" + fi + if [[ $prev == "service" ]]; then + opts="status enable reload_or_restart log start stop remove reload add disable regen-conf restart" + fi + if [[ $prev == "settings" ]]; then + opts="reset set list reset-all get" + fi + if [[ $prev == "firewall" ]]; then + opts="reload allow stop list upnp disallow" + fi + if [[ $prev == "backup" ]]; then + opts="info restore create list delete" + fi + if [[ $prev == "app" ]]; then + opts="map checkurl install makedefault checkport listlists change-url removelist info change-label upgrade fetchlist clearaccess ssowatconf list remove register-url removeaccess setting initdb debug addaccess" + fi + if [[ $prev == "hook" ]]; then + opts="info callback add exec list remove" + fi + if [[ $prev == "dyndns" ]]; then + opts="subscribe update installcron removecron" + fi + if [[ $prev == "tools" ]]; then + opts="upgrade ldapinit postinstall maindomain update reboot shell adminpw shutdown diagnosis port-available" + fi + if [[ $prev == "monitor" ]]; then + opts="enable network show-stats update-stats disk system disable" + fi + fi + + # If no options were found propose --help + if [ -z "$opts" ]; then + if [[ $prev != "--help" ]]; then + opts=( --help ) + fi + fi + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 +} + +complete -F _yunohost_completion yunohost \ No newline at end of file From ccdd7e645d7a745d5293a188820e6e75492330c5 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Thu, 28 Mar 2019 16:47:54 +0100 Subject: [PATCH 2/4] Be able to run the script from a distant folder --- data/actionsmap/yunohost_completion.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/data/actionsmap/yunohost_completion.py b/data/actionsmap/yunohost_completion.py index 9b5472837..915c6ac89 100644 --- a/data/actionsmap/yunohost_completion.py +++ b/data/actionsmap/yunohost_completion.py @@ -8,10 +8,12 @@ adds `--help` at the end if one presses [tab] again. author: Christophe Vuillot """ +import os import yaml -ACTIONSMAP_FILE = 'yunohost.yml' -BASH_COMPLETION_FILE = '../bash-completion.d/yunohost_completion' +THIS_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) +ACTIONSMAP_FILE = THIS_SCRIPT_DIR + '/yunohost.yml' +BASH_COMPLETION_FILE = THIS_SCRIPT_DIR + '/../bash-completion.d/yunohost_completion' with open(ACTIONSMAP_FILE, 'r') as stream: From b3d8167548803364b62ecb8cafafe0f78802497c Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Tue, 2 Apr 2019 01:53:10 +0200 Subject: [PATCH 3/4] Generate yunohost_completion.py during debian builds --- data/bash-completion.d/yunohost | 15 +---- data/bash-completion.d/yunohost_completion | 77 ---------------------- debian/rules | 4 ++ 3 files changed, 7 insertions(+), 89 deletions(-) delete mode 100644 data/bash-completion.d/yunohost_completion diff --git a/data/bash-completion.d/yunohost b/data/bash-completion.d/yunohost index 106f8fbdf..2572a391d 100644 --- a/data/bash-completion.d/yunohost +++ b/data/bash-completion.d/yunohost @@ -1,12 +1,3 @@ -# -# Bash completion for yunohost -# - -_python_argcomplete() { - local IFS=' ' - COMPREPLY=( $(IFS="$IFS" COMP_LINE="$COMP_LINE" COMP_POINT="$COMP_POINT" _ARGCOMPLETE_COMP_WORDBREAKS="$COMP_WORDBREAKS" _ARGCOMPLETE=1 "$1" 8>&1 9>&2 1>/dev/null 2>/dev/null) ) - if [[ $? != 0 ]]; then - unset COMPREPLY - fi -} -complete -o nospace -o default -F _python_argcomplete "yunohost" +# This file is automatically generated +# during Debian's package build by the script +# data/actionsmap/yunohost_completion.py diff --git a/data/bash-completion.d/yunohost_completion b/data/bash-completion.d/yunohost_completion deleted file mode 100644 index 715073475..000000000 --- a/data/bash-completion.d/yunohost_completion +++ /dev/null @@ -1,77 +0,0 @@ -# -# completion for yunohost -# automatically generated from the actionsmap -# - -_yunohost_completion() -{ - local cur prev opts narg - COMPREPLY=() - - # the number of words already typed - narg=${#COMP_WORDS[@]} - - # the current word being typed - cur="${COMP_WORDS[COMP_CWORD]}" - - # the last typed word - prev="${COMP_WORDS[COMP_CWORD-1]}" - - # If one is currently typing a domain, - # match with domains - if [[ $narg == 2 ]]; then - opts="user domain log service settings firewall backup app hook dyndns tools monitor" - fi - - # If one already typed a domain, - # match the actions of that domain - if [[ $narg == 3 ]]; then - if [[ $prev == "user" ]]; then - opts="info create list update delete" - fi - if [[ $prev == "domain" ]]; then - opts="cert-install cert-status list remove url-available add dns-conf cert-renew" - fi - if [[ $prev == "log" ]]; then - opts="list display" - fi - if [[ $prev == "service" ]]; then - opts="status enable reload_or_restart log start stop remove reload add disable regen-conf restart" - fi - if [[ $prev == "settings" ]]; then - opts="reset set list reset-all get" - fi - if [[ $prev == "firewall" ]]; then - opts="reload allow stop list upnp disallow" - fi - if [[ $prev == "backup" ]]; then - opts="info restore create list delete" - fi - if [[ $prev == "app" ]]; then - opts="map checkurl install makedefault checkport listlists change-url removelist info change-label upgrade fetchlist clearaccess ssowatconf list remove register-url removeaccess setting initdb debug addaccess" - fi - if [[ $prev == "hook" ]]; then - opts="info callback add exec list remove" - fi - if [[ $prev == "dyndns" ]]; then - opts="subscribe update installcron removecron" - fi - if [[ $prev == "tools" ]]; then - opts="upgrade ldapinit postinstall maindomain update reboot shell adminpw shutdown diagnosis port-available" - fi - if [[ $prev == "monitor" ]]; then - opts="enable network show-stats update-stats disk system disable" - fi - fi - - # If no options were found propose --help - if [ -z "$opts" ]; then - if [[ $prev != "--help" ]]; then - opts=( --help ) - fi - fi - COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) - return 0 -} - -complete -F _yunohost_completion yunohost \ No newline at end of file diff --git a/debian/rules b/debian/rules index ce03d0e31..d012c73f3 100755 --- a/debian/rules +++ b/debian/rules @@ -7,6 +7,10 @@ %: dh ${@} --with=python2,systemd +override_dh_auto_build: + # Generate bash completion file + python data/actionsmap/yunohost_completion.py + override_dh_installinit: dh_installinit -pyunohost --name=yunohost-api --restart-after-upgrade dh_installinit -pyunohost --name=yunohost-firewall --noscripts From 7ff9d849c414b8037720d4e92876126480fb3027 Mon Sep 17 00:00:00 2001 From: Christophe Vuillot Date: Fri, 12 Apr 2019 14:41:59 +0200 Subject: [PATCH 4/4] fixed naming convention --- data/actionsmap/yunohost_completion.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data/actionsmap/yunohost_completion.py b/data/actionsmap/yunohost_completion.py index 915c6ac89..a4c17c4d6 100644 --- a/data/actionsmap/yunohost_completion.py +++ b/data/actionsmap/yunohost_completion.py @@ -13,7 +13,7 @@ import yaml THIS_SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) ACTIONSMAP_FILE = THIS_SCRIPT_DIR + '/yunohost.yml' -BASH_COMPLETION_FILE = THIS_SCRIPT_DIR + '/../bash-completion.d/yunohost_completion' +BASH_COMPLETION_FILE = THIS_SCRIPT_DIR + '/../bash-completion.d/yunohost' with open(ACTIONSMAP_FILE, 'r') as stream: @@ -37,7 +37,7 @@ with open(ACTIONSMAP_FILE, 'r') as stream: generated_file.write('#\n\n') # Start of the completion function - generated_file.write('_yunohost_completion()\n') + generated_file.write('_yunohost()\n') generated_file.write('{\n') # Defining local variable for previously and currently typed words @@ -83,4 +83,4 @@ with open(ACTIONSMAP_FILE, 'r') as stream: generated_file.write('}\n\n') # Add the function to bash completion - generated_file.write('complete -F _yunohost_completion yunohost') + generated_file.write('complete -F _yunohost yunohost')