Skip to content

Commit

Permalink
A lot of random changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Szpadel committed Aug 1, 2019
1 parent fe7a7f0 commit e952ba7
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 18 deletions.
139 changes: 139 additions & 0 deletions cli.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#!/bin/usr/env bash

import log

declare -Ag cli__options=()
declare -Ag cli__descriptions=()
cli__program_name="$0"
cli__order=()

cli::reset() {
cli__order=()
declare -Ag cli__options=()
declare -Ag cli__descriptions=()
}

cli::set_program_name() {
local program_name="$1"

cli__program_name="${program_name}"
}

cli::_option_encode() {
local option="$1"

log::debug "Encoding ${option}"

echo "${option}"
}

cli::_option_decode() {
local option="$1"

log::debug "Decoding ${option}"

echo "${option}"
}

# if letter require argument, then subfix letter with ':'
cli::add_option() {
local letter="$1"
local callback="$2"
local description="$3"

cli__order+=("$letter")
cli__options["${letter:0:1}"]="$callback"
cli__descriptions[$letter]="$description"
}

cli::print_help() {
local usage="Usage: ${cli__program_name}"
local description=""
local description_formatted=""
log::debug "${!cli__descriptions[@]}"
log::debug "values: ${cli__descriptions[*]}"
for letter in "${cli__order[@]}";do
description_formatted="${cli__descriptions["$letter"]//\\n/\\n }"
if [ "${letter:1:1}" = ":" ];then
usage+=" [-${letter:0:1} ...]"
description+=" -${letter:0:1} <...> ${description_formatted}\n"
else
usage+=" [-${letter:0:1}]"
description+=" -${letter:0:1} ${description_formatted}\n"
fi
if [ "$(echo -e "$usage" | tail -n1 | wc -m )" -gt 70 ];then
usage+="\n "
fi
done
echo -e "${usage}\n\nOptions:\n${description}"
}

cli::handle() {
local all_options=""
for letter in "${cli__order[@]}";do
all_options+="${letter}"
done

while getopts ":${all_options}" opt; do
if [ "$opt" = ":" ];then
log::fatal "Option -${OPTARG} require an argument"
elif [ "$opt" = "?" ];then
log::fatal "Invalid option -${OPTARG}"
fi
${cli__options[$opt]} "${OPTARG}"
done
}

cli_test__a=0
cli_test__b=0
cli_test__c=0

cli_test::a_cb() {
cli_test__a=1
}

cli_test::b_cb() {
cli_test__b=$1
}

cli_test::c_cb() {
cli_test__c=1
}

cli_test::test_handle() {
cli::reset
cli::set_program_name "example"
cli::add_option "a" cli_test::a_cb "Example option a"
cli::add_option "b:" cli_test::b_cb "Example option b"
cli::add_option "c" cli_test::c_cb "Example option c"

cli::handle "-a" "-b" "sth"
unit::assert_eq "${cli_test__a}" "1"
unit::assert_eq "${cli_test__b}" "sth"
unit::assert_eq "${cli_test__c}" "0"
}

cli_test::test_help_gen() {
cli::reset
cli::set_program_name "example"
cli::add_option "a" cli_test::a_cb "Example option a"
cli::add_option "b:" cli_test::b_cb "Example option b"
cli::add_option "c" cli_test::c_cb "Example option c"

local expected_output
expected_output="Usage: example [-a] [-b ...] [-c]
Options:
-a Example option a
-b <...> Example option b
-c Example option c"
local output
output=$(cli::print_help)

unit::assert_eq "${output}" "${expected_output}"
}

cli_test::all() {
unit::test cli_test::test_help_gen
unit::test cli_test::test_handle
}
32 changes: 30 additions & 2 deletions exec.bash
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,44 @@ exec::silent() {
fi
}

exec::retried_exec() {
local retries=$1
local sleep=$2
shift;shift
local try
local log
for ((try=0; try<retries; try++)) {
if log="$("$@" 2>&1)" &>/dev/null;then
return 0
fi
if [ "$DEBUG" = "1" ];then
echo "$log" >&2
fi
sleep "$sleep"
}
log::error "Command $* failed $retries times"
log::error "$log"
return 1
}

exec__sudo_keeping=0

exec::sudo_keep_alive() {
exec::assert_cmd "sudo"
if [ "$exec__sudo_keeping" != "1" ];then
exec__sudo_keeping=1
sudo -v
if ! sudo -v;then
log::fatal "sudo authentication failed"
fi
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &
fi
}

exec::sudo() {
exec::sudo_keep_alive
sudo "$@"
}

