From 58b136fb2559415ec10f8252c8bb3b02f997d257 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Tue, 20 Jun 2017 11:18:28 -0500 Subject: Initial commit --- src.sh | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src.sh (limited to 'src.sh') diff --git a/src.sh b/src.sh new file mode 100644 index 0000000..c847bb1 --- /dev/null +++ b/src.sh @@ -0,0 +1,85 @@ +# Copyright (c) 2017 Red Hat, Inc +# +# This file is part of src.sh +# +# ttrun is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# ttrun is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with src.sh. If not, see . + +unset -f src +function src { + GOPATH=${GOPATH:-~} + SRCSRCDIR=${SRCSRCDIR:-$GOPATH/src} + SRCSRCPREFIXES=${SRCSRCPREFIXES:-github.com} + SRCSRCUSEPUSHD=${SRCSRCUSEPUSHD:-0} + + function try_get { + if type go >/dev/null 2>&1 ; then + # If it's there, just use go get + go get -v -d $1 >/dev/null 2>&1 + else + # Simulate go get. Slightly less efficient since it will attempt + # to contact remote locations even if the request doesn't have + # a hostname component. Also, results in more file io since it + # will clone to a temp dir then move it. Just having go get + # is recommended ... but it's not THAT much extra work to support + # its non-existence. + loc=$(mktemp -d) + pushd $loc >/dev/null + git clone https://$1 >/dev/null 2>&1 + if [[ -d $(basename $1) ]] ; then + mkdir -p $SRCSRCDIR/$1 + mv $(basename $1) $SRCSRCDIR/$1 + fi + popd + rm -rf $loc + fi + try_cd $2/$1 && return 0 + return 1 + } + + function try_cd { + if [[ -d $1 ]] ; then + if [[ $SRCSRCUSEPUSHD -eq 1 ]] ; then + pushd $1 + else + echo $1 + cd $1 + fi + return 0 + fi + return 1 + } + if [[ ! -d $SRCSRCDIR ]] ; then + echo "No usable SRCSRCDIR found - either set GOPATH or SRCSRCDIR" + return 1 + fi + + # Did we give a fully qualified name and it already exists? + try_cd $SRCSRCDIR/$1 && return 0 + + # Look for non-qualified names that already exist + for prefix in $SRCSRCPREFIXES ; do + try_cd $SRCSRCDIR/$prefix/$1 && return 0 + done + + # Did we provide fully qualified name? + try_get $1 $SRCSRCDIR && return 0 + + # Try name with prefixes + for prefix in $SRCSRCPREFIXES ; do + try_get $prefix/$1 $SRCSRCDIR && return 0 + done + + echo "Could not find or clone $1 - try fully qualifying it" + return 1 +} -- cgit v1.2.3