Skip to content

Commit

Permalink
Merge pull request #18 from thisendout/feature/vars-expansion-raw-opts
Browse files Browse the repository at this point in the history
Support vars sourcing, passing raw ssh/docker opts, prerun vars expansion
  • Loading branch information
cllunsford committed Aug 6, 2015
2 parents d99b2f3 + e31f442 commit 37731ca
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*.swp
test/tmp
examples_spec.sh
30 changes: 19 additions & 11 deletions shrift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function _usage() {
echo "-h Help"
echo "-d [container] Target running docker container"
echo "-s [hostname] Target remote host via ssh"
echo "-o '[opts]' Raw CLI options to pass to SSH/Docker (must be in quotes)"
echo "-v Enable verbose output"
echo
}
Expand All @@ -20,6 +21,10 @@ function _error() {
exit 1
}

function _mktemp() {
mktemp /tmp/$(basename ${0//-/}).XXX
}

function _print_cmd_summary() {
local rc=$1
local cmd=$2
Expand All @@ -45,6 +50,7 @@ function _print_dot() {
}

function _run_file() {
local file=$1
[ $verbose -eq 1 ] && echo "# ${1}"
# loop through specs
while read spec; do
Expand All @@ -55,8 +61,8 @@ function _run_file() {

# form command string
case $driver in
docker) cmd="docker exec ${target} ${spec}";;
ssh) cmd="ssh -nx ${target} ${spec}";;
docker) cmd="docker ${opts} exec ${target} ${spec}";;
ssh) cmd="ssh ${opts} -nx ${target} ${spec}";;
local) cmd="eval ${spec}";;
esac

Expand All @@ -76,22 +82,24 @@ function _run_file() {
_print_cmd_output $cmd_result
fi
result_summary+=$(_print_dot $cmd_rc)
done < $1
done < $file
}

function _main() {
driver='local'
opts=''
verbose=0
spec_files=()

# parse cli args
while [ ! -z $1 ]; do
case $1 in
-h) _usage; exit 0;;
-d) shift; driver='docker'; target=$1;;
-s) shift; driver='ssh'; target=$1;;
-v) verbose=1;;
*) spec_files+=("$1");;
-h|--help) _usage; exit 0;;
-d) shift; driver='docker'; target=$1;;
-s) shift; driver='ssh'; target=$1;;
-o) shift; opts="${1}";;
-v) verbose=1;;
*) spec_files+=("$1");;
esac
shift
done
Expand All @@ -107,7 +115,7 @@ function _main() {
if ! docker info &> /dev/null; then
_error "Cannot connect to docker daemon"
fi
if ! docker ps | grep -q $target; then
if ! docker inspect $target &> /dev/null; then
_error "No such container ${target}"
fi
fi
Expand All @@ -116,7 +124,7 @@ function _main() {
total_specs=0
failed_specs=0
result_summary=""
cmd_result=$(mktemp /tmp/$0.XXX)
cmd_result=$(_mktemp)
exit_status=0

# exec all files
Expand All @@ -142,5 +150,5 @@ function _main() {

# test if script is being called or sourced
if [[ $(basename ${0//-/}) == "shrift" ]]; then
_main $@
_main "$@"
fi
7 changes: 6 additions & 1 deletion shrift_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ assert "_usage | head -n1 | cut -d ' ' -f1" "usage:"
# test _error
assert "_error test" "${RED}ERROR${END}: test"

# test _mktemp
t=$(_mktemp)
assert_raises "test -f ${t}" 0
rm $t

# test _print_cmd_summary
assert "_print_cmd_summary 0 'good'" "${GREEN}Success 0${END}: good"
assert "_print_cmd_summary 1 'bad'" "${RED}Failure 1${END}: bad"
assert "_print_cmd_summary 255 'also bad'" "${RED}Failure 255${END}: also bad"

# test _print_cmd_output
t=$(mktemp test/tmp.XXX)
t=$(_mktemp)
echo -e "command not found: banana" > $t
assert "_print_cmd_output $t" " command not found: banana\n"
echo -e "command not found: barnana" >> $t
Expand Down

0 comments on commit 37731ca

Please sign in to comment.