Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default command to cqfd run, remove command and automatically init when needed #106

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ Just follow these steps:
* Go into your project's directory
* Create a .cqfdrc file
* Put a Dockerfile and save it as .cqfd/docker/Dockerfile
* Run ``cqfd init``
* Run ``cqfd --init``

Examples are available in the samples/ directory.

cqfd will use the provided Dockerfile to create a normalized runtime
build environment for your project.

Warning: Running cqfd init creates and names a new Docker image each
Warning: Running cqfd --init creates and names a new Docker image each
time the Dockerfile is modified, which may lead to a large number of
unused images that cannot be automatically purged. Currently, cqfd
does not have a systematic clean-up system in place.
Expand All @@ -45,8 +45,8 @@ default build command as configured in .cqfdrc, use:
Alternatively, you may want to specify a custom build command to be
executed from inside the build container.

$ cqfd run make clean
$ cqfd run "make linux-dirclean && make foobar-dirclean"
$ cqfd make clean
$ cqfd "make linux-dirclean && make foobar-dirclean"

When ``cqfd`` is running, the current directory is mounted by Docker
as a volume. As a result, all the build artefacts generated inside the
Expand All @@ -59,7 +59,7 @@ The release command behaves exactly like run, but creates a release
tarball for your project additionally. The release files (as specified
in your ``.cqfdrc``) will be included inside the release archive.

$ cqfd release
$ cqfd --release

The resulting release file is then called according to the archive
template, which defaults to ``%Po-%Pn.tar.xz``.
Expand Down
68 changes: 28 additions & 40 deletions cqfd
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ Options:
-h or --help Show this help text.

Commands:
init Initialize project build container
flavors List flavors from config file to stdout
run Run argument(s) inside build container
release Run argument(s) and release software
help Show this help text
--init Initialize project build container
--flavors List flavors from config file to stdout
--release Run argument(s) and release software
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these 3 now be in the Options: section?


By default, run is assumed, and the run command is the one
configured in .cqfdrc.
Expand Down Expand Up @@ -138,7 +136,7 @@ docker_build() {
docker_run() {

if ! docker image inspect $docker_img_name &> /dev/null; then
die "The docker image doesn't exist, launch 'cqfd init' to create it"
die "The docker image doesn't exist, launch 'cqfd --init' to create it"
fi

local interactive_options
Expand Down Expand Up @@ -456,20 +454,20 @@ has_custom_cqfdrc=false
has_to_release=false
while [ $# -gt 0 ]; do
case "$1" in
help|-h|"--help")
-h|"--help")
usage
exit 0
;;
version|-v|"--version")
-v|"--version")
echo $VERSION
exit 0
;;
init)
"--init")
config_load $flavor
docker_build
exit $?
;;
flavors)
"--flavors")
config_load
echo $build_flavors
exit 0
Expand All @@ -494,43 +492,33 @@ while [ $# -gt 0 ]; do
-q)
quiet=true
;;
run|release)
if [ "$1" = "release" ]; then
has_to_release=true
fi

shift

# No more args? run default command
[ "$#" -eq 0 ] && break

# -c appends following args to the default command
if [ "$1" = "-c" ]; then
if [ $# -lt 2 ]; then
die "option -c requires arguments"
fi

shift
build_cmd_append="$*"
break
fi

# Run alternate command
build_cmd_alt="$@"
break
;;
?*)
echo "Unknown command: $1"
usage
exit 1
"--release")
has_to_release=true
;;
*)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be out of scope, but can we support -- signifying the end of the options? Right now, cqfd -- ls doesn't work (shows the help message).

# empty or no argument case
# run a command
break
;;
esac
shift
done

