Fix getopts and its legacy mode

This commit is contained in:
Maniack Crudelis 2019-01-04 17:11:49 +01:00
parent 4bac17a162
commit 976f160afb
14 changed files with 90 additions and 19 deletions

View file

@ -16,6 +16,7 @@
# Unless you use the option --non-append
ynh_use_logrotate () {
# Declare an array to define the options of this helper.
local legacy_args=lnuya
declare -Ar args_array=( [l]=logfile= [n]=nonappend [u]=specific_user= [y]=non [a]=append )
# [y]=non [a]=append are only for legacy purpose, to not fail on the old option '--non-append'
local logfile
@ -113,6 +114,7 @@ ynh_remove_logrotate () {
#
ynh_add_systemd_config () {
# Declare an array to define the options of this helper.
local legacy_args=st
declare -Ar args_array=( [s]=service= [t]=template= )
local service
local template
@ -147,6 +149,7 @@ ynh_add_systemd_config () {
#
ynh_remove_systemd_config () {
# Declare an array to define the options of this helper.
local legacy_args=s
declare -Ar args_array=( [s]=service= )
local service
# Manage arguments with getopts

View file

@ -1,3 +1,5 @@
#!/bin/bash
CAN_BIND=${CAN_BIND:-1}
# Add a file or a directory to the list of paths to backup
@ -46,6 +48,7 @@ ynh_backup() {
# TODO find a way to avoid injection by file strange naming !
# Declare an array to define the options of this helper.
local legacy_args=sdbm
declare -Ar args_array=( [s]=src_path= [d]=dest_path= [b]=is_big [m]=not_mandatory )
local src_path
local dest_path
@ -214,6 +217,7 @@ with open(sys.argv[1], 'r') as backup_file:
#
ynh_restore_file () {
# Declare an array to define the options of this helper.
local legacy_args=odm
declare -Ar args_array=( [o]=origin_path= [d]=dest_path= [m]=not_mandatory )
local origin_path
local archive_path
@ -307,6 +311,7 @@ properly with chmod/chown." >&2
# | arg: -f, --file - The file on which the checksum will performed, then stored.
ynh_store_file_checksum () {
# Declare an array to define the options of this helper.
local legacy_args=f
declare -Ar args_array=( [f]=file= )
local file
# Manage arguments with getopts
@ -328,6 +333,7 @@ ynh_store_file_checksum () {
# | ret: Return the name a the backup file, or nothing
ynh_backup_if_checksum_is_different () {
# Declare an array to define the options of this helper.
local legacy_args=f
declare -Ar args_array=( [f]=file= )
local file
# Manage arguments with getopts
@ -356,6 +362,7 @@ ynh_backup_if_checksum_is_different () {
# | arg: -f, --file= - The file for which the checksum will be deleted
ynh_delete_file_checksum () {
# Declare an array to define the options of this helper.
local legacy_args=f
declare -Ar args_array=( [f]=file= )
local file
# Manage arguments with getopts
@ -371,6 +378,7 @@ ynh_delete_file_checksum () {
# | arg: -f, --file - File or directory to remove
ynh_secure_remove () {
# Declare an array to define the options of this helper.
local legacy_args=f
declare -Ar args_array=( [f]=file= )
local file
# Manage arguments with getopts

View file

@ -53,33 +53,33 @@ ynh_handle_getopts_args () {
# For each option in the array, reduce to short options for getopts (e.g. for [u]=user, --user will be -u)
# And built parameters string for getopts
# ${!args_array[@]} is the list of all keys in the array (A key is 'u' in [u]=user, user is a value)
# ${!args_array[@]} is the list of all option_flags in the array (An option_flag is 'u' in [u]=user, user is a value)
local getopts_parameters=""
local key=""
for key in "${!args_array[@]}"
local option_flag=""
for option_flag in "${!args_array[@]}"
do
# Concatenate each keys of the array to build the string of arguments for getopts
# Concatenate each option_flags of the array to build the string of arguments for getopts
# Will looks like 'abcd' for -a -b -c -d
# If the value of a key finish by =, it's an option with additionnal values. (e.g. --user bob or -u bob)
# Check the last character of the value associate to the key
if [ "${args_array[$key]: -1}" = "=" ]
# If the value of an option_flag finish by =, it's an option with additionnal values. (e.g. --user bob or -u bob)
# Check the last character of the value associate to the option_flag
if [ "${args_array[$option_flag]: -1}" = "=" ]
then
# For an option with additionnal values, add a ':' after the letter for getopts.
getopts_parameters="${getopts_parameters}${key}:"
getopts_parameters="${getopts_parameters}${option_flag}:"
else
getopts_parameters="${getopts_parameters}${key}"
getopts_parameters="${getopts_parameters}${option_flag}"
fi
# Check each argument given to the function
local arg=""
# ${#arguments[@]} is the size of the array
for arg in `seq 0 $(( ${#arguments[@]} - 1 ))`
do
# And replace long option (value of the key) by the short option, the key itself
# And replace long option (value of the option_flag) by the short option, the option_flag itself
# (e.g. for [u]=user, --user will be -u)
# Replace long option with =
arguments[arg]="${arguments[arg]//--${args_array[$key]}/-${key} }"
arguments[arg]="${arguments[arg]//--${args_array[$option_flag]}/-${option_flag} }"
# And long option without =
arguments[arg]="${arguments[arg]//--${args_array[$key]%=}/-${key}}"
arguments[arg]="${arguments[arg]//--${args_array[$option_flag]%=}/-${option_flag}}"
done
done
@ -132,6 +132,7 @@ ynh_handle_getopts_args () {
# Declare the content of option_var as a variable.
eval ${option_var}=""
# Then read the array value per value
local i
for i in `seq 0 $(( ${#all_args[@]} - 1 ))`
do
# If this argument is an option, end here.
@ -166,24 +167,27 @@ ynh_handle_getopts_args () {
if [ "${arguments[0]:0:1}" != "-" ]
then
# If not, enter in legacy mode and manage the arguments as positionnal ones.
echo "! Helper used in legacy mode !"
ynh_print_info --message="! Helper used in legacy mode !"
local i
for i in `seq 0 $(( ${#arguments[@]} -1 ))`
do
# Use getopts_parameters as a list of key of the array args_array
# Try to use legacy_args as a list of option_flag of the array args_array
# Otherwise, fallback to getopts_parameters to get the option_flag. But an associative arrays isn't always sorted in the correct order...
# Remove all ':' in getopts_parameters
getopts_parameters=${getopts_parameters//:}
# Get the key from getopts_parameters, by using the key according to the position of the argument.
key=${getopts_parameters:$i:1}
# Use the long option, corresponding to the key, as a variable
getopts_parameters=${legacy_args:-${getopts_parameters//:}}
# Get the option_flag from getopts_parameters, by using the option_flag according to the position of the argument.
option_flag=${getopts_parameters:$i:1}
# Use the long option, corresponding to the option_flag, as a variable
# (e.g. for [u]=user, 'user' will be used as a variable)
# Also, remove '=' at the end of the long option
# The variable name will be stored in 'option_var'
local option_var="${args_array[$key]%=}"
local option_var="${args_array[$option_flag]%=}"
# Store each value given as argument in the corresponding variable
# The values will be stored in the same order than $args_array
eval ${option_var}+=\"${arguments[$i]}\"
done
unset legacy_args
else
# END LEGACY MODE
# Call parse_arg and pass the modified list of args as an array of arguments.

View file

@ -1,3 +1,5 @@
#!/bin/bash
# Validate an IP address
#
# usage: ynh_validate_ip --family=family --ip_address=ip_address
@ -10,6 +12,7 @@ ynh_validate_ip()
# http://stackoverflow.com/questions/319279/how-to-validate-ip-address-in-python#319298
# Declare an array to define the options of this helper.
local legacy_args=fi
declare -Ar args_array=( [f]=family= [i]=ip_address= )
local family
local ip_address
@ -40,6 +43,7 @@ EOF
ynh_validate_ip4()
{
# Declare an array to define the options of this helper.
local legacy_args=i
declare -Ar args_array=( [i]=ip_address= )
local ip_address
# Manage arguments with getopts
@ -59,6 +63,7 @@ ynh_validate_ip4()
ynh_validate_ip6()
{
# Declare an array to define the options of this helper.
local legacy_args=i
declare -Ar args_array=( [i]=ip_address= )
local ip_address
# Manage arguments with getopts

View file

@ -1,3 +1,5 @@
#!/bin/bash
MYSQL_ROOT_PWD_FILE=/etc/yunohost/mysql
# Open a connection as a user
@ -11,6 +13,7 @@ MYSQL_ROOT_PWD_FILE=/etc/yunohost/mysql
# | arg: -d, --database - the database to connect to
ynh_mysql_connect_as() {
# Declare an array to define the options of this helper.
local legacy_args=upd
declare -Ar args_array=( [u]=user= [p]=password= [d]=database= )
local user
local password
@ -29,6 +32,7 @@ ynh_mysql_connect_as() {
# | arg: -d, --database - the database to connect to
ynh_mysql_execute_as_root() {
# Declare an array to define the options of this helper.
local legacy_args=sd
declare -Ar args_array=( [s]=sql= [d]=database= )
local sql
local database
@ -47,6 +51,7 @@ ynh_mysql_execute_as_root() {
# | arg: -d, --database - the database to connect to
ynh_mysql_execute_file_as_root() {
# Declare an array to define the options of this helper.
local legacy_args=fd
declare -Ar args_array=( [f]=file= [d]=database= )
local file
local database
@ -103,6 +108,7 @@ ynh_mysql_drop_db() {
# | ret: the mysqldump output
ynh_mysql_dump_db() {
# Declare an array to define the options of this helper.
local legacy_args=d
declare -Ar args_array=( [d]=database= )
local database
# Manage arguments with getopts
@ -130,6 +136,7 @@ ynh_mysql_create_user() {
ynh_mysql_user_exists()
{
# Declare an array to define the options of this helper.
local legacy_args=u
declare -Ar args_array=( [u]=user= )
local user
# Manage arguments with getopts
@ -164,6 +171,7 @@ ynh_mysql_drop_user() {
# | arg: -p, --db_password - Password of the database. If not given, a password will be generated
ynh_mysql_setup_db () {
# Declare an array to define the options of this helper.
local legacy_args=unp
declare -Ar args_array=( [u]=db_user= [n]=db_name= [p]=db_password= )
local db_user
local db_name
@ -186,6 +194,7 @@ ynh_mysql_setup_db () {
# | arg: -n, --db_name - Name of the database
ynh_mysql_remove_db () {
# Declare an array to define the options of this helper.
local legacy_args=un
declare -Ar args_array=( [u]=db_user= [n]=db_name= )
local db_user
local db_name
@ -216,6 +225,7 @@ ynh_mysql_remove_db () {
# | ret: the corrected name
ynh_sanitize_dbid () {
# Declare an array to define the options of this helper.
local legacy_args=n
declare -Ar args_array=( [n]=db_name= )
local db_name
# Manage arguments with getopts

View file

@ -1,3 +1,5 @@
#!/bin/bash
# Normalize the url path syntax
# Handle the slash at the beginning of path and its absence at ending
# Return a normalized url path
@ -12,6 +14,7 @@
# | arg: -p, --path_url - URL path to normalize before using it
ynh_normalize_url_path () {
# Declare an array to define the options of this helper.
local legacy_args=p
declare -Ar args_array=( [p]=path_url= )
local path_url
# Manage arguments with getopts
@ -35,6 +38,7 @@ ynh_normalize_url_path () {
# | arg: -p, --port - port to start to search
ynh_find_port () {
# Declare an array to define the options of this helper.
local legacy_args=p
declare -Ar args_array=( [p]=port= )
local port
# Manage arguments with getopts
@ -57,6 +61,7 @@ ynh_find_port () {
# | arg: -p, --path_url - the web path to check the availability of
ynh_webpath_available () {
# Declare an array to define the options of this helper.
local legacy_args=dp
declare -Ar args_array=( [d]=domain= [p]=path_url= )
local domain
local path_url
@ -76,6 +81,7 @@ ynh_webpath_available () {
# | arg: -p, --path_url - the web path to be registered
ynh_webpath_register () {
# Declare an array to define the options of this helper.
local legacy_args=adp
declare -Ar args_array=( [a]=app= [d]=domain= [p]=path_url= )
local app
local domain

View file

@ -1,3 +1,5 @@
#!/bin/bash
n_install_dir="/opt/node_n"
node_version_path="$n_install_dir/n/versions/node"
# N_PREFIX is the directory of n, it needs to be loaded as a environment variable.
@ -61,6 +63,7 @@ ynh_install_nodejs () {
# Use n, https://github.com/tj/n to manage the nodejs versions
# Declare an array to define the options of this helper.
local legacy_args=n
declare -Ar args_array=( [n]=nodejs_version= )
local nodejs_version
# Manage arguments with getopts

View file

@ -1,3 +1,5 @@
#!/bin/bash
# Check if apt is free to use, or wait, until timeout.
#
# [internal]
@ -29,6 +31,7 @@ ynh_wait_dpkg_free() {
# | arg: -p, --package - the package name to check
ynh_package_is_installed() {
# Declare an array to define the options of this helper.
local legacy_args=p
declare -Ar args_array=( [p]=package= )
local package
# Manage arguments with getopts
@ -48,6 +51,7 @@ ynh_package_is_installed() {
# | ret: the version or an empty string
ynh_package_version() {
# Declare an array to define the options of this helper.
local legacy_args=p
declare -Ar args_array=( [p]=package= )
local package
# Manage arguments with getopts

View file

@ -1,7 +1,10 @@
#!/bin/bash
# Print a message to stderr and exit
# usage: ynh_die --message=MSG [--ret_code=RETCODE]
ynh_die() {
# Declare an array to define the options of this helper.
local legacy_args=mc
declare -Ar args_array=( [m]=message= [c]=ret_code= )
local message
local ret_code
@ -17,6 +20,7 @@ ynh_die() {
# usage: ynh_print_info --message="Some message"
ynh_print_info() {
# Declare an array to define the options of this helper.
local legacy_args=m
declare -Ar args_array=( [m]=message= )
local message
# Manage arguments with getopts
@ -56,6 +60,7 @@ ynh_print_log () {
# | arg: -m, --message - The text to print
ynh_print_warn () {
# Declare an array to define the options of this helper.
local legacy_args=m
declare -Ar args_array=( [m]=message= )
local message
# Manage arguments with getopts
@ -70,6 +75,7 @@ ynh_print_warn () {
# | arg: -m, --message - The text to print
ynh_print_err () {
# Declare an array to define the options of this helper.
local legacy_args=m
declare -Ar args_array=( [m]=message= )
local message
# Manage arguments with getopts

View file

@ -1,3 +1,5 @@
#!/bin/bash
# Get an application setting
#
# usage: ynh_app_setting_get --app=app --key=key
@ -5,6 +7,7 @@
# | arg: -k, --key - the setting to get
ynh_app_setting_get() {
# Declare an array to define the options of this helper.
local legacy_args=ak
declare -Ar args_array=( [a]=app= [k]=key= )
local app
local key
@ -22,6 +25,7 @@ ynh_app_setting_get() {
# | arg: -v, --value - the setting value to set
ynh_app_setting_set() {
# Declare an array to define the options of this helper.
local legacy_args=akv
declare -Ar args_array=( [a]=app= [k]=key= [v]=value= )
local app
local key
@ -39,6 +43,7 @@ ynh_app_setting_set() {
# | arg: -k, --key - the setting to delete
ynh_app_setting_delete() {
# Declare an array to define the options of this helper.
local legacy_args=ak
declare -Ar args_array=( [a]=app= [k]=key= )
local app
local key

View file

@ -1,3 +1,5 @@
#!/bin/bash
# Generate a random string
#
# example: pwd=$(ynh_string_random --length=8)
@ -6,6 +8,7 @@
# | arg: -l, --length - the string length to generate (default: 24)
ynh_string_random() {
# Declare an array to define the options of this helper.
local legacy_args=l
declare -Ar args_array=( [l]=length= )
local length
# Manage arguments with getopts
@ -29,6 +32,7 @@ ynh_string_random() {
# (see sed manual page for more information)
ynh_replace_string () {
# Declare an array to define the options of this helper.
local legacy_args=mrf
declare -Ar args_array=( [m]=match_string= [r]=replace_string= [f]=target_file= )
local match_string
local replace_string
@ -55,6 +59,7 @@ ynh_replace_string () {
# characters, you can't use some regular expressions and sub-expressions.
ynh_replace_special_string () {
# Declare an array to define the options of this helper.
local legacy_args=mrf
declare -Ar args_array=( [m]=match_string= [r]=replace_string= [f]=target_file= )
local match_string
local replace_string

View file

@ -1,3 +1,5 @@
#!/bin/bash
# Manage a fail of the script
#
# [internal]

View file

@ -1,3 +1,5 @@
#!/bin/bash
# Check if a YunoHost user exists
#
# example: ynh_user_exists 'toto' || exit 1
@ -6,6 +8,7 @@
# | arg: -u, --username - the username to check
ynh_user_exists() {
# Declare an array to define the options of this helper.
local legacy_args=u
declare -Ar args_array=( [u]=username= )
local username
# Manage arguments with getopts
@ -24,6 +27,7 @@ ynh_user_exists() {
# | ret: string - the key's value
ynh_user_get_info() {
# Declare an array to define the options of this helper.
local legacy_args=uk
declare -Ar args_array=( [u]=username= [k]=key= )
local username
local key
@ -50,6 +54,7 @@ ynh_user_list() {
# | arg: -u, --username - the username to check
ynh_system_user_exists() {
# Declare an array to define the options of this helper.
local legacy_args=u
declare -Ar args_array=( [u]=username= )
local username
# Manage arguments with getopts
@ -65,6 +70,7 @@ ynh_system_user_exists() {
# | arg: -h, --home_dir - Path of the home dir for the user. Usually the final path of the app. If this argument is omitted, the user will be created without home
ynh_system_user_create () {
# Declare an array to define the options of this helper.
local legacy_args=uh
declare -Ar args_array=( [u]=username= [h]=home_dir= )
local username
local home_dir
@ -88,6 +94,7 @@ ynh_system_user_create () {
# | arg: -u, --username - Name of the system user that will be create
ynh_system_user_delete () {
# Declare an array to define the options of this helper.
local legacy_args=u
declare -Ar args_array=( [u]=username= )
local username
# Manage arguments with getopts

View file

@ -1,3 +1,5 @@
#!/bin/bash
# Extract a key from a plain command output
#
# example: yunohost user info tata --output-as plain | ynh_get_plain_key mail
@ -146,6 +148,7 @@ ynh_backup_before_upgrade () {
# | arg: -s, --source_id - Name of the app, if the package contains more than one app
ynh_setup_source () {
# Declare an array to define the options of this helper.
local legacy_args=ds
declare -Ar args_array=( [d]=dest_dir= [s]=source_id= )
local dest_dir
local source_id