Skip to content

Commit

Permalink
improve yaml rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
rcannood committed Aug 14, 2024
1 parent aaea0ef commit 147290d
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 104 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function ViashParseArgumentValue {
local env_name="$1"
local multiple="$2"
local flag="$3"
local value="$4"

if [ $# -lt 4 ]; then
ViashError "Not enough arguments passed to ${flag}. Use '--help' to get more information on the parameters."
exit 1
fi

if [ "$multiple" == "false" ]; then
# check whether the variable is already set
if [ ! -z ${!env_name+x} ]; then
local -n prev_value="$env_name"
ViashError "Pass only one argument to argument '${flag}'. Found: ${prev_value@Q} & ${value@Q}"
exit 1
fi

# set the variable
declare -g "$env_name=${value}"
else
local new_values

local -n prev_values="$env_name"

# todo: allow escaping the delimiter
readarray -d ';' -t new_values < <(printf '%s' "$value")

combined_values=( "${prev_values[@]}" "${new_values[@]}" )

declare -g -a "$env_name=(\"\${combined_values[@]}\")"
fi
}
31 changes: 12 additions & 19 deletions src/main/resources/io/viash/helpers/bashutils/ViashRenderYaml.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@


function ViashRenderYamlQuotedValue {
local key="$1"
local value="$2"
if [ "$value" == "UNDEFINED_ITEM" ]; then
echo "null"
return
fi
# escape quotes, backslashes and newlines, and then surround by quotes
echo "$value" | sed 's#"#\\"#g' | sed 's#\\#\\\\#g' | sed ':a;N;$!ba;s/\n/\\n/g' | sed 's#^#"#g' | sed 's#$#"#g'
echo "$value" | \
sed 's#\\#\\\\#g' | \
sed 's#"#\\"#g' | \
sed ':a;N;$!ba;s/\n/\\n/g' | \
sed 's#^#"#g;s#$#"#g'
}

function ViashRenderYamlBooleanValue {
local key="$1"
local value="$2"
if [ "$value" == "UNDEFINED_ITEM" ]; then
echo "null"
return
fi
# convert to lowercase
value=$(echo "$value" | tr '[:upper:]' '[:lower:]')
if [[ "$value" == "true" || "$value" == "yes" ]]; then
echo "true"
Expand All @@ -32,10 +27,6 @@ function ViashRenderYamlBooleanValue {
function ViashRenderYamlUnquotedValue {
local key="$1"
local value="$2"
if [ "$value" == "UNDEFINED_ITEM" ]; then
echo "null"
return
fi
echo "$value"
}

Expand All @@ -44,12 +35,12 @@ function ViashRenderYamlKeyValue {
local type="$2"
local multiple="$3"
shift 3
local out="$key: "

local out=" ${key}: "

if [ "$1" == "UNDEFINED" ]; then
out+="null"
echo $out
echo "$out"
return
fi

Expand All @@ -65,7 +56,9 @@ function ViashRenderYamlKeyValue {
else
out+=", "
fi
if [[ "$type" == "string" || "$type" == "file" ]]; then
if [ "$value" == "UNDEFINED_ITEM" ]; then
out+="null"
elif [[ "$type" == "string" || "$type" == "file" ]]; then
out+="$(ViashRenderYamlQuotedValue "$key" "$value")"
elif [[ "$type" == "boolean" || "$type" == "boolean_true" || "$type" == "boolean_false" ]]; then
out+="$(ViashRenderYamlBooleanValue "$key" "$value")"
Expand Down
3 changes: 1 addition & 2 deletions src/main/scala/io/viash/helpers/Bash.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ object Bash {
lazy val ViashRemoveFlags: String = readUtils("ViashRemoveFlags")
lazy val ViashAbsolutePath: String = readUtils("ViashAbsolutePath")
lazy val ViashDockerAutodetectMount: String = readUtils("ViashDockerAutodetectMount")
// backwards compatibility, for now
lazy val ViashAutodetectMount: String = ViashDockerAutodetectMount.replace("ViashDocker", "Viash")
lazy val ViashSourceDir: String = readUtils("ViashSourceDir")
lazy val ViashFindTargetDir: String = readUtils("ViashFindTargetDir")
lazy val ViashDockerFuns: String = readUtils("ViashDockerFuns")
lazy val ViashLogging: String = readUtils("ViashLogging")
lazy val ViashParseArgumentValue: String = readUtils("ViashParseArgumentValue")

/**
* Access the parameters contents in a bash script,
Expand Down
Loading

0 comments on commit 147290d

Please sign in to comment.