# No more args? run default command
if [ "$#" -ne 0 ]; then
# -c appends following args to the default command
if [ "$1" = "-c" ]; then
if [ $# -lt 2 ]; then
die "option -c requires arguments"
fi

shift
build_cmd_append="$*"
else
# Run alternate command
build_cmd_alt="$@"
fi
fi

config_load $flavor

if [ -n "$build_cmd_alt" ]; then
Expand Down
16 changes: 8 additions & 8 deletions tests/00-setup
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,31 @@ cp -a test_data/. $TDIR/.
cd $TDIR/

################################################################################
# running 'cqfd init' should fail, as there's no proper config
# running 'cqfd --init' should fail, as there's no proper config
################################################################################
if $TDIR/.cqfd/cqfd init; then
if $TDIR/.cqfd/cqfd --init; then
jtest_result fail
else
jtest_result pass
fi

################################################################################
# running 'cqfd init' should fail, as there's an empty config
# running 'cqfd --init' should fail, as there's an empty config
################################################################################
jtest_prepare "cqfd init complains with an empty .cqfdrc"
jtest_prepare "cqfd --init complains with an empty .cqfdrc"
touch $TDIR/.cqfdrc
if $TDIR/.cqfd/cqfd init; then
if $TDIR/.cqfd/cqfd --init; then
jtest_result fail
else
jtest_result pass
fi

################################################################################
# running 'cqfd init' should fail, as there's an incomplete config
# running 'cqfd --init' should fail, as there's an incomplete config
################################################################################
jtest_prepare "cqfd init complains with an incomplete .cqfdrc"
jtest_prepare "cqfd --init complains with an incomplete .cqfdrc"
echo '[project]' >$TDIR/.cqfdrc
if $TDIR/.cqfd/cqfd init; then
if $TDIR/.cqfd/cqfd --init; then
jtest_result fail
else
jtest_result pass
Expand Down
26 changes: 13 additions & 13 deletions tests/01-cqfd_init
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@
cd $TDIR/

################################################################################
# 'cqfd init' with a proper .cqfdrc should pass
# 'cqfd --init' with a proper .cqfdrc should pass
################################################################################
jtest_prepare "run cqfd init"
if $TDIR/.cqfd/cqfd init; then
jtest_prepare "run cqfd --init"
if $TDIR/.cqfd/cqfd --init; then
jtest_result pass
else
jtest_result fail
fi


################################################################################
# 'cqfd init' with a nonexistent Dockerfile should fail
# 'cqfd --init' with a nonexistent Dockerfile should fail
################################################################################
cqfdrc_old=$(mktemp)
jtest_prepare "init with a nonexisting dockerfile shall fail"
cp -f .cqfdrc $cqfdrc_old
sed -i -e "s/\[build\]/[build]\ndistro='thisshouldfail'/" .cqfdrc
if $TDIR/.cqfd/cqfd init; then
if $TDIR/.cqfd/cqfd --init; then
jtest_result fail
else
jtest_result pass
fi


################################################################################
# 'cqfd init' with a proper Dockerfile should pass
# 'cqfd --init' with a proper Dockerfile should pass
################################################################################
jtest_prepare "run cqfd init with an other Dockerfile"
jtest_prepare "run cqfd --init with an other Dockerfile"
sed -i -e "s/thisshouldfail/centos/" .cqfdrc
if $TDIR/.cqfd/cqfd init; then
if $TDIR/.cqfd/cqfd --init; then
jtest_result pass
else
jtest_result fail
Expand All @@ -44,25 +44,25 @@ mv -f $cqfdrc_old .cqfdrc


################################################################################
# 'cqfd init' with same uid/gid dummy user should pass
# 'cqfd --init' with same uid/gid dummy user should pass
################################################################################
jtest_prepare "add dummy user and dummy group with same uid/gid"
cat <<EOF >>.cqfd/docker/Dockerfile
RUN groupadd -og $GROUPS -f dummy && \
useradd -s /bin/bash -ou $UID -g $GROUPS dummy
EOF
if $TDIR/.cqfd/cqfd init; then
if $TDIR/.cqfd/cqfd --init; then
jtest_result pass
else
jtest_result fail
fi


################################################################################
# 'cqfd init' in quiet mode should be quiet
# 'cqfd --init' in quiet mode should be quiet
################################################################################
jtest_prepare "cqfd -q init should remain quiet"
if ! $TDIR/.cqfd/cqfd -q init | grep -E "^sha256:"; then
jtest_prepare "cqfd -q --init should remain quiet"
if ! $TDIR/.cqfd/cqfd -q --init | grep -E "^sha256:"; then
jtest_result fail
else
jtest_result pass
Expand Down
4 changes: 2 additions & 2 deletions tests/03-cqfd_error
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ fi
################################################################################
# Running cqfd without a .cqfdrc file in the current directory shall fail
################################################################################
jtest_prepare "'cqfd run' with no config file shall fail"
jtest_prepare "'cqfd' with no config file shall fail"

empty_dir=$(mktemp -d)
pushd "$empty_dir" >/dev/null || exit 1

if "$cqfd" run true; then
if "$cqfd" true; then
jtest_result fail
else
jtest_result pass
Expand Down
10 changes: 5 additions & 5 deletions tests/03-cqfd_help
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
. "$(dirname $0)"/jtest.inc "$1"

################################################################################
# 'cqfd help' shall be an accepted command
# 'cqfd --help' shall be an accepted command
################################################################################
jtest_prepare "cqfd help shall exit normally"
jtest_prepare "cqfd --help shall exit normally"
TEST=$(mktemp)

if ! $TDIR/.cqfd/cqfd help >$TEST; then
if ! $TDIR/.cqfd/cqfd --help >$TEST; then
jtest_result fail
rm -f $TEST
else
Expand All @@ -17,9 +17,9 @@ fi


################################################################################
# 'cqfd help' shall produce a useful help message
# 'cqfd --help' shall produce a useful help message
################################################################################
jtest_prepare "cqfd help shall produce an help message"
jtest_prepare "cqfd --help shall produce an help message"
# Those words shall be present in the output
for word in Usage Options Commands; do
if ! grep -q "^$word:" $TEST; then
Expand Down
14 changes: 7 additions & 7 deletions tests/03-cqfd_version
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
. "$(dirname $0)"/jtest.inc "$1"

################################################################################
# 'cqfd version' shall be an accepted command
# 'cqfd --version' shall be an accepted command
################################################################################
jtest_prepare "cqfd version shall exit normally"
jtest_prepare "cqfd --version shall exit normally"
TEST=$(mktemp)

if ! $TDIR/.cqfd/cqfd version >$TEST; then
if ! $TDIR/.cqfd/cqfd --version >$TEST; then
jtest_result fail
rm -f $TEST
else
jtest_result pass
fi

################################################################################
# 'cqfd version' shall produce a version message
# 'cqfd --version' shall produce a version message
################################################################################
jtest_prepare "cqfd version shall produce a version string"
jtest_prepare "cqfd --version shall produce a version string"

if grep -qE "^[0-9.]+(-[a-z]+)?\$" $TEST; then
jtest_result pass
Expand All @@ -29,9 +29,9 @@ cat $TEST >&2
fi

################################################################################
# 'cqfd version' shall match the latest documented version
# 'cqfd --version' shall match the latest documented version
################################################################################
jtest_prepare "cqfd version major number shall match the latest documented version"
jtest_prepare "cqfd --version major number shall match the latest documented version"

doc=$(grep '^## Version' $TDIR/CHANGELOG.md | head -1 | grep -Eo '[0-9.]+(-[a-z]+)?' | head -1)
version="$(cat $TEST)"
Expand Down
16 changes: 8 additions & 8 deletions tests/04-cqfd_init_context
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ cqfd="$TDIR/.cqfd/cqfd"
cd $TDIR/

################################################################################
# 'cqfd init' without context should fail the context
# 'cqfd --init' without context should fail the context
################################################################################
jtest_prepare "cqfd init without using build_context"
if $cqfd init &&
$cqfd run "grep '^build_context=' /tmp/cqfdrc-build_context" | grep -q '^build_cont'; then
jtest_prepare "cqfd --init without using build_context"
if $cqfd --init &&
$cqfd "grep '^build_context=' /tmp/cqfdrc-build_context" | grep -q '^build_cont'; then
jtest_result fail
else
jtest_result pass
Expand All @@ -26,11 +26,11 @@ cp -f .cqfd/docker/Dockerfile.build_context .cqfd/docker/Dockerfile
cp -f cqfdrc-build_context .cqfdrc

################################################################################
# 'cqfd init' with context changes the context
# 'cqfd --init' with context changes the context
################################################################################
jtest_prepare "cqfd init using build_context"
if $cqfd init &&
$cqfd run "grep '^build_context=' /tmp/cqfdrc-build_context" | grep -q '^build_cont'; then
jtest_prepare "cqfd --init using build_context"
if $cqfd --init &&
$cqfd "grep '^build_context=' /tmp/cqfdrc-build_context" | grep -q '^build_cont'; then
jtest_result pass
else
jtest_result fail
Expand Down
Loading