fol_de_dol | Hello les gens,
Je découvre ce forum, et je me suis dit que je pouvais éventuellement demander aux gens ici un avis sur un sujet un point technique En bref (dans le titre :3 ), je suis en train d'écrire un plugin d'autocomplétion ZSH pour YunoHost[1].
Je me demandais si certains d'entre vous aviez un peu d'expérience sur ce sujet.
J'en ai écrit une partie, pas tout, et je voulais avoir un avis sur la direction que je prenais, si je n'étais pas en train de faire un truc dégeu
J'ai déjà posé un topic sur le forum de YunoHost[2], mais je n'ai pas trouvé de personne qui s'y connaissait en ZSH dessus.
Voilà Merci de votre temps, si vous prenez la peine de vous intéresser à ce sujet.
Je posterai en dessous pour des questions plus spécifiques. C'est juste un premier post pour prendre un peu la température des gens avec ZSH ici
[1] https://yunohost.org/
[2] https://forum.yunohost.org/t/zsh-completion-plugin/7016
Le code du machin :
Code :
- #compdef yunohost
- # ------------------------------------------------------------------------------
- # Description
- # -----------
- #
- # Completion script for yunohost
- #
- # ------------------------------------------------------------------------------
- # Authors
- # -------
- #
- # * Fol <fol -> fol.tf>
- #
- # ------------------------------------------------------------------------------
- local state line curcontext
- __log() {
- # For debug purposes only
- echo $@ >> '/root/zsh-completion.log'
- }
- __jump() {
- local cmd
- #__log -n "__jump :1:$curcontext|$words|$CURRENT|$index"
- # Remember the subcommand name
- if (( ${#@} == 0 )); then
- local cmd=${words[2]}
- else
- cmd=$1 # < no more used?
- fi
- # Set the context for the subcommand
- curcontext="${curcontext}_${cmd}"
- # Narrow the range of words we are looking at to exclude `yunohost`
- (( CURRENT-- ))
- shift words
- #__log ":2: $curcontext|$words"
- # Run the completion for the subcommand
- if ! _call_function ret ${curcontext#:*:}; then
- _default && ret=0
- fi
- return ret
- }
- _ynh_user_list() {
- local -a list
- list=${(@)$(yunohost user list --fields uid --output-as plain):#\#*}
- compadd "$@" -a list
- }
- _ynh_domain_list() {
- local -a list
- list=${(@)$(yunohost domain list --output-as plain)}
- compadd "$@" -a list
- }
- _ynh_app_listlists() {
- local -a list
- list=${(@)${${(f)"$(yunohost app listlists)"}:# *}%:*}
- compadd "$@" -a list
- }
- # TODO: try to make a function which excludes *installed* apps
- _ynh_app_list_id() {
- local -a list
- list=${(@)${(M)${(f)"$(yunohost app list)"}:#*id:*}#*: }
- #list=${(@)${(M)${(f)"$(yunohost app list)"}:#*(id|name):*}#*: }
- compadd "$@" -a list
- }
- _ynh_app_list_id_installed() {
- local -a list
- list=${(@)${(M)${(f)"$(yunohost app list -i)"}:#*id:*}#*: }
- compadd "$@" -a list
- }
- _ynh_app_setting_keys() {
- # TODO: parse from /etc/yunohost/apps/{app}/ < give {app} from calling function: $1
- }
- #(( $+functions[_yunohost] )) ||
- _yunohost() {
- #__log '------------------------------------------------------------'
- curcontext='_yunohost'
- typeset -ag help_option; help_option=(
- '(- *)'{-h,--help}'[show this help message and exit]'
- )
- typeset -ag common_options; common_options=(
- "--no-cache[Don't use action map cache]" \
- '--output-as=[Output result in another format]:(json plain none)' \
- '--debug[Log and print debug messages]' \
- "--quiet[Don't produce any output]" \
- "--timeout=[Number of seconds before this command will timeout because it can't aqcuire the lock (meaning that another command is currently running), by default there is no timeout and the command will wait until it can get the lock]:seconds" \
- '--admin-password[The admin password to authenticate]:password' \
- $help_option
- )
- if (( CURRENT > 2 )); then
- __jump
- else
- local -a yunohost_categories; yunohost_categories=(
- 'user:Manage users'
- 'domain:Manage domains'
- 'app:Manage apps'
- 'backup:Manage backups'
- 'monitor:Monitor the server'
- 'settings:Manage YunoHost global settings'
- 'service:Manage services'
- 'firewall:Manage firewall rules'
- 'dyndns:Subscribe and Update DynDNS Hosts'
- 'tools:Specific tools'
- 'hook:Manage hooks'
- 'log:Manage debug logs'
- )
- _describe -V -t yunohost-commands 'yunohost category' yunohost_categories "$@"
- fi
- # TODO: $common_options+=(
- # '(-v --version)'{-v,--version}'[Display YunoHost packages versions]'
- # )
- _arguments $common_options
- unset common_option
- }
- # -------- User ----------------------------------------- {{{
- # --
- #(( $+functions[_yunohost_user] )) || \
- _yunohost_user() {
- if (( CURRENT > 2 )); then
- __jump
- else
- local -a yunohost_user; yunohost_user=(
- 'list:List users'
- 'create:Create user'
- 'delete:Delete user'
- 'update:Update user informations'
- 'info:Get user information'
- )
- local -a yunohost_user_subcategories; yunohost_user_subcategories=(
- 'ssh:Manage ssh access'
- )
- _describe -V -t yunohost-user 'yunohost user category' yunohost_user "$@"
- _describe -V -t yunohost-user-subcategories 'yunohost user subcategories' yunohost_user_subcategories "$@"
- fi
- }
- #(( $+functions[_yunohost_user_list] )) || \
- _yunohost_user_list() {
- local i_fields=${words[(ie)--fields]}
- if [[ i_fields -le ${#words} ]]; then # <=> $words.contains('--fields')
- _yunohost_user_list_field
- else
- local -a yunohost_user_list_field; yunohost_user_list_field=(
- '--fields:FIELD1 FIELD2 ...'
- )
- _describe -V -t yunohost-user-list 'yunohost user list FIELDS' yunohost_user_list_field "$@"
- fi
- _arguments $common_options
- # TODO : test _arguments $common_options[@]
- }
- _yunohost_user_list_field() {
- local -a yunohost_user_list_field_values; yunohost_user_list_field_values=(
- 'uid:username' \
- 'cn:fullname' \
- 'mail:mail' \
- 'maildrop:mail-forward' \
- 'loginShell:shell' \
- 'homeDirectory:home_path' \
- 'mailuserquota:mailbox-quota'
- )
- _describe -V -t yunohost-user-list-field 'yunohost user list --fields FIELD [FIELD ...]' yunohost_user_list_field_values "$@"
- }
- #(( $+functions[_yunohost_user_create] )) || \
- _yunohost_user_create() {
- _arguments \
- '1::The unique username to create' \
- '(-f --firstname)'{-f,--firstname}'[Firstname]:firstname:' \
- '(-l --lastname)'{-l,--lastname}'[Lastname]:lastname' \
- '(-m --mail)'{-m,--mail}'[Main unique email address]:email' \
- '(-p --password)'{-p,--password}'[User password]:password' \
- '(-q --mailbox-quota)'{-q,--mailbox-quota}'[Mailbox size quota (SIZE|0)]:quota (SIZE|0)' \
- $common_options
- }
- _yunohost_user_delete() {
- _arguments \
- '*::Username to delete:_ynh_user_list' \
- '--purge'
- # TODO: $common_options
- }
- _yunohost_user_update() {
- local last_double_dash=${words[(R)\-\-*]}
- local last_double_dash_index=${words[(I)\-\-*]}
- if [[ "$last_double_dash" =~ '--.*-mail.*' && \
- "$last_double_dash_index" != "${#words}" ]]; then
- local description
- case $last_double_dash in
- --add-mailforward)
- description='--add-mailforward[Mailforward addresses to add]:MAIL [MAIL ...]';;
- --remove-mailforward)
- description='--remove-mailforward[Mailforward addresses to remove]:MAIL [MAIL ...]';;
- --add-mailalias)
- description='--add-mailalias[Mail aliases to add]:MAIL [MAIL ...]';;
- --remove-mailalias)
- description='--remove-mailalias[Mail aliases to remove]:MAIL [MAIL ...]';;
- esac
- _message -e "yunohost user update $last_double_dash MAIL [MAIL ...]"
- fi
- _arguments \
- '1::Username to update:_ynh_user_list' \
- '(-f --firstname)'{-f,--firstname}'+[Firstname]:firstname:' \
- '(-l --lastname)'{-l,--lastname}'+[Lastname]:lastname' \
- '(-m --mail)'{-m,--mail}'+[Main unique email address]:email' \
- '(-p --change-password)'{-p,--change-password}'+[User password]:password' \
- '--add-mailforward[Mailforward addresses to add]:' \
- '--remove-mailforward[Mailforward addresses to remove]:' \
- '--add-mailalias[Mail aliases to add]:' \
- '--remove-mailalias[Mail aliases to remove]:' \
- '(-q --mailbox-quota)'{-q,--mailbox-quota}'+[Mailbox size quota (SIZE|0)]:quota (SIZE|0):_guard "[0-9]#" "numeric value"'
- #$common_options
- }
- _yunohost_user_info() {
- _arguments \
- '1::Username or email to get information' \
- $common_options
- }
- # --- SSH ------------------------------------------------- {{{
- _yunohost_user_ssh() {
- if (( CURRENT > 2 )); then
- __jump
- else
- local -a yunohost_user; yunohost_user=(
- 'allow:Allow the user to uses ssh' \
- 'disallow:Disallow the user to uses ssh' \
- "list-keys:Show user's authorized ssh keys" \
- 'add-key:Add a new authorized ssh key for this user' \
- 'remove-key:Remove an authorized ssh key for this user'
- )
- _describe -V -t yunohost-user 'yunohost user category' yunohost_user "$@"
- fi
- }
- _yunohost_user_ssh_allow() {
- _arguments \
- '1::Username of the user' \
- $common_options
- }
- _yunohost_user_ssh_disallow() {
- _yunohost_user_ssh_allow
- }
- _yunohost_user_ssh_list-keys() {
- _yunohost_user_ssh_allow
- }
- _yunohost_user_ssh_add-key() {
- _arguments \
- '1::Username of the user' \
- '2::The key to be added' \
- '(-c --comment)'{-c,--comment}'+[Optionnal comment about the key]:' \
- '(- *)'{-h,--help}'[show this help message and exit]'
- # TODO: add $common_options in a different section
- }
- _yunohost_user_ssh_remove-key() {
- _arguments \
- '1::Username of the user' \
- '2::The key to be removed' \
- '(-c --comment)'{-c,--comment}'+[Optionnal comment about the key]:' \
- '(- *)'{-h,--help}'[show this help message and exit]'
- # TODO: add $common_options in a different section
- }
- # }}}
- # }}}
- # -------- Domain --------------------------------------- {{{
- _yunohost_domain() {
- if (( CURRENT > 2 )); then
- __jump
- else
- local -a yunohost_domain; yunohost_domain=(
- 'list:List domains'
- 'add:Create a custom domain'
- 'remove:Delete domains'
- 'dns-conf:Generate DNS Configuration for a domain'
- 'cert-status:List status of current certificates (all by default).'
- "cert-install:Install Let's Encrypt certificates for given domains (all by default)."
- "cert-renew:Renew the Let's Encrypt certificates for given domains (all by default)."
- 'url-available:Check availability of a web path'
- )
- _describe -V -t yunohost-domain 'yunohost domain category' yunohost_domain "$@"
- fi
- }
- _yunohost_domain_list(){
- _arguments $common_options
- }
- _yunohost_domain_add(){
- _arguments \
- '1::Domain name to add' \
- '(-d --dyndns)'{-d,--dyndns}'+[Subscribe to the DynDNS service]' \
- '(- *)'{-h,--help}'[show this help message and exit]'
- # TODO: $common_options
- }
- _yunohost_domain_remove(){
- _arguments \
- '*::Domain to delete:_ynh_domain_list' \
- '(- *)'{-h,--help}'[show this help message and exit]'
- # TODO: $common_options
- }
- _yunohost_domain_dns-conf(){
- _arguments \
- '1::Target domain' \
- '(-t --ttl)'{-t,--ttl}'+[Time To Live (TTL) in seconds before DNS servers update. Default is 3600 seconds (i.e. 1 hour)]:TTL (seconds|3600):_guard "[0-9]#" "numeric value"' \
- '(- *)'{-h,--help}'[show this help message and exit]'
- # TODO: $common_options
- }
- _yunohost_domain_cert-status(){
- # TODO: there should be another way do to this, it looks dirty
- local -a args=(
- '--full[Show more details]'
- '(- *)'{-h,--help}'[show this help message and exit]'
- )
- _arguments \
- '1::Domains to check:->domains' \
- $args
- case $state in
- domains)
- _arguments \
- '*::Domains to check:_ynh_domain_list' \
- $args
- ;;
- *)
- _arguments \
- '*:Other domains to check:_ynh_domain_list' \
- ;;
- esac
- }
- _yunohost_domain_cert-install() {
- local -a args=(
- '--force[Install even if current certificate is not self-signed]'
- '--no-checks[Does not perform any check that your domain seems correctly configured (DNS, reachability) before attempting to install. (Not recommended)]'
- "--self-signed[Install the self-signed certificate instead of Let's Encrypt]"
- "--staging[Use the fake/staging Let's Encrypt certification authority. The new certificate won't actually be enabled - it is only intended to test main steps of the procedure.]"
- '(- *)'{-h,--help}'[show this help message and exit]'
- )
- _arguments \
- '1::Domains for which to install the certificates:->domains' \
- $args
- # TODO: $common_options
- case $state in
- domains)
- _arguments \
- '*::Domains for which to install the certificates:_ynh_domain_list' \
- $args
- ;;
- *)
- _arguments \
- '*:Other domains for which to install the certificates:_ynh_domain_list' \
- ;;
- esac
- }
- _yunohost_domain_cert-renew(){
- local -a args=(
- '--force[Ignore the validity threshold (30 days)]'
- '--email[Send an email to root with logs id some renewing fails]'
- '--no-checks[Does not perform any check that your domain seems correctly configured (DNS, reachability) before attempting to install. (Not recommended)]'
- "--staging[Use the fake/staging Let's Encrypt certification authority. The new certificate won't actually be enabled - it is only intended to test main steps of the procedure.]"
- '(- *)'{-h,--help}'[show this help message and exit]'
- )
- _arguments \
- '1::Domains for which to renew the certificates:->domains' \
- $args
- # TODO: $common_options
- case $state in
- domains)
- _arguments \
- '1::Domains for which to renew the certificates:_ynh_domain_list' \
- $args
- ;;
- *)
- _arguments \
- '*:Other domains for which to renew the certificates:_ynh_domain_list' \
- ;;
- esac
- }
- _yunohost_domain_url-available(){
- _arguments \
- '1::The domain for the web path (e.g. your.domain.tld):_ynh_domain_list' \
- '2::The path to check (e.g. /coffee)' \
- '(- *)'{-h,--help}'[show this help message and exit]'
- # TODO: $common_options
- }
- # }}}
- # -------- App ------------------------------------------ {{{
- #(( $+functions[_yunohost_app] )) || \
- _yunohost_app() {
- if (( CURRENT > 2 )); then
- __jump
- else
- local -a yunohost_app; yunohost_app=(
- 'fetchlist:Fetch application lists from app servers, or register a new one.'
- 'listlists:List registered application lists'
- 'removelist:Remove and forget about a given application list'
- 'list:List apps'
- 'info:Get information about an installed app'
- 'map:List apps by domain'
- 'install:Install apps'
- 'remove:Remove app'
- 'upgrade:Upgrade app'
- "change-url:Change app's URL"
- 'setting:Set or get an app setting value'
- 'register-url:Book/register a web path for a given app'
- 'debug:Display all debug informations for an application'
- 'makedefault:Redirect domain root to an app'
- 'ssowatconf:Regenerate SSOwat configuration file'
- 'change-label:Change app label'
- 'addaccess:Grant access right to users (everyone by default)'
- 'removeaccess:Revoke access right to users (everyone by default)'
- 'clearaccess:Reset access rights for the app'
- )
- local -a yunohost_app_subcategories; yunohost_app_subcategories=(
- 'action:Handle apps actions'
- 'config:Applications configuration panel'
- )
- _describe -V -t yunohost-app 'yunohost app action' yunohost_app "$@"
- _describe -V -t yunohost-app-subcategories 'yunohost app subcategories' yunohost_app_subcategories "$@"
- fi
- }
- _yunohost_app_fetchlist() {
- _arguments \
- $help_option \
- '(- *)'{-n,--name}'[Name of the list to fetch (fetches all registered lists if empty)]:NAME:_ynh_app_listlists' \
- '(- *)'{-u,--url}'[URL of a new application list to register. To be specified with -n.]:URL'
- # TODO: $common_options
- }
- _yunohost_app_listlists() {
- _arguments $help_option
- }
- _yunohost_app_removelist() {
- _arguments \
- '1::Name of the list to remove:_ynh_app_listlists' \
- $help_option
- }
- _yunohost_app_list() {
- _arguments \
- $help_option \
- '(-f --filter)'{-f,--filter}'[Name filter of app_id or app_name]:FILTER (only ids are displayed):_ynh_app_list_id' \
- '(-r --raw)'{-r,--raw}'[Return the full app_dict]' \
- '(-i --installed)'{-i,--installed}'[Return only installed apps]' \
- '(-b --with-backup)'{-b,--with-backup}'[Return only apps with backup feature (force --installed filter)]'
- }
- _yunohost_app_info() {
- _arguments \
- '1::App ID to get information from:_ynh_app_list_id_installed' \
- $help_option \
- '(-s --show-status)'{-s,--show-status}'[Show app installation status]' \
- '(-r --raw)'{-r,--raw}'[Return the full app_dict]'
- }
- _yunohost_app_map() {
- _arguments \
- $help_option \
- '(-a --app)'{-a,--app}'[Specific app to map]:APP:_ynh_app_list_id_installed' \
- '(-r --raw)'{-r,--raw}'[Return complete dict]' \
- '(-u --user)'{-u,--user}'[Allowed app map for a user]:USER:_ynh_user_list'
- }
- _yunohost_app_install() {
- _arguments \
- '1::Name, local path or git URL of the app to install:_ynh_app_list_id' \
- $help_option \
- '(-l --label)'{-l,--label}'+[Custom name for the app]' \
- '(-a --args)'{-a,--args}'+[Serialized arguments for app script (i.e. "domain=domain.tld&path=/path" )]' \
- '(-n, --no-remove-on-failure)'{-n,--no-remove-on-failure}'[Debug option to avoid removing the app on a failed installation]' \
- '(-f --force)'{-f,--force}'[Do not ask confirmation if the app is not safe to use (low quality, experimental or 3rd party)]'
- }
- _yunohost_app_remove() {
- _arguments \
- '*::App(s) to delete:_ynh_app_list_id_installed' \
- $help_option
- }
- _yunohost_app_upgrade() {
- _arguments \
- $help_option \
- '(-u --url)'{-u,--url}'[Git url to fetch for upgrade]:URL | Git url to fetch for the upgrade' \
- '(-f --file)'{-f,--file}'[Folder or tarball for upgrade]:FILE | Folder or tarball for upgrade:_files' \
- '*:App(s) to upgrade (default all):_ynh_app_list_id_installed'
- }
- _yunohost_app_change-url() {
- _arguments \
- "1::Target app instance name:_ynh_app_list_id_installed" \
- $help_option \
- '(-d --domain)'{-d,--domain}'[New app domain on which the application will be moved]:New app domain:_ynh_domain_list' \
- '(-p --path)'{-p,--path}'[New path at which the application will be moved]:Path:'
- }
- _yunohost_app_setting() {
- _arguments \
- '1::App ID:_ynh_app_list_id_installed' \
- '2::Key to get/set:_ynh_app_setting_keys' \
- $help_option \
- '(-v --value)'{-v,--value}'[Value to set]:Value to set' \
- '(-d --delete)'{-d,--delete}'[Delete the key]'
- }
- _yunohost_app_register-url() {
- _arguments \
- $help_option \
- '1::App which will use the web path:->app' \
- '2::The domain on which the app should be registered (e.g. your.domain.tld):->domain' \
- '3::The path to be registered (e.g. /coffee):->path'
- }
- _yunohost_app_debug() {
- _arguments \
- '1::App name:->app' \
- $help_option \
- }
- _yunohost_app_makedefault() {
- _arguments \
- '1::App name to put on domain root:->app' \
- $help_option \
- '(-d --domain)'{-d,--domain}'+[Specific domain to put app on (the app domain by default)]'
- }
- _yunohost_app_ssowatconf() {
- _arguments $help_option
- }
- _yunohost_app_change-label() {
- _app_new_label_[-h]
- _arguments \
- '1::App ID:->app' \
- '2::New app label:->label' \
- $help_option
- }
- _yunohost_app_addaccess() {
- _arguments \
- '*::App ID:->app' \
- $help_option \
- '(-u --users)'{-u,--users}'+[USERS [USERS ...]]'
- }
- _yunohost_app_removeaccess() {
- _arguments \
- '*::App ID:->app' \
- $help_option \
- '(-u --users)'{-u,--users}'+[USERS [USERS ...]]'
- }
- _yunohost_app_clearaccess() {
- _arguments \
- '*::App ID:->app' \
- $help_option
- }
- # ------ App Action -----------------------------------------------
- # {{{
- _yunohost_app_action() {
- if (( CURRENT > 2 )); then
- __jump
- else
- local -a yunohost_app_action; yunohost_app_action=(
- 'list:List app actions'
- 'run:Run app actions'
- )
- _describe -V -t yunohost-app-action 'yunohost app action subaction' yunohost_app_action "$@"
- fi
- }
- _yunohost_app_action_list() {
- _arguments \
- '1::App name:->app' \
- $help_option
- }
- _yunohost_app_action_run() {
- _arguments \
- '1::App name:->app' \
- '2::Action ID:->action' \
- $help_option \
- '(-a --args)'{-a,--args}'+[Serialized arguments for app script (i.e. "domain=domain.tld&path=/path" )]'
- }
- # }}}
- # ------ App Action -----------------------------------------------
- # {{{
- _yunohost_app_config() {
- if (( CURRENT > 2 )); then
- __jump
- else
- local -a yunohost_app_config; yunohost_app_config=(
- 'show-panel:show config panel for the application'
- 'apply:apply the new configuration'
- )
- _describe -V -t yunohost-app-config 'yunohost app config subaction' yunohost_app_config "$@"
- fi
- }
- _yunohost_app_action_show-panel() {
- _arguments \
- '1::App name:->app' \
- $help_option
- }
- _yunohost_app_action_apply() {
- _arguments \
- '1::App name:->app' \
- '2::Action ID:->action' \
- $help_option \
- '(-a --args)'{-a,--args}'+[Serialized arguments for app script (i.e. "domain=domain.tld&path=/path" )]'
- }
- # }}}
- # -------- Backup --------------------------------------- {{{
- _yunohost_backup_actions() {
- typeset -a _backup
- _backup=(
- 'create:Create a backup local archive. If neither --apps or --system are given, this will backup all apps and all system parts. If only --apps if given, this will only backup apps and no system parts. Similarly, if only --system is given, this will only backup system parts and no apps.'
- 'restore:Restore from a local backup archive. If neither --apps or --system are given, this will restore all apps and all system parts in the archive. If only --apps if given, this will only restore apps and no system parts. Similarly, if only --system is given, this will only restore system parts and no apps.'
- 'list:List available local backup archives'
- 'info:Show info about a local backup archive'
- 'delete:Delete a backup archive'
- )
- }
- # }}}
- # run the main dispatcher
- _yunohost "$@"
|
Message édité par fol_de_dol le 16-02-2019 à 13:35:34
|