Skip to content

Commit

Permalink
allow for denv name to be an invalid hostname (tomeichlersmith#155)
Browse files Browse the repository at this point in the history
We allow for underscores in the name because they are often used within directory names which are the default names for the denv. Any other special character should trigger an error during `denv init` that informs the user of the issue in a more helpful manner than the (currently `apptainer`-only) error message from the underlying runner.
  • Loading branch information
tomeichlersmith authored Feb 21, 2025
1 parent cb677d7 commit 8ac7b40
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
29 changes: 28 additions & 1 deletion denv
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ _denv_environment_to_args() {
echo "${env_args}"
}

# check if the passed string has any invalid special characters
# Arguments
# 1 - string to check
# Outputs
# 0 if it does have invalid special characters, 1 otherwise
_denv_has_invalid_char() {
bad_char="$(echo "${1}" | tr -d "[:alnum:]._-")"
test -n "${bad_char}"
}

###################################################################################################
# container runner interface
#
Expand Down Expand Up @@ -225,7 +235,14 @@ _denv_pull() {
# as well as the DENV_* variables
# - set SHELL to be ${denv_shell} in container
_denv_run() {
_hostname="${denv_name}.$(uname -n || true)"
# valid hostnames only consist of lower and upper case letters, numbers, and the hyphen
# we want to use the name of the workspace as a hostname in the container
# so that programs in the container can show that they are in the container (e.g. the bash prompt)
# with this in mind, we translate underscores into hyphens so
# that the workspace name (by default taken from the directory name) can have underscores
# we do not check for other special characters
valid_hostname="$(printf '%s' "${denv_name}" | tr '_' '-')"
_hostname="${valid_hostname}.$(uname -n || true)"
# we will be running now and not writing the config
# so we can update the denv_mounts list
[ -d /tmp/.X11-unix ] && denv_mounts="${denv_mounts} /tmp/.X11-unix"
Expand Down Expand Up @@ -905,6 +922,11 @@ _denv_init() {
return 1
fi
denv_name="${2}"
if _denv_has_invalid_char "${denv_name}"; then
_denv_error "The name you passed '${denv_name}' contains invalid special characters." \
"The name for the denv can only contain letters, numbers, underscores, and hyphens."
return 2
fi
shift
;;
*)
Expand Down Expand Up @@ -983,6 +1005,11 @@ _denv_init() {
# set the default denv name to the workspace directory name
if [ -z "${denv_name+x}" ]; then
denv_name="$(basename "${denv_workspace}")"
if _denv_has_invalid_char "${denv_name}"; then
_denv_error "The default name for this denv '${denv_name}' taken from the name of the directory has invalid special characters." \
"Either rename this directory or use --name to provide a name without special characters."
return 2
fi
fi
# we have a clean workspace directory, lets make a new denv
denv_image="${image}"
Expand Down
16 changes: 16 additions & 0 deletions test/init.bats
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,19 @@ teardown() {
cd subdir
run -1 denv init --no-over alpine:latest
}

@test "can init and run with under score in workspace name #154" {
run denv init --mkdir alpine:latest under_score
cd under_score
run -0 denv true
}

@test "refuse to init with name that has special characters #154" {
run -2 denv init alpine:latest --name "one+two"
}

@test "refuse to init with directory that has special characters #154" {
mkdir "one?two"
cd "one?two"
run -2 denv init alpine:latest
}

0 comments on commit 8ac7b40

Please sign in to comment.