From 8ac7b40084afce83f851139917af6bdbc836219e Mon Sep 17 00:00:00 2001 From: Tom Eichlersmith <31970302+tomeichlersmith@users.noreply.github.com> Date: Fri, 21 Feb 2025 14:59:30 -0600 Subject: [PATCH] allow for denv name to be an invalid hostname (#155) 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. --- denv | 29 ++++++++++++++++++++++++++++- test/init.bats | 16 ++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/denv b/denv index 3d44b44..cc86bdc 100755 --- a/denv +++ b/denv @@ -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 # @@ -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" @@ -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 ;; *) @@ -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}" diff --git a/test/init.bats b/test/init.bats index 63b7eab..5aabc84 100644 --- a/test/init.bats +++ b/test/init.bats @@ -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 +}