bugy / script-server

Web UI for your scripts with execution management
Other
1.61k stars 249 forks source link

Text type parameters values forced with quotes #436

Closed rbrdevs closed 3 years ago

rbrdevs commented 3 years ago

Hi @bugy ,

Is there any possibility to return the parameter value with quotes? (single or double) I always get parameters values without quotes.

--name John Smith --optional-arg

It could be nice if Text field types (at least) could be forced to return quoted. Ej:

  "parameters": [
    {
      "name": "Name",
      "required": false,
      "param": "--name",
      "same_arg_param": false,
      "type": "text",
      "value_quoted": true
    },

Thanks for your great work!!

bugy commented 3 years ago

Hi @rbrdevs, why do you need quotes there? To be honest, I have a feeling, that somewhere in your script, the value is split by whitespace.

rbrdevs commented 3 years ago

Hi @bugy, thanks for the quick response!

Well, maybe you're right. But in some situations I need to parse the entire arguments line by using a regex command. In this case It's easier if I could get the parameters values enclosed in quotes. I thought It could be useful and easy to implement in code and maybe helps other people with the same problem.

It's just a suggestion, but anyway thanks again for your work!

bugy commented 3 years ago

Just to better understand it, how are you doing it, if it's not a secret? I mean, where are you getting this argument line from?

Another problem, which I see here, imagine the following script:

#!/bin/bash

echo "Hello $1"

This would print Hello "world" (with quotes). I.e. quotes would be a part of the used string and not just a wrapper for whitespace. You could work around this problem by using echo 'Hello '$1, but it's a bad practice to use unquoted values in scripts

PS sorry if it sounds like "you don't need it", but I think it's beneficial for me to understand your usecase, or may be advice about better approach.

rbrdevs commented 3 years ago

Not, it's not a secret. It's my lack of knowledge in shell scripting for sure xD When I use your example code it doesn't return the string quotes. Don't know why. This is what I get on script-server terminal:

Hello Param one

On the one hand, in some cases I need to alter some parameters values returned by script-server to fix some paths in order to pass them to one python script executed in a docker container where the path differes from one obtained by a "server_file" type parameter, for example. In this situations I tried to get the full arguments line to parse with regex and "fix" the path. For example, this example script fixes one txt type parameter from script-server.

#!/bin/bash

ARGS=$*
echo "ALL ARGUMENTS: $ARGS"
echo "ARG1 value: $2"

ARGS1_TXT_FIXED=$(echo $* | sed -E 's/.*(--txt-param1 ([^-]+)) .*/\2/g') 

if [ -z "$ARGS1_TXT_FIXED" ]
then
    ARGS1_TXT_FIXED=""
else
    ARGS1_TXT_FIXED="--txt-param1 '/workspace/data/input/$ARGS1_TXT_FIXED'"
fi

echo "FIXED ARGUMENT: $ARGS1_TXT_FIXED"

docker-compose run --rm service-prod python3 testargs.py $ARGS1_TXT_FIXED 

Some parameters are optional and it's difficult to make this fix based on their position. In other cases I'm using "getops" with longoptions to validate and fix the arguments values, but the problem is that I need to fix the parameter value prior to pass to the python script.

This is what I get on the script-server terminal:

ALL ARGUMENTS: --txt-param1 Param one --txt-param2 Param two --check-param --list-param option1
ARG1 value: Param one
FIXED ARGUMENT: --txt-param1 '/workspace/data/input/Param one'
Creating service-prod_run ... done
usage: testargs.py [-h] --txt-param1 TXT_PARAM1
testargs.py: error: unrecognized arguments: one'
ERROR: 2

On the other hand, I thought that my problem will be solved when adding this quote manually. But, as you can see, the error persists even with the fixed parameter. The python script fails always when the parameter value has spaces, even with quotes...¿? So, it seems that the new parameter option will not solve this problem. This only will help with the string parsing stuff when value overwrite is needed. Maybe the problem could be related to the form in which "docker-compose" pass the parameters to the container execution. I'll investigate it more deeply. Thanks for your interest!

bugy commented 3 years ago

Please wrap the argument with quotes, when you call your pyhton script: docker-compose run --rm service-prod python3 testargs.py "$ARGS1_TXT_FIXED"

It's a good idea to always wrap your variables with double quotes, otherwise it's effectively the same as calling docker-compose run --rm service-prod python3 testargs.py Param one

It can also be a subject to script injection, e.g. if ARGS1_TXT_FIXED="smth || rm /", the executed command would be docker-compose run --rm service-prod python3 testargs.py smth || rm / and the rm / part will be executed

rbrdevs commented 3 years ago

Ok, good advice! Sometimes I simply reuse the original param list from script-server doing like this:

ARGS=$*
docker-compose run --rm service-prod python3 testargs.py $ARGS

In this case you can't double quote the variable "$ARGS" in order to all incoming parameters work well in dockerized python script. But this is also the case in which parameters with spaces fail. So, as you say, the best and safest solution is to validate and recompose all the argument list and double quote one by one. I've checked that is necessary to use "=" in the expression. For example: docker-compose run --rm greenfield-service-prod python3 testargs.py "$ARGS1_TXT_FIXED" It works, ARGS1_TXT_FIXED="--txt-param1=$ARGS1_TXT_FIXED" but It doesn't, ARGS1_TXT_FIXED="--txt-param1 $ARGS1_TXT_FIXED"

Thank you for your help and keep up the good work!

bugy commented 3 years ago

Please take a look here: https://stackoverflow.com/a/3816747