Skip to content

Commit

Permalink
specialize denv init for script writers (#142)
Browse files Browse the repository at this point in the history
This introduces four new command line flags to the `denv init` CLI
making it easier for script writers to avoid the user prompt if desired.
This follows the idea outlined in
#140 (comment)

`--[no-]mkdir` answers the prompt about if denv is allowed to create the workspace
directory if it doesn't exist.
`--[no-]over` answers the prompt about if denv is allowed to overwrite/override an
existing configuration

Besides the introduction of these flags, more tests were included to make sure
they are oprational. The manual and CLI help was updated accordingly,
and some cleanup was done to use true/false instead of 1/0 within variables
storing the results of these flags.
  • Loading branch information
tomeichlersmith authored Oct 25, 2024
1 parent e655a83 commit 41960e7
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 32 deletions.
70 changes: 45 additions & 25 deletions denv
Original file line number Diff line number Diff line change
Expand Up @@ -829,8 +829,7 @@ _denv_config() {
_denv_init_help() {
cat <<\HELP
denv init [help|-h|--help] IMAGE [WORKSPACE] [--no-gitignore]
[--force] [--name NAME] [--clean-env|--no-copy-all]
denv init [OPTIONS] IMAGE [WORKSPACE]
ARGUMENT
help : print this help and exit
Expand All @@ -841,8 +840,11 @@ _denv_init_help() {
OPTIONS
-h, --help : print this help and exit
--no-gitignore : don't generate a gitignore for the .denv directory
--force : overwrite/override an existing denv if it exists and create
the WORKSPACE without prompting if it doesn't exist
--[no-]over : Don't ask about overwrite/override.
Instead just do it (--over) or don't do it (--no-over).
--[no-]mkdir : Don't ask about creating WORKSPACE if it doesn't exist.
Instead just do it (--mkdir) or don't (--no-mkdir).
--force : alias for --over --mkdir
--name : set a name for this denv
--clean-env : don't enable copying of host environment variables
--no-copy-all is another alias which more closely
Expand All @@ -858,9 +860,8 @@ _denv_init() {
_denv_error "Provide at least an image to use for running"
return 1
fi
copyall=1
gitignore=1
force=1
copyall=true
gitignore=true
positionals=""
while [ "$#" -gt "0" ]; do
case "$1" in
Expand All @@ -869,13 +870,26 @@ _denv_init() {
return 0
;;
--no-gitignore)
gitignore=0
gitignore=false
;;
--clean-env|--no-copy-all)
copyall=0
copyall=false
;;
--force)
force=0
over=true
mkwork=true
;;
--no-over)
over=false
;;
--over)
over=true
;;
--no-mkdir)
mkwork=false
;;
--mkdir)
mkwork=true
;;
--name)
if [ -z "${2+x}" ]; then
Expand Down Expand Up @@ -903,19 +917,24 @@ _denv_init() {
denv_workspace="$1"
# check if this directory exists, if not create it
if [ ! -d "${denv_workspace}" ]; then
if [ "${force}" = "0" ]; then
_denv_info "creating ${denv_workspace} for the denv workspace"
if [ -n "${mkwork+x}" ]; then
if ${mkwork}; then
_denv_info "user allows denv to create ${denv_workspace}"
else
_denv_info "refusing to create a directory per user instruction"
return 0
fi
elif [ -n "${DENV_NOPROMPT+x}" ]; then
_denv_error "denv prompt disabled but unwilling to create a directory without user input."
return 1
elif ! _denv_user_confirm "This directory does not exist. Would you like to create it for the denv?"; then
_denv_info "Exiting without creating '${denv_workspace}'..."
_denv_info "exiting without creating '${denv_workspace}'..."
return 0
fi
_denv_info "Creating '${denv_workspace}' for the denv..."
_denv_info "creating '${denv_workspace}' for the denv..."
mkdir "${denv_workspace}"
fi
_denv_info "Entering '${denv_workspace}'..."
_denv_info "entering '${denv_workspace}'..."
cd "${denv_workspace}"
fi
# check if there is a workspace here or "above" (in a parent directory)
Expand All @@ -924,13 +943,18 @@ _denv_init() {
if [ "${denv_workspace}" = "${PWD}" ]; then
verb="overwrit"
fi
if [ "${force}" = "0" ]; then
_denv_info "${verb}ing previous denv workspace"
if [ -n "${over+x}" ]; then
if ${over}; then
_denv_info "${verb}ing previous denv workspace at ${denv_workspace}"
else
_denv_info "refusing to ${verb}e previous denv workspace per user instruction"
return 0
fi
elif [ -n "${DENV_NOPROMPT+x}" ]; then
_denv_error "denv prompt disabled but unwilling to ${verb}e a denv without user input."
return 1
elif ! _denv_user_confirm "This workspace already has a denv (in ${denv_workspace}). Would you like to ${verb}e it?"; then
_denv_info "Exiting without ${verb}ing denv within '${denv_workspace}'..."
_denv_info "exiting without ${verb}ing denv within '${denv_workspace}'..."
return 0
fi
fi
Expand All @@ -952,16 +976,12 @@ _denv_init() {
denv_shell="/bin/bash -i"
denv_mounts=""
denv_network="true"
if [ "${copyall}" = "1" ]; then
denv_env_var_copy_all="true"
else
denv_env_var_copy_all="false"
fi
denv_env_var_copy_all="${copyall}"
denv_env_var_copy=""
denv_env_var_set=""
_denv_write_config
if [ "${gitignore}" = "1" ]; then
_denv_info "Writing a gitignore for the .denv directory."
if ${gitignore}; then
_denv_info "writing a gitignore for the .denv directory."
cat > "${denv_workspace}/.denv/.gitignore" <<\GITIGNORE
# ignore everything in this directory
*
Expand Down
29 changes: 26 additions & 3 deletions man/man1/denv-init.1
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
denv init
.SH SYNOPSIS
.PP
\f[B]denv init\f[R] [help|-h|\[en]help] IMAGE [WORKSPACE]
[\[en]no-gitignore] [\[en]clean-env|\[en]no-copy-all] [\[en]force]
[\[en]name NAME]
\f[B]denv init\f[R] [help|-h|--help] IMAGE [WORKSPACE]
[--no-gitignore] [--clean-env|--no-copy-all] [--force]
[--name NAME] [--no-mkdir|--mkdir] [--no-over|--over]
.SH OPTIONS
.PP
\f[B]\f[CB]--help\f[B]\f[R], \f[B]\f[CB]-h\f[B]\f[R], or
Expand All @@ -32,6 +32,13 @@ the current workspace has one
.PP
\f[B]\f[CB]--name\f[B]\f[R] sets the name for the denv workspace that is
being initialized to NAME
.PP
\f[B]\f[CB]--[no-]mkdir\f[B]\f[R] don't prompt about if denv can create the
workspace directory. Just do it (--mkdir) or not (--no-mkdir).
.PP
\f[B]\f[CB]--[no-]over\f[B]\f[R] don't prompt about if denv should overwrite
a configuration within the workspace or override a configuration in a parent directory.
Just do it (--over) or not (--no-over).
.SH ARGUMENTS
.PP
\f[B]\f[CB]IMAGE\f[B]\f[R] the name of a container image to use when
Expand Down Expand Up @@ -97,6 +104,22 @@ different names.
denv init python:3.11 py311
\f[R]
.fi
.PP
As a standalone program, \f[B]denv\f[R] can be used within other scripts
and support programs.
With this in mind, a common process is to have a denv configuration that
can be ensured to exist for the rest of additional tasks.
The following example uses the --no-mkdir and --no-over flags to silently
ensure that the present working directory has the python:3.11 image configured.
.IP
.nf
\f[C]denv init --no-mkdir --no-over python:3.11\f[R]
.fi
.PP
WARNING: \f[B]denv init\f[R] does not ensure that the image is the same as
the one passed, it just quielty refused to overwrite an existing configuration
allowing it to be quickly bypassed if the configuration has already been made
or initialize the configuration if no configuration is present.
.SH DEFAULT CONFIGURATION
.PP
\f[B]denv init\f[R] makes the following choices on configuration that
Expand Down
41 changes: 37 additions & 4 deletions test/init.bats
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,51 @@ teardown() {
@test "denv should not init twice" {
run denv init alpine:latest
assert_success
run ! denv init alpine:latest
run -1 denv init alpine:latest
assert_failure
# but can be forced
run -0 denv init --force alpine:3.19
}

@test "denv can init twice with force" {
run denv init alpine:latest
assert_success
run denv init --force alpine:3.19
assert_success
assert_file_contains .denv/config '^denv_image="alpine:3.19"$'
run denv init --over alpine:latest
assert_success
assert_file_contains .denv/config '^denv_image="alpine:latest"$'
}

@test "denv can skip double init without force" {
run denv init alpine:latest
assert_success
run denv init --no-over alpine:3.19
assert_success
assert_file_contains .denv/config '^denv_image="alpine:latest"$'
}

@test "denv should not init inside another denv" {
run denv init alpine:latest
assert_success
mkdir subdir
cd subdir
run ! denv init alpine:latest
run -1 denv init alpine:latest
}

@test "denv can force init inside another denv" {
run denv init alpine:latest
assert_success
mkdir subdir
cd subdir
run denv init --over alpine:latest
assert_success
}

@test "denv will not create new directory by default" {
run -1 denv init alpine:latest subdir
}

@test "denv can be told to create new directory by default" {
run denv init --mkdir alpine:latest subdir
assert_success
}

0 comments on commit 41960e7

Please sign in to comment.