summaryrefslogtreecommitdiff
path: root/src.sh
diff options
context:
space:
mode:
Diffstat (limited to 'src.sh')
-rw-r--r--src.sh85
1 files changed, 85 insertions, 0 deletions
diff --git a/src.sh b/src.sh
new file mode 100644
index 0000000..c847bb1
--- /dev/null
+++ b/src.sh
@@ -0,0 +1,85 @@
1# Copyright (c) 2017 Red Hat, Inc
2#
3# This file is part of src.sh
4#
5# ttrun is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# ttrun is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with src.sh. If not, see <http://www.gnu.org/licenses/>.
17
18unset -f src
19function src {
20 GOPATH=${GOPATH:-~}
21 SRCSRCDIR=${SRCSRCDIR:-$GOPATH/src}
22 SRCSRCPREFIXES=${SRCSRCPREFIXES:-github.com}
23 SRCSRCUSEPUSHD=${SRCSRCUSEPUSHD:-0}
24
25 function try_get {
26 if type go >/dev/null 2>&1 ; then
27 # If it's there, just use go get
28 go get -v -d $1 >/dev/null 2>&1
29 else
30 # Simulate go get. Slightly less efficient since it will attempt
31 # to contact remote locations even if the request doesn't have
32 # a hostname component. Also, results in more file io since it
33 # will clone to a temp dir then move it. Just having go get
34 # is recommended ... but it's not THAT much extra work to support
35 # its non-existence.
36 loc=$(mktemp -d)
37 pushd $loc >/dev/null
38 git clone https://$1 >/dev/null 2>&1
39 if [[ -d $(basename $1) ]] ; then
40 mkdir -p $SRCSRCDIR/$1
41 mv $(basename $1) $SRCSRCDIR/$1
42 fi
43 popd
44 rm -rf $loc
45 fi
46 try_cd $2/$1 && return 0
47 return 1
48 }
49
50 function try_cd {
51 if [[ -d $1 ]] ; then
52 if [[ $SRCSRCUSEPUSHD -eq 1 ]] ; then
53 pushd $1
54 else
55 echo $1
56 cd $1
57 fi
58 return 0
59 fi
60 return 1
61 }
62 if [[ ! -d $SRCSRCDIR ]] ; then
63 echo "No usable SRCSRCDIR found - either set GOPATH or SRCSRCDIR"
64 return 1
65 fi
66
67 # Did we give a fully qualified name and it already exists?
68 try_cd $SRCSRCDIR/$1 && return 0
69
70 # Look for non-qualified names that already exist
71 for prefix in $SRCSRCPREFIXES ; do
72 try_cd $SRCSRCDIR/$prefix/$1 && return 0
73 done
74
75 # Did we provide fully qualified name?
76 try_get $1 $SRCSRCDIR && return 0
77
78 # Try name with prefixes
79 for prefix in $SRCSRCPREFIXES ; do
80 try_get $prefix/$1 $SRCSRCDIR && return 0
81 done
82
83 echo "Could not find or clone $1 - try fully qualifying it"
84 return 1
85}