Last active
October 17, 2016 09:52
-
-
Save danhaywood/21b5b885433fd8bc440da3fab88c91cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# location of _repos.txt file | |
# (example at https://gist.github.com/danhaywood/938f0f751f756b1cfd6a9751b8779407) | |
# | |
#PREFIX="/mnt" | |
PREFIX="" | |
DEV_HOME="$PREFIX/c/dev" | |
_foreach_usage() | |
{ | |
cat << EOF | |
usage: $0 options command | |
Runs the specified command for all uncommented lines in _repos.txt file | |
(or as specified by -f flag) | |
OPTIONS: | |
-f [file] file to read from (defaults to _repos.txt) | |
-c continue if exception encountered | |
-g [grep] filter (grep) the input file, using expression | |
EXAMPLES: | |
foreach git status | |
foreach git diff | |
foreach git fetch | |
foreach -g isisaddons git fetch | |
foreach -c git pull --ff-only | |
foreach mvn clean install -o | |
foreach mvn clean install -o -DskipTests=true | |
foreach git add . | |
foreach git push origin master | |
EOF | |
} | |
foreach() { | |
local OPTIND continue_on_error grep_expression file | |
file="$DEV_HOME/_repos.txt" | |
grep_expression=".*" | |
continue_on_error=1 | |
while getopts "f:g:c" OPT; do | |
case "$OPT" in | |
f) file=$OPTARG | |
;; | |
g) grep_expression=$OPTARG | |
;; | |
c) continue_on_error=0 | |
;; | |
*) | |
_foreach_usage | |
return 1 | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
cmd="$*" | |
if [ "$cmd" == "" ]; then | |
_foreach_usage | |
return 1 | |
fi | |
echo "" | |
echo "cmd: $cmd" | |
echo "" | |
for a in `cat $file | \ | |
grep "$grep_expression" | \ | |
grep -v ^# ` | |
do | |
echo "" | |
pushd $a >/dev/null | |
if [ $? != 0 ]; then | |
echo "" >&2 | |
echo "">&2 | |
echo "bad directory: $a" >&2 | |
echo "">&2 | |
return 1 | |
fi | |
echo "" | |
pwd | |
eval "$cmd" | |
if [ $? != 0 ]; then | |
if [ $continue_on_error == 0 ]; then | |
echo "" >&2 | |
echo "cmd failed: $exec" >&2 | |
echo "" >&2 | |
popd >/dev/null | |
return 1 | |
fi | |
fi | |
popd >/dev/null | |
done | |
echo "" | |
echo "" | |
echo "DONE" | |
echo "" | |
} | |
############################################################### | |
_repo_usage() | |
{ | |
cat << EOF | |
usage: $0 options repo | |
Switches to the specified repository as listed in the _repos.txt file | |
(or as specified by -f flag) | |
OPTIONS: | |
-f [file] file to read from (defaults to _repos.txt) | |
[repo] identify (using regexp) the repo from input file | |
EXAMPLES: | |
repo classific | |
repo command | |
EOF | |
} | |
repo() { | |
local OPTIND grep_expression file | |
file="$DEV_HOME/_repos.txt" | |
while getopts "f:" OPT; do | |
case "$OPT" in | |
f) file=$OPTARG | |
;; | |
*) | |
_repo_usage | |
return 1 | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
grep_expression="$*" | |
if [ "Z${grep_expression}Z" == "ZZ" ]; then | |
_repo_usage | |
return 1 | |
fi | |
for a in `cat $file | \ | |
grep "$grep_expression" | \ | |
grep -v ^# ` | |
do | |
echo $a | |
done | |
for a in `cat $file | \ | |
grep "$grep_expression" | \ | |
grep -v ^# ` | |
do | |
pushd $a >/dev/null | |
if [ $? != 0 ]; then | |
echo "" >&2 | |
echo "">&2 | |
echo "bad directory: $a" >&2 | |
echo "">&2 | |
return 1 | |
fi | |
return 0 | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment