}
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
}
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
}
}
clip_get() {
- local sel
-
- sel="$1"
+ local sel="$1"
if ! xclip -selection "$sel" -o 2>/dev/null; then
return 1
}
clip_set() {
- local sel
- local data
-
- sel="$1"
- data="$2"
+ local sel="$1"
+ local data="$2"
if (( $# < 2 )); then
data=$(</dev/stdin)
}
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"
}
_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
}
_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" \
}
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"
}
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"
}
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
}
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
}
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
}
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"
}
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"
}
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"
}
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
}
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
# 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
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##*:}"
}
_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" \
}
_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" \
}
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
}
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
}
_iruca_status_is_valid() {
- local status
+ local status="$1"
local valid_states
- status="$1"
-
valid_states=(
"$__iruca_state_present"
"$__iruca_state_absent"
}
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
}
is_digits() {
- local str
-
- str="$1"
+ local str="$1"
if [[ "$str" =~ ^[0-9]+$ ]]; then
return 0
}
is_hex() {
- local str
-
- str="$1"
+ local str="$1"
if [[ "$str" =~ ^[0-9a-fA-F]+$ ]]; then
return 0
}
is_upper() {
- local str
-
- str="$1"
+ local str="$1"
if [[ "$str" =~ ^[A-Z]+$ ]]; then
return 0
}
is_lower() {
- local str
-
- str="$1"
+ local str="$1"
if [[ "$str" =~ ^[a-z]+$ ]]; then
return 0
}
is_alpha() {
- local str
-
- str="$1"
+ local str="$1"
if [[ "$str" =~ ^[a-zA-Z]+$ ]]; then
return 0
}
is_alnum() {
- local str
-
- str="$1"
+ local str="$1"
if [[ "$str" =~ ^[a-zA-Z0-9]+ ]]; then
return 0
}
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
re_object='^\{.*\}$'
re_array='^\[.*\]$'
- name="${!i}"
+ name="${args[$i]}"
((i++))
- value="${!i}"
+ value="${args[$i]}"
if [ -z "$name" ] || [ -z "$value" ]; then
continue
}
json_array() {
+ local args=("$@")
+
local arg
local n
printf "["
n=0
- for arg in "$@"; do
+ for arg in "${args[@]}"; do
if [ -z "$arg" ]; then
continue
fi
}
log_set_verbosity() {
- local verb
-
- verb="$1"
+ local verb="$1"
if (( verb < __log_error )); then
verb="$__log_error"
}
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
}
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
}
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[@]}"
}
}
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
local action
local regex
- param="${!i}"
+ param="${argv[$i]}"
long="${__opt_map[$param]}"
if [[ -z "$long" ]]; then
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\""
}
opt_get() {
- local long
-
- long="$1"
+ local long="$1"
if array_contains "$long" "${!__opt_value[@]}"; then
echo "${__opt_value[$long]}"
}
_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
}
_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