exec::is_cmd_available() {
local cmd="$1"
if command -v "$cmd" &>/dev/null;then
Expand All @@ -49,7 +77,7 @@ exec::is_fn() {
exec::assert_cmd() {
local cmd="$1"
if ! exec::is_cmd_available "$cmd";then
log::panic "Command \`$cmd\` is not available, but is required by this application"
log::fatal "Command \`$cmd\` is not available, but is required by this application"
fi
}

Expand Down
9 changes: 9 additions & 0 deletions fetch.bash
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ fetch::download_stream() {
curl -sfL "$url"
}

fetch::file_exists() {
local url=$1
if curl --output /dev/null --silent --head --fail -L "$url";then
return 0
else
return 1
fi
}

fetch::verify() {
local sha=$1
local file=$2
Expand Down
4 changes: 2 additions & 2 deletions github.bash
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ github::latest_release() {
local _repo=$1
local -n output_version=$2
local _url
_url=$(curl -w "%{url_effective}" -I -L -s -S "https://github.com/$_repo/releases/latest" -o /dev/null)
_url=$(curl -f -w "%{url_effective}" -I -L -s -S "https://github.com/$_repo/releases/latest" -o /dev/null)
if [ "$_url" != "https://github.com/$_repo/releases" ];then
# shellcheck disable=SC2034
output_version=$(echo -n "$_url"| sed -e 's|.*/||')
Expand Down Expand Up @@ -33,7 +33,7 @@ github::latest_commit() {
local -n output_sha=$3
exec::assert_cmd jq
local _json
_json=$(curl -s -L "https://api.github.com/repos/$_repo/commits/$_branch")
_json=$(curl -f -s -L "https://api.github.com/repos/$_repo/commits/$_branch")
local _sha=""
if _sha=$(echo -n "$_json" | jq '.sha' -re);then
# shellcheck disable=SC2034
Expand Down
2 changes: 1 addition & 1 deletion installer.bash
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ installer::copy_file() {
if [ -f "$dst" ];then
local backup=""
tmp::create_persistent_file backup "backup" "$installer__files_dir"
cp "$dst" "$backup"
cp -p "$dst" "$backup"
local relative_backup="${backup##$installer__dir/}"
installer::_add_sha_item "$installer__dir" "$relative_backup"
installer::_write_line "log 'Restoring $dst'"
Expand Down
69 changes: 64 additions & 5 deletions log.bash
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
#!/usr/bib/env bash

log__debug=0
log__colors=-1

log::detect_colors() {
if [ -t 1 ];then
log__colors=1
else
log__colors=0
fi
}

log::enable_debug() {
log__debug=1
Expand All @@ -10,10 +19,27 @@ log::disable_debug() {
log__debug=0
}

log::disable_colors() {
log__colors=0
log::debug "Colors forcefuly disabled"
}

log::enable_colors() {
log__colors=1
log::debug "Colors forcefuly enabled"
}

log::debug() {
local pid=$BASHPID
if [ "$log__debug" = "1" ] || [ "$DEBUG" = "1" ];then
echo -e "\e[90m$(printf "%-6s %-30s" "$pid" "${FUNCNAME[1]}"): $*\e[0m" >&2
if [ "$log__colors" = "1" ];then
echo -e "\e[90m$(printf "%-6s %-30s" "$pid" "${FUNCNAME[1]}"): $*\e[0m" >&2
elif [ "$log__colors" = "-1" ];then
log::detect_colors
log::debug "$@"
else
echo -e "[DEBUG ] $(printf "%-6s %-30s" "$pid" "${FUNCNAME[1]}"): $*" >&2
fi
fi
}

Expand All @@ -22,18 +48,51 @@ log::panic() {
stack::print 1 1
}

log::fatal() {
log::error "$*"
exit 1
}

log::success() {
echo -e "\e[32m$*\e[0m" >&2
if [ "$log__colors" = "1" ];then
echo -e "\e[32m$*\e[0m" >&2
elif [ "$log__colors" = "-1" ];then
log::detect_colors
log::success "$@"
else
echo -e "[SUCCESS] $*" >&2
fi
}

log::info() {
echo -e "\e[36m$*\e[0m" >&2
if [ "$log__colors" = "1" ];then
echo -e "\e[36m$*\e[0m" >&2
elif [ "$log__colors" = "-1" ];then
log::detect_colors
log::info "$@"
else
echo -e "[INFO ] $*" >&2
fi
}

log::error() {
echo -e "\e[31m$*\e[0m" >&2
if [ "$log__colors" = "1" ];then
echo -e "\e[31m$*\e[0m" >&2
elif [ "$log__colors" = "-1" ];then
log::detect_colors
log::error "$@"
else
echo -e "[ERROR ] $*" >&2
fi
}

log::warn() {
echo -e "\e[33m$*\e[0m" >&2
if [ "$log__colors" = "1" ];then
echo -e "\e[33m$*\e[0m" >&2
elif [ "$log__colors" = "-1" ];then
log::detect_colors
log::warn "$@"
else
echo -e "[WARN ] $*" >&2
fi
}
5 changes: 5 additions & 0 deletions stack.bash
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ stack::print() {
set +o xtrace
local code="${1:-1}"
local up=${2:-0}
# Ignore errors detected in bashdb
if [[ ${FUNCNAME[$up+1]} == _Dbg_* ]];then
return
fi
echo -e "\n\e[91mRuntime failure at ${BASH_SOURCE[$up+1]}:${BASH_LINENO[$up]} exit code $err\n"
stack::point_line "${BASH_SOURCE[$up+1]}" "${BASH_LINENO[$up]}"
if [ ${#FUNCNAME[@]} -gt $((up+2)) ];then
Expand Down Expand Up @@ -41,6 +45,7 @@ stack::point_line() {

trap stack::print ERR
set -o errtrace
set -E

import traps
traps::add_err_trap "stack::print 1 1"
2 changes: 2 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import dirdb
import tmp
import installer
import daemon
import cli

test_fails() {
unit::assertEq 1 2 "this is error"
Expand All @@ -23,3 +24,4 @@ dirdb_test::all
tmp_test::all
installer_test::all
daemon_test::all
cli_test::all
5 changes: 3 additions & 2 deletions traps.bash
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,18 @@ traps::_handle_int() {
}

traps::_handle_err() {
local err=$?
log::debug "Handling ERR traps"
if [ "$traps__err_ignore" = "1" ];then
return 0
fi
for cb in "${traps__err_trap[@]}";do
$cb
$cb "$err"
done
}

traps::_init() {
if [ "$traps__set_up" != "$BASHPID" ] && [ -z "$BASH_LIB_UNDER_TEST" ];then
if [ "$traps__set_up" = "0" ] && [ -z "$BASH_LIB_UNDER_TEST" ];then
log::debug "Setting up traps for $BASHPID"
traps__set_up=$BASHPID
traps__exit_trap=()
Expand Down
Loading

0 comments on commit e952ba7

Please sign in to comment.