Skip to content

Commit

Permalink
Merge pull request #29 from greymd/feature/error_codes
Browse files Browse the repository at this point in the history
Feature/error codes
  • Loading branch information
greymd authored Apr 26, 2017
2 parents d005e84 + 3082be3 commit af9e4e7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 22 deletions.
62 changes: 47 additions & 15 deletions bin/xpanes
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,29 @@ set -e
# @Filename xpanes
readonly _XP_VERSION="2.0.0-alpha"

trap "exit 130" SIGHUP SIGINT SIGQUIT SIGTERM
# Error constants
# ===============

# Undefined or General errors
readonly _XP_EUNDEF=1

# Invalid option/argument
readonly _XP_EINVAL=4

# Could not open tty.
readonly _XP_ETTY=5

# Log related exit status is 2x.
## Could not create a directory.
readonly _XP_ELOGDIR=20

## Could not directory to store logs is not writable.
readonly _XP_ELOGWRITE=21

# Necessary commands are not found
readonly _XP_ENOCMD=127

# ===============

# _XP_THIS_FILE_NAME is supposed to be "xpanes".
readonly _XP_THIS_FILE_NAME="${0##*/}"
Expand Down Expand Up @@ -175,6 +197,7 @@ __xpanes::log_filenames () {
#
__xpanes::ensure_directory() {
local _dir="$1"
local _checkfile="${_XP_THIS_FILE_NAME}.$$"

# Remove end of slash '/'
_dir="${_dir%/}"
Expand All @@ -183,23 +206,25 @@ __xpanes::ensure_directory() {
_dir="${_dir/#~/$HOME}"

# Check directory.
if [ ! -e "${_dir}" ]; then
if [ ! -d "${_dir}" ]; then
# Create directory
if mkdir "${_dir}"; then
echo "${_dir} is created." >&2
else
echo "Failed to create ${_dir}" >&2
exit 20
exit $_XP_ELOGDIR
fi
fi
if [ ! -d "${_dir}" ]; then
echo "${_dir} is not directory." >&2
exit 21
fi
if [ ! -w "${_dir}" ]; then
# Try to create file.
# Not only checking directory permission,
# but also i-node and other misc situations.
touch "${_dir}/$_checkfile"
if [ $? -ne 0 ]; then
echo "${_dir} is not writable." >&2
exit 22
rm -f "${_dir}/$_checkfile"
exit $_XP_ELOGWRITE
fi
rm -f "${_dir}/$_checkfile"

# Return absolute path
echo "$(cd "$_XP_LOG_DIR" && pwd)"
Expand Down Expand Up @@ -298,11 +323,18 @@ __xpanes::arrange_pane_location() {
local _window_name="$1" ; shift
local _hosts_num="$1"

# ----------------
# Default behavior
# ----------------
if [ $_hosts_num -eq 1 ]; then
tmux select-layout -t ${_window_name} even-horizontal
elif [ $_hosts_num -gt 1 ]; then
tmux select-layout -t ${_window_name} tiled
fi
# ----------------
# TODO: User behavior
# ----------------
# tmux select-layout -t ${_window_name} even-vertical
}

#
Expand Down Expand Up @@ -347,7 +379,7 @@ __xpanes::check_env() {
type $cmd > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "$cmd is required to work." >&2
exit 127
exit $_XP_ENOCMD
fi
done < <(echo "$_cmds" | tr ' ' '\n')

Expand Down Expand Up @@ -392,7 +424,7 @@ __xpanes::pre_execution() {
echo ""
echo " tmux -S $_XP_SOCKET_PATH attach-session -t $_XP_SESSION_NAME"
echo ""
exit 1
exit $_XP_ETTY
fi
set -e
fi
Expand Down Expand Up @@ -440,7 +472,7 @@ __xpanes::switch_xargs_mode() {

if [ ! -z "${_XP_ARGS[*]}" ] && [ ! -z "$_XP_CMD_UTILITY" ]; then
echo "Error: Both arguments and '-c' option are given." >&2
exit 1
exit $_XP_EINVAL
fi

while read line;
Expand Down Expand Up @@ -559,7 +591,7 @@ __xpanes::enable_long_options() {
echo "Invalid option -- '${1#--}'" >&2
__xpanes::usage
echo "Try '$_XP_THIS_FILE_NAME --help' for more information." >&2
exit 4
exit $_XP_EINVAL
fi
}

Expand Down Expand Up @@ -627,7 +659,7 @@ __xpanes::parse_options() {
echo "Invalid option -- '${1#-}'" >&2
__xpanes::usage
echo "Try '$_XP_THIS_FILE_NAME --help' for more information." >&2
exit 4
exit $_XP_EINVAL
fi
fi
;;
Expand Down Expand Up @@ -655,7 +687,7 @@ __xpanes::parse_options() {
echo "No arguments are given." >&2
__xpanes::usage
echo "Try '$_XP_THIS_FILE_NAME --help' for more information." >&2
exit 5
exit $_XP_EINVAL
fi

# Set default value
Expand Down
16 changes: 9 additions & 7 deletions test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ tearDown(){

test_argument_and_utility_xargs() {
echo 10 | ${EXEC} -c 'seq {}' factor {}
assertEquals "1" "$?"
assertEquals "4" "$?"
}

test_unsupported_version() {
Expand Down Expand Up @@ -407,15 +407,15 @@ test_no_args() {
printf "\n $ $_cmd\n"
# execute
$_cmd > /dev/null
assertEquals "5" "$?"
assertEquals "4" "$?"
}

test_hyphen_only() {
local _cmd="${EXEC} --"
printf "\n $ $_cmd\n"
# execute
$_cmd > /dev/null
assertEquals "5" "$?"
assertEquals "4" "$?"
}

test_hyphen_and_option1() {
Expand Down Expand Up @@ -628,6 +628,7 @@ test_desync_option_2() {
close_tmux_session "$_socket_file"
}
}

test_failed_creat_directory() {
local _log_dir="${SHUNIT_TMPDIR}/dirA/dirB"
local _cmd="${EXEC} --log=$_log_dir 1 2 3"
Expand All @@ -637,9 +638,10 @@ test_failed_creat_directory() {
assertEquals "20" "$?"
}

test_use_file_insteadof_directory() {
local _log_dir="${SHUNIT_TMPDIR}/file"
echo "dummy" > $_log_dir
test_directory_is_not_writable() {
local _log_dir="${SHUNIT_TMPDIR}/non_writable"
mkdir -p $_log_dir
chmod 666 $_log_dir
local _cmd="${EXEC} --log=$_log_dir 1 2 3"
printf "\n $ $_cmd\n"
# execute
Expand All @@ -661,7 +663,7 @@ test_non_writable_directory() {
printf "\n $ $_cmd\n"
# execute
$_cmd > /dev/null
assertEquals "22" "$?"
assertEquals "21" "$?"
}

test_insufficient_cmd() {
Expand Down

0 comments on commit af9e4e7

Please sign in to comment.