~skiqqy/bin

aac55bff5398dd39a0048bfcb48a4ecf70e1f775 — Stephen Cochrane 2 years ago e187890
Added my goto script
1 files changed, 68 insertions(+), 0 deletions(-)

A goto
A goto => goto +68 -0
@@ 0,0 1,68 @@
#!/usr/bin/env bash
# cd to git repos, and start a subshell in that directory.
# Written by skiqqy with love.
# Usage: goto -h

usage()
{
	cat << EOF
$(basename "$0") ~ Git Repository Finder.

Usage: goto [OPTIONS] PATTERN

OPTIONS
-h          Shows This Message.
-f          Always goto the first matched expr.
-s SHELL    Specify the shell to use (defaults to bash).
EOF
	exit "$1"
}

main()
{
	all=true
	shell=bash
	while getopts hfs: opt
	do
		case "$opt" in
			h)
				usage 0
				;;
			f)
				all=false
				;;
			s)
				shell="$OPTARG"
				! command -v "$shell" > /dev/null && exit 1
				;;
			*)
				usage 1
				;;
		esac
	done
	shift "$((OPTIND-1))" # Get rid of parsed options

	[ -z "$1" ] && usage 1

	dirs=( $(find . -regex ".*$1.*\.git" | sed -E 's|(.*)/\.git|\1|g') ) # I want splitting

	if "$all" && [ "${#dirs[@]}" -gt 1 ]
	then
		select dir in "${dirs[@]}"
		do
			[ -n "$dir" ] && break
		done

	elif [ "${#dirs[@]}" -ge 1 ]
	then
		dir="${dirs[0]}"
	else
		printf -- 'Pattern did not match a repo.\n'
		exit 1
	fi

	[ -d "$dir" ] && cd "$dir" || exit 1
	$shell # Start the subshell
}

main "$@"