utils/bash/bashrc/sources/40_go-command

150 lines
2.9 KiB
Plaintext
Raw Normal View History

2014-07-28 02:08:24 +00:00
#!/bin/bash
# go command
function go() {
ARG1=$1; ARG2=$2; ARG3=$3;
if [[ -z "$ARG1" ]]; then
2014-08-04 09:40:25 +00:00
__func_head "[MODE] SITE TARGET_DIR"
__func_help "<up|down> [MODE] TARGET_DIR"
2014-07-28 02:08:24 +00:00
echo
return 1
fi
NAV=false;
case "$ARG1" in
p)
CC='pushd' ;;
c)
CC='cd' ;;
e)
CC='echo' ;;
up|down)
NAV=true ;;
*)
CC='cd'
ARG3=$ARG2
ARG2=$ARG1
;;
esac
if [ $NAV = true ]; then
__go_nav $ARG1 $ARG2 $ARG3
return 0;
fi
if [[ -f ~/.go_conf ]]; then
source ~/.go_conf
else
echo "Cannot source config file"
return 1;
fi
SITE=$(ls $UDEV|grep -m 1 $ARG2)
if [[ -z "$SITE" ]]; then
echo "No such site: $ARG2"
ls $UDEV -1 | sed 's/^/ /g'
return 1
fi
if [[ -z "$ARG3" ]]; then
$CC $UDEV/$SITE
return 0
fi
for LOC in ${LOCS[@]}
do
LOCATION=$(ls $UDEV/$SITE/$LOC/ 2> /dev/null | grep -m 1 $ARG3)
if [[ -n "$LOCATION" ]]; then
LOCATION="$LOC/$LOCATION"
break;
fi
done
if [[ -n "$LOCATION" ]]; then
$CC $UDEV/$SITE/$LOCATION/
return 0
else
echo "Location '$ARG3' not found under: $SITE"
return 1
fi
return 1
}
__go_nav() {
ARG1=$1; ARG2=$2; ARG3=$3;
case "$ARG2" in
p)
CC='pushd' ;;
c|"")
CC='cd' ;;
e)
CC='echo' ;;
*)
CC='cd'
ARG3=$ARG2
;;
esac
if [[ "$ARG1" == "up" ]]; then
IFS="/" read -a PWDA <<< "$(pwd)"
MARK=0
2014-12-24 10:42:12 +00:00
NOT_FOUND=1
2014-07-28 02:08:24 +00:00
LISTOF=''
for i in "${!PWDA[@]}"
do
2014-12-24 10:12:39 +00:00
STACK=${PWDA[$i]}
if [ -z "$STACK" ]; then
2014-12-24 09:22:48 +00:00
continue
fi
2014-12-24 10:12:39 +00:00
MARK=$i
if [ -n "$ARG3" ] && [[ "$STACK" =~ "$ARG3" ]]; then
2014-12-24 10:42:12 +00:00
unset NOT_FOUND
2014-12-21 12:30:42 +00:00
break
2014-07-28 02:08:24 +00:00
fi
2014-12-24 10:12:39 +00:00
if [[ -n "$STACK" ]]; then
LISTOF="$LISTOF\n $STACK";
2014-07-28 02:08:24 +00:00
fi
done
if [ -z "$ARG3" ]; then
2014-12-24 10:42:12 +00:00
unset NOT_FOUND
2014-07-28 02:08:24 +00:00
MARK=$(( $MARK - 1 ))
fi
2014-12-24 10:42:12 +00:00
if [ -n "$NOT_FOUND" ]; then
2014-07-28 02:08:24 +00:00
echo "No such token in up stack: $ARG3"
echo -e $LISTOF;
echo
return 1
fi
DIR='/'
for (( i=1; i<=$MARK; i++ ))
do
DIR="$DIR${PWDA[$i]}/"
done
else
if [ -z "$ARG3" ]; then
DIR=$(find . -maxdepth 1 -type d | head -n 2 | tail -n 1)
else
DIR=$(find . -type d -name '*'$ARG3'*')
if [[ -z $DIR ]]; then
echo "Directory not found: $ARG3"
return 1
fi
fi
fi
$CC $DIR
}