utils/bash/sources/40_go-command

255 lines
5.3 KiB
Plaintext
Raw Normal View History

2014-07-28 02:08:24 +00:00
#!/bin/bash
# go command
function _begin_sel() {
2021-09-27 21:24:09 +00:00
local i
_selected=
_sel_len=${#_sel_args[@]}
if [ $_sel_len -gt 1 ]; then
echo "There are $_sel_len possible paths under $1@[$2]"
j=1
for i in ${_sel_args[@]}; do
echo " $j. $i"
j=$(( j + 1 ))
done
echo -n "Your choice [1-$(( j - 1 ))]: "
read num
re="^[1-$(( j - 1 ))]+\$"
if [[ ! $num =~ $re ]]; then
echo "Value does not fall into expected range"
return 1
fi
_selected=${_sel_args[$(( num - 1 ))]}
return 0
fi
_selected=${_sel_args[0]}
return 0
}
2014-07-28 02:08:24 +00:00
function go() {
local i _GO
2014-07-28 02:08:24 +00:00
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]"
__func_help "compiler commands use \"go help\""
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
bug|build|clean|doc|env|fix|fmt|generate|get|install|list|mod|work|run|test|tool|version|vet|help)
_GO=$( which go )
2022-10-08 13:15:12 +00:00
"$_GO" $@
return $?
;;
2014-07-28 02:08:24 +00:00
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
_sel_args=( $( ls "$UDEV" | grep "$ARG2" ) )
_begin_sel $UDEV $ARG2
SITE=$_selected
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
2018-01-31 09:59:49 +00:00
__SFT23=0
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:59:14 +00:00
LOCATION=$(ls -d "$UDEV/$SITE/$LOC/"*"$ARG3"*/ 2> /dev/null )
2014-07-28 02:08:24 +00:00
if [[ -n "$LOCATION" ]]; then
2018-01-31 09:59:49 +00:00
NPATHS=$( echo -n "$LOCATION" | grep -c '^' )
if [[ $NPATHS -gt 1 ]]; then
# shift ARG2 ARG3
shift; shift
__SFT23=1
j=1
CHOICES=()
__CC=$CC
for i in ${LOCATION[@]}; do
__dive "test -d" "$i" $@ > /dev/null
if [[ $? -eq 0 ]]; then
__LOC="$i"
CHOICES+=($i)
j=$(( j + 1 ))
fi
done
CC=$__CC
# No possible solution for this path
if [[ $j -eq 1 ]]; then
echo "Unable to trace \"$@\" under $UDEV/$SITE/*$ARG3*/"
return 1
elif [[ $j -eq 2 ]]; then
LOCATION=$__LOC
else
_sel_args=$CHOICES
2021-09-27 21:24:09 +00:00
_begin_sel "$LOCATION" $@
if [ -z $_selected ]; then
return 1
fi
LOCATION=$_selected
2018-01-31 09:59:49 +00:00
fi
fi
2017-09-21 08:25:23 +00:00
break
2014-07-28 02:08:24 +00:00
fi
done
2018-01-31 09:59:49 +00:00
if [[ $__SFT23 -eq 0 ]]; then
# shift ARG2 ARG3
shift; shift
fi
2017-09-21 08:25:23 +00:00
2014-07-28 02:08:24 +00:00
if [[ -n "$LOCATION" ]]; then
2017-09-22 02:59:14 +00:00
__dive $CC "$LOCATION" $@
2017-09-21 08:25:23 +00:00
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() {
2021-09-27 21:24:09 +00:00
local i
2017-09-21 08:25:23 +00:00
CC=$1
WDIR=$2
shift; shift
2017-09-22 02:59:14 +00:00
if [ -z "$*" ]; then
$CC "$WDIR"
return $?
fi
_EXCLUDE_ARGS=
if [ -n $EXCLUDE_DIRS ]; then
for i in ${EXCLUDE_DIRS[@]}; do
_EXCLUDE_ARGS="$_EXCLUDE_ARGS -path '*/$i' -prune -o -name $i -prune -o"
done
fi
2017-09-21 08:25:23 +00:00
OIFS=$IFS
IFS="*"
PATT="*$**"
IFS=$OIFS
pushd "$WDIR" > /dev/null
DIR=$(find . $_EXCLUDE_ARGS -path "$PATT" -type d -print -quit | cut -c 3- )
2017-09-21 08:25:23 +00:00
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() {
2021-09-27 21:24:09 +00:00
local i
2014-07-28 02:08:24 +00:00
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
}