Skip to content

Commit

Permalink
prefix all messages (event error messages) with check on quiet (#140)
Browse files Browse the repository at this point in the history
This resolves #139 by silencing all (except CL parsing) messages when the user requests
`--silent`.

Namely
```
if ! denv check --workspace --silent; then
  denv init owner/repo:tag
fi
```
can quietly insure that a workspace has been initialized in scripts while still
allowing `denv init` to prompt the user about creating the workspace directory
if need be.

If script writers want to avoid all prompts, look towards the --[no-]over and
--[no-]mkdir flags for the `denv init` command.

Updates to the testing and manual have been included as well.
  • Loading branch information
tomeichlersmith authored Oct 25, 2024
1 parent 41960e7 commit e17752e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 58 deletions.
57 changes: 31 additions & 26 deletions denv
Original file line number Diff line number Diff line change
Expand Up @@ -1030,15 +1030,16 @@ _denv_check_help() {
127 : denv check was supplied an argument it didn't recognize
OPTIONS
-h, --help : print this help and exit
-q, --quiet : suppress non-error output, i.e. will silently return 0 if
denv can function properly
--workspace : check if denv can deduce a workspace from the current directory
-h, --help : print this help and exit
-q, --quiet : suppress all non-error output
-s, --silent : suppress all output (including errors), just return exist code above
--workspace : check if denv can deduce a workspace from the current directory
HELP
}
_denv_check() {
quiet=false
quiet_inf=false
quiet_err=false
workspace=false
while [ "$#" -gt "0" ]; do
case "$1" in
Expand All @@ -1047,7 +1048,11 @@ _denv_check() {
return 0
;;
-q|--quiet)
quiet=true
quiet_inf=true
;;
-s|--silent)
quiet_inf=true
quiet_err=true
;;
--workspace)
workspace=true
Expand All @@ -1061,9 +1066,9 @@ _denv_check() {
done
# check for entrypoint
if [ -x "${denv_entrypoint}" ] && [ -f "${denv_entrypoint}" ]; then
${quiet} || printf "\033[32mEntrypoint found alongside denv\033[0m\n"
${quiet_inf} || printf "\033[32mEntrypoint found alongside denv\033[0m\n"
else
_denv_error "_denv_entrypoint does not exist as an executable file alongside denv"
${quiet_err} || _denv_error "_denv_entrypoint does not exist as an executable file alongside denv"
return 1
fi

Expand All @@ -1080,7 +1085,7 @@ _denv_check() {
# the user which runner would be deduced when running
for possible in apptainer singularity podman docker;
do
${quiet} || printf "Looking for %s... " "${possible}"
${quiet_inf} || printf "Looking for %s... " "${possible}"
if command -v "${possible}" >/dev/null 2>&1; then
# extra version compatibility checking
case "${possible}" in
Expand All @@ -1090,54 +1095,54 @@ _denv_check() {
minor="$(echo "${version}" | cut -f 2 -d '.')"
#patch="$(echo "${version}" | cut -f 3 -d '.')" # not used
if singularity --version 2>&1 | grep -q apptainer; then
${quiet} || printf "found apptainer emulating singularity"
${quiet_inf} || printf "found apptainer emulating singularity"
elif [ "${major}" -lt "3" ] || [ "${major}" -eq "3" ] && [ "${minor}" -lt "6" ]; then
${quiet} || printf "found '%s', but " "$(singularity --version)"
${quiet} || printf "\033[31m%s\033[0m" \
${quiet_inf} || printf "found '%s', but " "$(singularity --version)"
${quiet_inf} || printf "\033[31m%s\033[0m" \
"denv requires the --env flag for singularity which was introduced in v3.6"
else
${quiet} || printf "\033[32mfound '%s'\033[0m" "$(${possible} --version)"
${quiet_inf} || printf "\033[32mfound '%s'\033[0m" "$(${possible} --version)"
fi
;;
docker)
# check for podman emulation of docker
# if podman is emulating docker, docker --version will return podman --version
# which includes 'podman'
if docker version 2>&1 | grep -iq podman; then
${quiet} || printf "found podman emulating docker"
${quiet_inf} || printf "found podman emulating docker"
else
${quiet} || printf "\033[32mfound '%s'\033[0m" "$(${possible} --version)"
${quiet_inf} || printf "\033[32mfound '%s'\033[0m" "$(${possible} --version)"
fi
;;
*)
# no extra checking, just print the version found
${quiet} || printf "\033[32mfound '%s'\033[0m" "$(${possible} --version)"
${quiet_inf} || printf "\033[32mfound '%s'\033[0m" "$(${possible} --version)"
;;
esac
if ${denv_runner_defined}; then
if [ "${possible}" = "${DENV_RUNNER}" ]; then
denv_runner_matched=true
${quiet} || printf "\033[32;1m <- DENV_RUNNER\033[0m"
${quiet_inf} || printf "\033[32;1m <- DENV_RUNNER\033[0m"
fi
fi
if [ "${first}" = "true" ]; then
${quiet} || printf "\033[32;1m <- use without DENV_RUNNER defined\033[0m"
${quiet_inf} || printf "\033[32;1m <- use without DENV_RUNNER defined\033[0m"
first="false"
${denv_runner_defined} || would_run_with="${possible}"
fi
else
${quiet} || printf "not found"
${quiet_inf} || printf "not found"
fi
${quiet} || printf "\n"
${quiet_inf} || printf "\n"
done
if [ "${first}" = "true" ]; then
_denv_error "No container runner found!"
${quiet_err} || _denv_error "No container runner found!"
return 2
else
${quiet} || printf "denv would run with '%s'\n" "${would_run_with}"
${quiet_inf} || printf "denv would run with '%s'\n" "${would_run_with}"
fi
if ${denv_runner_defined} && ! ${denv_runner_matched}; then
_denv_error "DENV_RUNNER=${DENV_RUNNER} but this runner is not supported by denv" \
${quiet_err} || _denv_error "DENV_RUNNER=${DENV_RUNNER} but this runner is not supported by denv" \
"(or is not available on this machine)"
return 3
fi
Expand All @@ -1150,18 +1155,18 @@ _denv_check() {
gui_incompatible_err="xhost is controlling access, disable it with 'xhost +'."
fi
if [ "${gui_incompatible_err}" != "" ]; then
_denv_error "GUI Interaction on MacOS is not expected to work." \
${quiet_err} || _denv_error "GUI Interaction on MacOS is not expected to work." \
"${gui_incompatible_err}" \
"Enabling the GUI Interaction is not necessary but may be desired."
fi
fi
if ${workspace}; then
if [ -z "${denv_workspace+x}" ] && ! _denv_deduce_workspace; then
_denv_error "Unable to deduce a denv workspace" \
${quiet_err} || _denv_error "Unable to deduce a denv workspace" \
"You may need to just 'denv init' in the directory of your choice."
return 4
else
${quiet} || printf "Found denv_workspace=\"%s\"\n" "${denv_workspace}"
${quiet_inf} || printf "Found denv_workspace=\"%s\"\n" "${denv_workspace}"
fi
fi
return 0
Expand Down
5 changes: 4 additions & 1 deletion man/man1/denv-check.1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
denv check
.SH SYNOPSIS
.PP
\f[B]denv check\f[R] [-h|\[en]help] [-q|\[en]quiet] [\[en]workspace]
\f[B]denv check\f[R] [-h|--help] [-q|--quiet] [-s|--silent] [--workspace]
.SH OPTIONS
.PP
\f[B]\f[CB]--help\f[B]\f[R] or \f[B]\f[CB]-h\f[B]\f[R] print a short
Expand All @@ -16,6 +16,9 @@ help message
\f[B]\f[CB]--quiet\f[B]\f[R] or \f[B]\f[CB]-q\f[B]\f[R] suppress
non-error output
.PP
\f[B]\f[CB]--silent\f[B]\f[R] or \f[B]\f[CB]-s\f[B]\f[R] suppress all
output include error messages (i.e. just return the exit code below)
.PP
\f[B]\f[CB]--workspace\f[B]\f[R] check to see if denv can deduce a
workspace from the current directory
.SH EXIT CODES
Expand Down
53 changes: 53 additions & 0 deletions test/check.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env bats

setup() {
load "test_helper/denv"
common_setup

go_to_tmp_work
}

teardown() {
clean_tmp_work
}

@test "basic check run" {
run -0 denv check
assert_output --partial "denv would run with '${DENV_RUNNER}'"
}

@test "quiet check run" {
run -0 denv check --quiet
refute_output
}

@test "quiet but not silent check run" {
run -4 denv check --workspace --quiet
assert_output
run -0 denv init alpine:latest
run -0 denv check --workspace --quiet
refute_output
}

@test "silent check run" {
run -4 denv check --workspace --silent
refute_output
}

@test "check fails when using unsupported runner" {
export DENV_RUNNER=dne
run -3 denv check
assert_output --partial "runner is not supported by denv"
}

@test "check that we are in a workspace" {
run -0 denv init alpine:latest
run -0 denv check --workspace
assert_output --partial "Found denv_workspace"
}

@test "check that we are not in a workspace" {
run -4 denv check --workspace
assert_output --partial "Unable to deduce a denv workspace"
}

31 changes: 0 additions & 31 deletions test/config.bats
Original file line number Diff line number Diff line change
Expand Up @@ -54,34 +54,3 @@ teardown() {
denv config network off
assert_file_contains .denv/config '^denv_network="false"$'
}

# the rest do not support norunner
# bats file_tags=

@test "basic check run" {
run denv check
assert_output --partial "denv would run with '${DENV_RUNNER}'"
}

@test "quiet check run" {
run denv check --quiet
refute_output
}

@test "check fails when using unsupported runner" {
export DENV_RUNNER=dne
run -3 denv check
assert_output --partial "runner is not supported by denv"
}

@test "check that we are in a workspace" {
run denv check --workspace
assert_output --partial "Found denv_workspace"
}

@test "check that we are not in a workspace" {
rm -r .denv
run -4 denv check --workspace
assert_output --partial "Unable to deduce a denv workspace"
}

0 comments on commit e17752e

Please sign in to comment.