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

167 lines
3.2 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
2017-09-21 08:25:23 +00:00
__func_head "[MODE] SITE TARGET_DIR [..SLICE]"
__func_help "up [MODE] TARGET_DIR"
__func_help "down [MODE] TARGET_DIR [..SLICE]"
2014-07-28 02:08:24 +00:00
echo
return 1
fi
2017-09-21 08:25:23 +00:00
NAV=false
2014-07-28 02:08:24 +00:00
case "$ARG1" in
p)
2017-09-21 08:25:23 +00:00
shift
2014-07-28 02:08:24 +00:00
CC='pushd' ;;
c)
2017-09-21 08:25:23 +00:00
shift
2014-07-28 02:08:24 +00:00
CC='cd' ;;
e)
2017-09-21 08:25:23 +00:00
shift
2014-07-28 02:08:24 +00:00
CC='echo' ;;
up|down)
2017-09-21 08:25:23 +00:00
shift
2014-07-28 02:08:24 +00:00
NAV=true ;;
*)
CC='cd'
ARG3=$ARG2
ARG2=$ARG1
;;
esac
if [ $NAV = true ]; then
2017-09-21 08:25:23 +00:00
__go_nav $ARG1 $@
return $?
2014-07-28 02:08:24 +00:00
fi
if [[ -f ~/.go_conf ]]; then
source ~/.go_conf
else
echo "Cannot source config file"
2017-09-21 08:25:23 +00:00
return 1
2014-07-28 02:08:24 +00:00
fi
2017-09-21 08:25:23 +00:00
SITE=$( ls "$UDEV" | grep -m 1 "$ARG2" )
2014-07-28 02:08:24 +00:00
if [[ -z "$SITE" ]]; then
echo "No such site: $ARG2"
2017-09-21 08:25:23 +00:00
find "$UDEV/" -maxdepth 1 -type d | sed "s/^.\+\/\([^\/]\+\)\/\?$/ \1/g"
2014-07-28 02:08:24 +00:00
return 1
fi
if [[ -z "$ARG3" ]]; then
2017-09-21 08:25:23 +00:00
$CC "$UDEV/$SITE"
return $?
2014-07-28 02:08:24 +00:00
fi
2017-09-21 08:25:23 +00:00
for ((i = 0; i < ${#LOCS[@]}; i++))
2014-07-28 02:08:24 +00:00
do
2017-09-21 08:25:23 +00:00
LOC=${LOCS[i]}
2017-09-22 02:43:15 +00:00
LOCATION=$(ls -d "$UDEV/$SITE/$LOC/*$ARG3*/" 2> /dev/null )
2014-07-28 02:08:24 +00:00
if [[ -n "$LOCATION" ]]; then
LOCATION="$LOC/$LOCATION"
2017-09-21 08:25:23 +00:00
break
2014-07-28 02:08:24 +00:00
fi
done
2017-09-21 08:25:23 +00:00
# shift ARG2 ARG3
shift; shift
2014-07-28 02:08:24 +00:00
if [[ -n "$LOCATION" ]]; then
2017-09-21 08:25:23 +00:00
__dive $CC "$UDEV/$SITE/$LOCATION/" $@
return $?
2014-07-28 02:08:24 +00:00
else
2017-09-21 08:25:23 +00:00
__dive $CC "$UDEV/$SITE" "$ARG3" $@
return $?
fi
}
__dive() {
CC=$1
WDIR=$2
shift; shift
OIFS=$IFS
IFS="*"
PATT="*$**"
IFS=$OIFS
pushd "$WDIR" > /dev/null
DIR=$(find . -type d -path "$PATT" -print -quit | cut -c 3- )
popd > /dev/null
if [[ -z $DIR ]]; then
echo "Path: $WDIR"
echo " - Pattern not found: $PATT"
2014-07-28 02:08:24 +00:00
return 1
fi
2017-09-21 08:25:23 +00:00
$CC "$WDIR/$DIR"
2014-07-28 02:08:24 +00:00
}
__go_nav() {
ARG1=$1; ARG2=$2; ARG3=$3;
case "$ARG2" in
p)
2017-09-21 08:25:23 +00:00
shift
2014-07-28 02:08:24 +00:00
CC='pushd' ;;
c|"")
2017-09-21 08:25:23 +00:00
shift
2014-07-28 02:08:24 +00:00
CC='cd' ;;
e)
2017-09-21 08:25:23 +00:00
shift
2014-07-28 02:08:24 +00:00
CC='echo' ;;
*)
CC='cd'
ARG3=$ARG2
;;
esac
if [[ "$ARG1" == "up" ]]; then
2017-09-21 08:25:23 +00:00
OIFS=$IFS
2014-07-28 02:08:24 +00:00
IFS="/" read -a PWDA <<< "$(pwd)"
2017-09-21 08:25:23 +00:00
IFS=$OIFS
2014-07-28 02:08:24 +00:00
MARK=0
2017-09-21 08:25:23 +00:00
S_LEN=$(( ${#PWDA[@]} - 2 ))
for (( i=1; i<=$S_LEN; i++ ))
2014-07-28 02:08:24 +00:00
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
if [ -n "$ARG3" ] && [[ "$STACK" =~ "$ARG3" ]]; then
2017-09-21 08:25:23 +00:00
MARK=$i
2014-07-28 02:08:24 +00:00
fi
done
if [ -z "$ARG3" ]; then
2017-09-21 08:25:23 +00:00
# Go up 1 level
MARK=$(( ${#PWDA[@]} - 2 ))
2014-07-28 02:08:24 +00:00
fi
2017-09-21 08:25:23 +00:00
if [ $MARK -eq 0 ]; then
2014-07-28 02:08:24 +00:00
echo "No such token in up stack: $ARG3"
2017-09-21 08:25:23 +00:00
for (( i=1; i<=$S_LEN; i++ )); do echo " ${PWDA[$i]}"; done
2014-07-28 02:08:24 +00:00
return 1
fi
DIR='/'
for (( i=1; i<=$MARK; i++ ))
do
DIR="$DIR${PWDA[$i]}/"
done
2017-09-21 08:25:23 +00:00
$CC "$DIR"
2014-07-28 02:08:24 +00:00
else
2017-09-21 08:25:23 +00:00
shift
__dive $CC . $@
return $?
2014-07-28 02:08:24 +00:00
fi
}