diff --git a/README.md b/README.md index 8173341..f2c10ed 100644 --- a/README.md +++ b/README.md @@ -42,10 +42,13 @@ default build command as configured in .cqfdrc, use: $ cqfd -Alternatively, you may want to specify a custom build command to be +Alternatively, you may want to specify a single custom command to be executed from inside the build container. - $ cqfd run make clean + $ cqfd exec make clean + +Or custom commands composed with shell grammar: + $ cqfd run "make linux-dirclean && make foobar-dirclean" Additionally, you may want to open an interactive shell: diff --git a/bash-completion b/bash-completion index cdd6fa8..d76f5ab 100644 --- a/bash-completion +++ b/bash-completion @@ -48,9 +48,9 @@ _cqfd() { local arg= _get_first_arg - if [[ "$arg" == run ]]; then + if [[ "$arg" =~ ^(exec|run)$ ]]; then for (( i=1; i <= cword; i++ )); do - if [[ ${words[i]} == run ]]; then + if [[ ${words[i]} =~ ^(exec|run)$ ]]; then if [[ $((i+1)) -eq $cword ]]; then break elif [[ ${words[i+1]} == -c ]]; then @@ -61,7 +61,11 @@ _cqfd() { fi done - COMPREPLY=( $(compgen -c -W "-c" -- "$cur") ) + if [[ "$arg" == exec ]]; then + COMPREPLY=( $(compgen -c -- "$cur") ) + else + COMPREPLY=( $(compgen -c -W "-c" -- "$cur") ) + fi return elif [[ "$arg" == shell ]]; then for (( i=1; i <= cword; i++ )); do @@ -84,7 +88,7 @@ _cqfd() { return fi - local cmds="init flavors run release shell version help" + local cmds="init flavors exec run release shell version help" COMPREPLY=( $(compgen -W "$cmds $opts" -- "$cur") ) } && complete -F _cqfd cqfd diff --git a/cqfd b/cqfd index a43d662..bb7bad3 100755 --- a/cqfd +++ b/cqfd @@ -44,6 +44,7 @@ Options: Commands: init Initialize project build container. flavors List flavors from config file to stdout. + exec cmd [args] Run argument(s) inside build container. run [cmdstring] Run argument(s) inside build container. release [cmdstring] Run argument(s) and release software. shell [shargs] Run shell command inside build container. @@ -493,6 +494,16 @@ while [ $# -gt 0 ]; do -q) quiet=true ;; + exec) + if [ "$#" -lt 2 ]; then + die "command exec requires arguments" + fi + shift + config_load $flavor + command_string="${@@Q}" + docker_run "$command_string" + exit + ;; run|release) if [ "$1" = "release" ]; then has_to_release=true diff --git a/tests/07-cqfd_exec b/tests/07-cqfd_exec new file mode 100755 index 0000000..030ad53 --- /dev/null +++ b/tests/07-cqfd_exec @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +# +# validate the behavior of exec command + +set -o pipefail + +. "$(dirname "$0")"/jtest.inc "$1" +cqfd="$TDIR/.cqfd/cqfd" + +cd $TDIR/ + +jtest_prepare "cqfd exec with no argument should fail" +if ! $cqfd exec; then + jtest_result pass +else + jtest_result fail +fi + +jtest_prepare "cqfd exec with argument should succeed" +if $cqfd exec true; then + jtest_result pass +else + jtest_result fail +fi + +jtest_prepare "cqfd exec should return same status" +if $cqfd exec exit 10; + test "$?" -eq 10; then + jtest_result pass +else + jtest_result fail +fi + +jtest_prepare "cqfd exec should preserve the arguments" +if $cqfd exec /bin/sh -c 'printf "0=$0,*=$*,#=$#"' zero one two three \ + | grep "0=zero,\*=one two three,#=3$"; then + jtest_result pass +else + jtest_result fail +fi