I have a call to Ant exec task that needs to accept input from console stdin. Unfortunately, I cannot find a way to do this. The stdin filehandle seems closed to console input at runtime.
It is possible to specify input using attributes input and inputstring. However, I need to prompt for user input at the console.
How can I do this?
From the documentation of the task:
Note that you cannot interact with the
forked program, the only way to send
input to it is via the input and
inputstring attributes. Also note that
since Ant 1.6, any attempt to read
input in the forked program will
receive an EOF (-1). This is a change
from Ant 1.5, where such an attempt
would block.
You could try to use the input-task to prompt for input in the ant-buildfile and pass these input with the inputstring-attribute of exec. I can't think of a better option at the moment.
Related
I'm invoking the <java> task using Ant to run a program that prints out some stuff to stdout that ultimately, I'd like the user to be able to copy-paste easily. However, each line of stdout is prefixed with [java], which makes things needlessly challenging for the user.
Is there some way to print just the output of System.out.println(...) without getting prefixed with [java]?
Set ANT_ARGS=-emacs, see ant FAQ:
Ant adds a "banner" with the name of the current task in front of all
logging messages - and there are no built-in regular expressions in
your editor that would account for this.
You can disable this banner by invoking Ant with the -emacs switch.
[...]
Also ant manual Running Apache Ant listing all cli options might be helpful :
-emacs, -e produce logging information without adornments
I have an ActiveJob which triggers a system script to run:
`grunt custom-job --src=files --dest="file" --vars='#{user_input_vars_from_json}'`
Point being is that
user_input_vars_from_json
Is a json config which comes as user input parameter from a controller.
I do validate the json format but how can I ensure that there is no harmful code send to my system command?
I would just like to preface this with: Any user input should be treated as dangerous. I would not recommend executing any command using user-provided inputs.
The first thing you're going to want to do is lock down the input as much as possible. Consider restricting the length of the user_input_vars_from_json to prevent buffer overflow and DoS attacks. I also recommend trying to figure out a way to both validate and restrict the "vars" you are trying to set in the user_input_vars_from_json JSON to filter out any unwanted keys/values.
Once your input is cleaned, you can use Kernel#system in combination with Shellwords to get as close to safe as possible in executing your command from your job:
require 'shellwords'
system("grunt", "custom-job", "--src=files", '--dest="file"', "--vars=\"#{Shellwords.escape(user_input_vars_from_json)}\""
I am very new to ruby on rails and I have a very simple question.
I am able to read the user input from a text field in ruby on rails
#user = params[:user]
I have to pass this value as an argument to a shell script
I tried a number of things, but nothing works
run_command "./file.sh #{#user}"
and
%x(./file.sh #{#user})
The script receives "{user="
Any help is appreciated!
First, make sure you escape any parameters you pass to command line. You may be easily attacked via command line injection this way. Try this instead:
system "/path/to/file.sh #{#user.shellescape}"
You may also want to try exec, depending on how you want to track output. Please see http://ruby-doc.org/core-2.3.0/Kernel.html for more details on both
Im trying to write a logic that depends on the success or failure of cccheckout command. Is there something similar to returnProperty attribute as in exec task?
Thanks,
Aarthi
Looking at the CCCheckout documentation, I would rather use the exception mechanism to process any failure.
failonerr
Throw an exception if the command fails. Default is true.
Since ant 1.6.1
In ant, that means you can separate your ant process in two (one if no failure, one one exception), using the ant trycatch task.
It's possible you're asking that question because of cleartool.exe behaves strangly
sometimes, means it returns RC -1 even if no real error occured.
Means using cccheckout with failonerr="true" would sometimes cause an unneccessary Build failed
as any RC != 0 is handled as error by exec task.
But you might use the <exec> task directly with executable cleartool.exe and set attribute
resultproperty to make the RC available as property
outputproperty to make stdout available as property
errorproperty to make stderr available as property
for further processing, i.e. use condition task to check those properties..
Some try/catch/finally feature provided by Ant addons like :
AntContrib
Flaka
might come in handy, as VonC already mentioned.
If it gets more complicated afterwards use Groovy ant task or script task.
I use SecureInputHandler to accept passwords from the end user in ant script, but it forces user to press Enter before it allows text input.
Is this expected behavior? Also, I didn't find much documentation on this topic, please feel free to point me to any resources on this.
<input message="Please enter password:" addproperty="password.property">
<handler classname="org.apache.tools.ant.input.SecureInputHandler" />
</input>
SecureInputHandler requires Ant 1.7.1 or greater (to support the handler) and Java 6 or greater (to provide the Console class).
If you don't have the right Ant version, you'll get an error. If you don't have the right Java version, Ant falls back to the default input handler.
Note that you can also use:
<handler type="secure"/>
The javadoc and source can be seen here.
Using Ant 1.8.2 and Java 1.6 in a windows cmd shell, I get this:
test:
Please enter password:<cursor here>
The password is masked. You type the password and hit Enter.
Using a Cygwin shell, or Java 1.5 in cmd shell, I get this:
test:
[input] Please enter password:
<cursor here>
The password is not masked. You type the password and hit Enter.
Using Eclipse console, I couldn't get the password to enter at all. I type, it is echoed on the console, I press Enter, nothing happens.
I notice that in the documentation of the Input task, it says:
IDE behaviour depends upon the IDE: some hang waiting for input, some
let you type it in. For this situation, place the password in a
(secured) property file and load in before the input task.
In other words, it may not work in an IDE, use a work around.
exec was interfering with the input tasks.
It can be solved by specifying inputstring=”” in the exec task.
See exec causes other tasks to hang or leads to strange behaviour of <input> tasks in the Ant FAQ.