]> git.corax.cc Git - toolbox/commitdiff
include: Explicitly declare arguments at the top of all functions
authorMatthias Kruk <m@m10k.eu>
Thu, 24 Jun 2021 23:40:01 +0000 (08:40 +0900)
committerMatthias Kruk <m@m10k.eu>
Thu, 24 Jun 2021 23:40:01 +0000 (08:40 +0900)
Several functions do not declare and assign all arguments at the top,
violating the toolbox coding style.
This commit changes all argument declarations so that they conform to
the toolbox style.

include/array.sh
include/clip.sh
include/gitlab.sh
include/iruca.sh
include/is.sh
include/json.sh
include/log.sh
include/opt.sh
include/ssh.sh

index 7800cf71e6cb822365fa87419d0f4436dc450f80..37554c97ddf3c4ae752bf7f3fd38188108041408 100644 (file)
@@ -21,14 +21,11 @@ __init() {
 }
 
 array_contains() {
-       local needle
-       local haystack
+       local needle="$1"
+       local haystack=("${@:2}")
 
        local cur
 
-       needle="$1"
-       haystack=("${@:2}")
-
        for cur in "${haystack[@]}"; do
                if [[ "$needle" == "$cur" ]]; then
                        return 0
@@ -39,13 +36,16 @@ array_contains() {
 }
 
 array_to_lines() {
+       local array=("$@")
        local item
 
-       for item in "$@"; do
+       for item in "${array[@]}"; do
                echo "$item"
        done
 }
 
 array_sort() {
-       array_to_lines "$@" | sort -V
+       local array=("$@")
+
+       array_to_lines "${array[@]}" | sort -V
 }
index 4c2bd1eafd1a7595a362bc7069dee67108532a99..38322b1cb00b0c6d502fb319c79aa60ae760f01c 100644 (file)
@@ -21,9 +21,7 @@ __init() {
 }
 
 clip_get() {
-       local sel
-
-       sel="$1"
+       local sel="$1"
 
        if ! xclip -selection "$sel" -o 2>/dev/null; then
                return 1
@@ -49,11 +47,8 @@ clip_get_any() {
 }
 
 clip_set() {
-       local sel
-       local data
-
-       sel="$1"
-       data="$2"
+       local sel="$1"
+       local data="$2"
 
        if (( $# < 2 )); then
                data=$(</dev/stdin)
@@ -67,14 +62,11 @@ clip_set() {
 }
 
 clip_swap() {
-       local left
-       local right
+       local left="$1"
+       local right="$2"
 
        local left_data
 
-       left="$1"
-       right="$2"
-
         left_data=$(clip_get "$left")
        clip_get "$right" | clip_set "$left"
        clip_set "$right" <<< "$left_data"
index 5e7cb4c2b4668cd23ceca3b7372cff51913b7daf..13f09cb3f3e5b59aec4e6ea1f0ea8b325025b3e3 100755 (executable)
@@ -25,19 +25,14 @@ __init() {
 }
 
 _gitlab_urlencode() {
-        local str
-
-        str="$1"
+        local str="$1"
 
         echo "${str//\//%2F}"
 }
 
 _gitlab_get() {
-        local token
-        local url
-
-       token="$1"
-       url="$2"
+        local token="$1"
+        local url="$2"
 
         if ! curl --silent --location -X GET \
             --header "Private-Token: $token" "$url"; then
@@ -48,13 +43,9 @@ _gitlab_get() {
 }
 
 _gitlab_post() {
-        local token
-        local url
-        local data
-
-        token="$1"
-        url="$2"
-        data="$3"
+        local token="$1"
+        local url="$2"
+        local data="$3"
 
         if ! curl --silent --location -X POST \
              --header "Private-Token: $token" \
@@ -79,17 +70,13 @@ _gitlab_put() {
 }
 
 gitlab_import_status() {
-       local host
-        local token
-        local project
+       local host="$1"
+       local token="$2"
+       local project="$3"
 
         local url
         local res
 
-       host="$1"
-        token="$2"
-        project="$3"
-
         id=$(_gitlab_urlencode "$project")
         url="$host/api/v4/projects/$id"
 
@@ -102,20 +89,14 @@ gitlab_import_status() {
 }
 
 gitlab_download_file() {
-       local host
-       local token
-       local project
-       local branch
-       local file
+       local host="$1"
+       local token="$2"
+       local project="$3"
+       local branch="$4"
+       local file="$5"
 
        local url
 
-       host="$1"
-       token="$2"
-       project="$3"
-       branch="$4"
-       file="$5"
-
        project=$(_gitlab_urlencode "$project")
        file=$(_gitlab_urlencode "$file")
        url="$host/api/v4/projects/$project/repository/files/$file/raw?ref=$branch"
@@ -128,14 +109,11 @@ gitlab_download_file() {
 }
 
 gitlab_get_users() {
-       local host
-       local token
+       local host="$1"
+       local token="$2"
 
        local url
 
-       host="$1"
-       token="$2"
-
        url="$host/api/v4/users?per_page=512"
 
        if ! _gitlab_get "$token" "$url"; then
@@ -146,14 +124,11 @@ gitlab_get_users() {
 }
 
 gitlab_user_list() {
-       local host
-       local token
+       local host="$1"
+       local token="$2"
 
        local resp
 
-       host="$1"
-       token="$2"
-
        if ! resp=$(gitlab_get_users "$host" "$token"); then
                return 1
        fi
@@ -178,19 +153,15 @@ gitlab_get_current_user() {
 }
 
 gitlab_get_user_id() {
-       local host
-       local token
-       local user
+       local host="$1"
+       local token="$2"
+       local user="$3"
 
        local resp
        local uid
        local username
        local fullname
 
-       host="$1"
-       token="$2"
-       user="$3"
-
        if ! resp=$(gitlab_user_list "$host" "$token"); then
                return 1
        fi
@@ -273,21 +244,15 @@ gitlab_fork_sync() {
 }
 
 gitlab_create_branch() {
-       local host
-       local token
-       local project
-       local branch
-       local ref
+       local host="$1"
+       local token="$2"
+       local project="$3"
+       local branch="$4"
+       local ref="$5"
 
        local id
        local url
 
-       host="$1"
-       token="$2"
-       project="$3"
-       branch="$4"
-        ref="$5"
-
        id=$(_gitlab_urlencode "$project")
        data=$(json_make "id" "$id" "branch" "$branch" "ref" "$ref")
        url="$host/api/v4/projects/$id/repository/branches"
@@ -300,17 +265,13 @@ gitlab_create_branch() {
 }
 
 gitlab_project_get_branches() {
-       local host
-       local token
-       local project
+       local host="$1"
+       local token="$2"
+       local project="$3"
 
        local url
        local resp
 
-       host="$1"
-       token="$2"
-       project="$3"
-
        project=$(_gitlab_urlencode "$project")
        url="$host/api/v4/projects/$project/repository/branches"
 
@@ -465,17 +426,13 @@ gitlab_project_merge_merge_request() {
 }
 
 gitlab_get_project_id() {
-       local host
-       local token
-       local project
+       local host="$1"
+       local token="$2"
+       local project="$3"
 
        local url
        local resp
 
-       host="$1"
-       token="$2"
-       project="$3"
-
        project=$(_gitlab_urlencode "$project")
        url="$host/api/v4/projects/$project"
 
@@ -488,19 +445,14 @@ gitlab_get_project_id() {
 }
 
 gitlab_list_projects_page() {
-       local host
-       local token
-       local perpage
-       local page
+       local host="$1"
+       local token="$2"
+       local perpage="$3"
+       local page="$4"
 
        local url
        local results
 
-       host="$1"
-       token="$2"
-       perpage="$3"
-       page="$4"
-
        url="$host/api/v4/projects?simple=true&per_page=$perpage&page=$page"
 
        if ! results=$(_gitlab_get "$token" "$url"); then
@@ -513,15 +465,12 @@ gitlab_list_projects_page() {
 }
 
 gitlab_list_projects() {
-       local host
-       local token
+       local host="$1"
+       local token="$2"
 
        local page
        local perpage
 
-       host="$1"
-       token="$2"
-
        page=1
        perpage=50
 
@@ -564,13 +513,13 @@ gitlab_list_projects() {
 #  respectively.
 #
 gitlab_merge_request() {
-       local host
-       local token
-       local source
-       local destination
-       local title
-       local assignee
-       local description
+       local host="$1"
+       local token="$2"
+       local source="$3"
+       local destination="$4"
+       local title="$5"
+       local assignee="$6"
+       local description="$7"
 
        local source_name
        local destination_name
@@ -581,14 +530,6 @@ gitlab_merge_request() {
        local assignee_id
        local url
 
-       host="$1"
-       token="$2"
-       source="$3"
-       destination="$4"
-       title="$5"
-       assignee="$6"
-       description="$7"
-
        source_name="${source%:*}"
        destination_name="${destination%:*}"
        source_branch="${source##*:}"
index e8d4f149c8d2b5f7270b8469e850a01d0a1ae487..af167340e3ba1120f468e8c74bc150c7fdd72d7c 100644 (file)
@@ -34,11 +34,8 @@ __init() {
 }
 
 _iruca_get() {
-       local token
-       local url
-
-       token="$1"
-       url="$2"
+       local token="$1"
+       local url="$2"
 
        if ! curl --silent --location \
             --header "X-Iruca-Token: $token" \
@@ -50,13 +47,9 @@ _iruca_get() {
 }
 
 _iruca_put() {
-       local token
-       local url
-       local data
-
-       token="$1"
-       url="$2"
-       data="$3"
+       local token="$1"
+       local url="$2"
+       local data="$3"
 
        if ! curl --silent --location -X PUT \
             --header "X-Iruca-Token: $token" \
@@ -69,14 +62,11 @@ _iruca_put() {
 }
 
 iruca_list_members() {
-       local token
-       local room
+       local token="$1"
+       local room="$2"
 
        local url
 
-       token="$1"
-       room="$2"
-
        url="$__iruca_url/rooms/$room/members"
 
        if ! _iruca_get "$token" "$url"; then
@@ -87,16 +77,12 @@ iruca_list_members() {
 }
 
 iruca_get_status() {
-       local token
-       local room
-       local member
+       local token="$1"
+       local room="$2"
+       local member="$3"
 
        local url
 
-       token="$1"
-       room="$2"
-       member="$3"
-
        url="$__iruca_url/rooms/$room/members/$member"
 
        if ! _iruca_get "$token" "$url"; then
@@ -107,12 +93,10 @@ iruca_get_status() {
 }
 
 _iruca_status_is_valid() {
-       local status
+       local status="$1"
 
        local valid_states
 
-       status="$1"
-
        valid_states=(
                "$__iruca_state_present"
                "$__iruca_state_absent"
@@ -130,21 +114,15 @@ _iruca_status_is_valid() {
 }
 
 iruca_set_status() {
-       local token
-       local room
-       local member
-       local status
-       local message
+       local token="$1"
+       local room="$2"
+       local member="$3"
+       local status="$4"
+       local message="$5"
 
        local url
        local data
 
-       token="$1"
-       room="$2"
-       member="$3"
-       status="$4"
-       message="$5"
-
        if ! _iruca_status_is_valid "$status"; then
                return 1
        fi
index ea8453a7d39626c70fd6700bdabaf299307e5824..427e0b64befd2cfbd96b215d0c63d3e02f7b6a43 100644 (file)
@@ -21,9 +21,7 @@ __init() {
 }
 
 is_digits() {
-       local str
-
-       str="$1"
+       local str="$1"
 
        if [[ "$str" =~ ^[0-9]+$ ]]; then
                return 0
@@ -33,9 +31,7 @@ is_digits() {
 }
 
 is_hex() {
-       local str
-
-       str="$1"
+       local str="$1"
 
        if [[ "$str" =~ ^[0-9a-fA-F]+$ ]]; then
                return 0
@@ -62,9 +58,7 @@ is_base64() {
 }
 
 is_upper() {
-       local str
-
-       str="$1"
+       local str="$1"
 
        if [[ "$str" =~ ^[A-Z]+$ ]]; then
                return 0
@@ -74,9 +68,7 @@ is_upper() {
 }
 
 is_lower() {
-       local str
-
-       str="$1"
+       local str="$1"
 
        if [[ "$str" =~ ^[a-z]+$ ]]; then
                return 0
@@ -86,9 +78,7 @@ is_lower() {
 }
 
 is_alpha() {
-       local str
-
-       str="$1"
+       local str="$1"
 
        if [[ "$str" =~ ^[a-zA-Z]+$ ]]; then
                return 0
@@ -98,9 +88,7 @@ is_alpha() {
 }
 
 is_alnum() {
-       local str
-
-       str="$1"
+       local str="$1"
 
        if [[ "$str" =~ ^[a-zA-Z0-9]+ ]]; then
                return 0
index f4255fd39accef7fb9a9f75deda286d84ac5e6f0..606b15c15239a1dc2a7c695d01777134429595e4 100644 (file)
@@ -25,20 +25,20 @@ __init() {
 }
 
 json_object() {
-        local argc
+       local args=("$@")
+
         local i
         local nvps
 
-        argc="$#"
         nvps=0
 
-        if (( argc % 2 != 0 )); then
+        if (( ${#args[@]} % 2 != 0 )); then
                 log_error "Invalid number of arguments"
                 return 1
         fi
 
         printf "{"
-        for (( i = 1; i <= argc; i++ )); do
+        for (( i = 0; i < ${#args[@]}; i++ )); do
                 local name
                 local value
 
@@ -50,9 +50,9 @@ json_object() {
                re_object='^\{.*\}$'
                re_array='^\[.*\]$'
 
-                name="${!i}"
+                name="${args[$i]}"
                 ((i++))
-                value="${!i}"
+                value="${args[$i]}"
 
                 if [ -z "$name" ] || [ -z "$value" ]; then
                         continue
@@ -81,6 +81,8 @@ json_object() {
 }
 
 json_array() {
+       local args=("$@")
+
        local arg
        local n
 
@@ -95,7 +97,7 @@ json_array() {
        printf "["
        n=0
 
-       for arg in "$@"; do
+       for arg in "${args[@]}"; do
                if [ -z "$arg" ]; then
                        continue
                fi
index fbd596ad4ae216ff38f5dd7587ef8efbea013789..9d678baa07da4025b3c18426f52bd54ab6d88387 100644 (file)
@@ -43,9 +43,7 @@ __init() {
 }
 
 log_set_verbosity() {
-       local verb
-
-       verb="$1"
+       local verb="$1"
 
        if (( verb < __log_error )); then
                verb="$__log_error"
@@ -83,12 +81,10 @@ log_decrease_verbosity() {
 }
 
 log_write() {
-       local level
-       local prefix
-       local line
+       local level="$1"
+       local prefix="$2"
 
-       level="$1"
-       prefix="$2"
+       local line
 
        if (( __log_verbosity < level )); then
                return 0
@@ -135,15 +131,14 @@ log_stacktrace() {
 }
 
 log_highlight() {
-       local tag
-
-       tag="$1"
+       local tag="$1"
+       local lines=("${@:2}")
 
        echo "===== BEGIN $tag ====="
-       if (( $# > 1 )); then
+       if (( ${#lines[@]} > 1 )); then
                local arg
 
-               for arg in "${@:2}"; do
+               for arg in "${lines[@]}"; do
                        echo "$arg"
                done
        else
@@ -153,22 +148,29 @@ log_highlight() {
 }
 
 log_debug() {
+       local lines=("$@")
+
        local dbgtag
-       local line
 
        dbgtag="${BASH_SOURCE[1]}:${BASH_LINENO[1]} ${FUNCNAME[1]}:"
 
-       log_write "$__log_debug" "[DBG] $dbgtag" "$@"
+       log_write "$__log_debug" "[DBG] $dbgtag" "${lines[@]}"
 }
 
 log_info() {
-       log_write "$__log_info" "[INF]" "$@"
+       local lines=("$@")
+
+       log_write "$__log_info" "[INF]" "${lines[@]}"
 }
 
 log_warn() {
-       log_write "$__log_warning" "[WRN]" "$@"
+       local lines=("$@")
+
+       log_write "$__log_warning" "[WRN]" "${lines[@]}"
 }
 
 log_error() {
-       log_write "$__log_error" "[ERR]" "$@"
+       local lines=("$@")
+
+       log_write "$__log_error" "[ERR]" "${lines[@]}"
 }
index 3a32b5a5607d15a4425debc3ce163cfcbca0cb5d..18c4c59f6a40252339ff7da43d60226d02544fc1 100644 (file)
@@ -145,15 +145,17 @@ opt_print_help() {
 }
 
 opt_parse() {
+       local argv=("$@")
+
        local optname
        local err
        local i
 
-       declare -argx __opt_argv=("$@")
+       declare -argx __opt_argv=("${argv[@]}")
 
        err=0
 
-       for (( i = 1; i <= $#; i++ )); do
+       for (( i = 0; i < ${#argv[@]}; i++ )); do
                local param
                local long
                local flags
@@ -161,7 +163,7 @@ opt_parse() {
                local action
                local regex
 
-               param="${!i}"
+               param="${argv[$i]}"
                long="${__opt_map[$param]}"
 
                if [[ -z "$long" ]]; then
@@ -176,12 +178,12 @@ opt_parse() {
                if (( flags & __opt_flag_has_value )); then
                        ((i++))
 
-                       if (( i > $# )); then
+                       if (( i > ${#argv[@]} )); then
                                log_error "Missing argument after $param"
                                return 1
                        fi
 
-                       value="${!i}"
+                       value="${argv[$i]}"
 
                        if [[ -n "$regex" ]] && ! [[ "$value" =~ $regex ]]; then
                                log_error "Value \"$value\" doesn't match \"$regex\""
@@ -225,9 +227,7 @@ opt_parse() {
 }
 
 opt_get() {
-       local long
-
-       long="$1"
+       local long="$1"
 
        if array_contains "$long" "${!__opt_value[@]}"; then
                echo "${__opt_value[$long]}"
index 39e5ba217b0524f52a83ef87b242fd7cf4455baa..2bc9b5b06063d2675717361d6f1f704ac2238d85 100644 (file)
@@ -37,16 +37,12 @@ _ssh_get_socket_dir() {
 }
 
 _ssh_tunnel_ctrl_socket_name() {
-       local host
-       local port
-       local lport
+       local host="$1"
+       local port="$2"
+       local lport="$3"
 
        local sockdir
 
-       host="$1"
-       port="$2"
-       lport="$3"
-
        if ! sockdir=$(_ssh_get_socket_dir); then
                return 1
        fi
@@ -56,14 +52,11 @@ _ssh_tunnel_ctrl_socket_name() {
 }
 
 _ssh_proxy_ctrl_socket_name() {
-       local host
-       local port
+       local host="$1"
+       local port="$2"
 
        local sockdir
 
-       host="$1"
-       port="$2"
-
        if ! sockdir=$(_ssh_get_socket_dir); then
                return 1
        fi