Write environment variable to a file in Gradle - gradle

I need to save an environment variable into a file in Gradle.
I have tried to use the below code, but it just writes null.
def file1 = new File('.test')
file1 << System.getenv(“MY_PW")
Also it is required to check, if any contents previously exist, then it should replace with the System.getenv(“MY_PW").
Any help please, how to achieve this?

If null is written to the file, then the environment variable seems to not be defined, because the code is fine.
To replace the contents instead of appending to it, use file1.text = ... instead of file1 << ....

Related

How i can Insert contents of text file with heredoc and variable with ruby

There was a problem when im creating the CLI. I'm want to give the user the opportunity to insert their data into a text file, for this I created a file and added a heredoc to it
I'm trying to get data from a text document that has a heredoc inside of it with a function that is supposed to interpolate
When I try to display the result of the file, I get the entire contents of the file, including the heredoc
an example will be below
I tried to solve my problem through File class
variable_name = File::open("path_directory/file_with_heredoc.txt", "r+")::read
Next, I decided to give the value of the variable to the terminal via
exec("echo #{variable_name}")
The terminal displays
file = <<-EOM
single text with def result: #{upcase_def("Hello")}
EOM
Tried to give through struct, but result is unchanged
exec("echo #{variable_name.strip}")
What do I need to do to get only data, no HEREDOC syntax?
I want to get this result
"single text with def result: HELLO"
I think this is what you are trying to do but I recommend you to first do some research why 'eval() is evil'. If the file is a user (or hacker) input you definitely want some sanitization there or a completely different approach.
def upcase_def(str)
str.upcase
end
data = File.read('file_with_heredoc.txt')
eval(data)
# => " single text with def result: HELLO\n"

Add shell script output value in a Jenkins ArrayList variable

def path = ....
def files = []
sh "for file in $path/*.json; do files.add(file); done"
echo ${files}
Error I get in jenkins: /jenkins/workspace/....."syntax error near unexpected token 'file'
Can someone help me as to how can I add file in files? I tried looking for answers but couldn't find anything useful which solved my scenario.
I want to add the file variable inside Arraylist variable files so that I can fire curl command for each file in my Jenkins pipeline.
Also needed to know is there some way I can test the script before deploying it on any environment?
If you want to get a List JSON files in a directory as a List you can use the following code.
path = "/home/your/path"
dir(path) {
def files = findFiles(glob: '**/*.json')
println files.size()
println files[0].name
}

How check is my file create/modyfy after other file?

I need a function similar 'make' program.
If my file not exist or if file need update (modyfy time is before my other file) tell me true.
I have one file dependencies to other file. How update it only if neccesary.
You might want to use FileUtils.uptodate?.
uptodate?(new, old_list)`
Returns true if new is newer than all old_list. Non-existent files are older than any file.
In your example you can use it like this:
unless FileUtils.uptodate?('file_a', ['file_b'])
# file_a needs to be updated
end
require 'time_difference'
TimeDifference.between(File.ctime("FilenameA"), File.ctime("FilenameB.txt")).humanize
Using gem 'time_difference'
trouble with other lang than english (withouth .humanize You get number)

Elixir : Feed content from a file to a function in command line

I have a csv type file. I want to be able to call a function from the command line, and feed it the content of this file - not the file itself.
I tried to call
mix run lib/my_module.ex $(cat tmp.txt)
having in the end of my module file :
IO.puts MyModule.my_func(System.argv)
Content is correctly processed to the func, but System.argv being a list of strings, it lost its format and is not possible to parse correctly.
If I instead try to
mix run -e "MyModule.my_func(:args)"
I can't find how to feed it the content of the file, using cat or something else.
How to make it work ?
Try to put between quotes, like this: mix run lib/my_module.ex "$(cat tmp.txt)"
This looks like XY Problem to me. Pass the name of the file only and do File.read/1 inside your module.
Somewhat like:
mix run lib/my_module.ex tmp.txt
and in your module:
System.argv
|> File.read!()
|> MyModule.my_func()
|> IO.puts()

shell cd command in Ruby

Im trying to execute shell commands using ruby, but i cant change directory to PATH with blank spaces.
variable = %x[cd #{ENV["HOME"]}/Virtual\\ VMs/]
This is not working.
Thank you
To be absolutely safe:
path = File.join [ENV["HOME"], 'Virtual VMs']
variable = %x[cd '#{path}']
Please note, that cd has empty output, so to make sure it works one probably wants to do smth like:
path = File.join [ENV["HOME"], 'Virtual VMs']
variable = %x[cd '#{path}' && ls -la]
#⇒ "total 32\ndrwxr-xr-x ....."
What is ist supposed to do? You try to chdir into a directory, but then don't do anything in it. Your variable will be empty in any case. Aside from the fact that it is pointless to do, you can not reliably execute a cd by itself in this way, because it is not an executable file. You can see this if you just execute %x[cd]. You will get an Errno::ENOENT exception.
Maybe you should first describe in a broader context, what you want to achieve with your code. Where would you like to change the working directory? Within the Ruby process - in which case you have to use Dir.chdir - or in the child process - in which case you have to execute some command after the cd.

Resources