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
}
Related
In Ruby, * is used to represent the name of a file.
For example, /home/user/*.rb will return all files ending with .rb. I want to do something similar in Chef InSpec.
For example:
describe file ('home/user/*') do
it {should_not exist }
end
It should give me all the files inside /home/user directory and check that no file exists inside this directory. In other words, I want to check if this directory is empty or not in Chef.
How can I do that?
* for globs is mostly a shell feature, and as you might expect the file resource doesn't support them. Use a command resource instead:
describe command('ls /home/user') do
its(:stdout) { is_expected.to eq "\n" }
end
Here's an alternate approach that tests for the existence of the directory, and if it exists it uses Ruby to test for files within it. It also uses the expect syntax, which allows for a custom error message.
control 'Test for an empty dir' do
describe directory('/hey') do
it 'This directory should exist and be a directory.' do
expect(subject).to(exist)
expect(subject).to(be_directory)
end
end
if (File.exist?('/hey'))
Array(Dir["/hey/*"]).each do |bad_file|
describe bad_file do
it 'This file should not be here.' do
expect(bad_file).to(be_nil)
end
end
end
end
end
If there are files present, the resulting error message is informative:
× Test for an empty dir: Directory /hey (2 failed)
✔ Directory /hey This directory should exist and be a directory.
× /hey/test2.png This file should not be here.
expected: nil
got: "/hey/test2.png"
× /hey/test.png This file should not be here.
expected: nil
got: "/hey/test.png"
I'm writing a Mac OS program, and I have the following lines:
os.execute("cd ~/testdir")
configfile = io.open("configfile.cfg", "w")
configfile:write("hello")
configfile:close()
The problem is, it only creates the configfile in the scripts current directory instead of the folder I have just cd' into. I realised this is because I'm using a console command to change directory, then direct Lua code to write the file. To combat this I changed the code to this:
configfile = io.open("~/testdir/configfile.cfg", "w")
However I get the following result:
lua: ifontinst.lua:22: attempt to index global 'configfile' (a nil value)
stack traceback:
ifontinst.lua:22: in main chunk
My question is, what's the correct way to use IO.Open to create a file in a folder I have just created in the users home directory?
I appreciate I'm making a rookie mistake here, so I apologise if you waste your time on me.
You have problems with ~ symbol. In your os.execute("cd ~/testdir") is the shell who interprets the symbol and replaces it by your home path. However, in io.open("~/testdir/configfile.cfg", "w") is Lua who receives the string and Lua doesn't interprets this symbol, so your program tries to open a file in the incorrect folder. One simple solution is to call os.getenv("HOME") and concatenate the path string with your file path:
configfile = io.open(os.getenv("HOME").."/testdir/configfile.cfg", "w")
In order to improve error messages I suggests you to wrap io.open() using assert() function:
configfile = assert( io.open(os.getenv("HOME").."/testdir/configfile.cfg", "w") )
Using the application task I am specifying:
applicationDefaultJvmArgs = ['$DEBUG_OPTS',
'-Djava.library.path=${ZMQ_LIB_PATH}']
In the generated start scripts I see:
DEFAULT_JVM_OPTS='"\$DEBUG_OPTS" "-Djava.library.path=\${ZMQ_LIB_PATH}"'
I don't want the \$ in there. I tried using '$$DEBUG_OPTS' and also '\$DEBUG_OPTS' but got the same result. What is the right way to escape the $ so it ends up in the script without a backslash in front of it?
I had a similar issue, trying to add a commandline parameter $1 in there. With some googling came up with this solution, fixing the script after the fact.
applicationDefaultJvmArgs=['-Dmy.property=DOLLARONE']
...
startScripts{
doLast{
def bashFile = new File(getOutputDir(),applicationName)
String bashContent = bashFile.text
bashFile.text = bashContent.replaceFirst('DOLLARONE', Matcher.quoteReplacement('$1'))
}
}
The StartScriptGenerator code implies that '$' will be unconditionally replaced by the '\$'.
I assume that your intention is to use '$' character for shell parameters extension but I would like to point out that such usage (if permitted by the gradle task that generates the scripts) is not interoperable between bash and bat scripts - in the bash it will be used for shell parameters extension but in the bat it will have no meaning.
For Kotlin build script the solution could look like:
tasks.named<CreateStartScripts>("startScripts") {
doLast {
unixScript.writeText(unixScript.readText().replace("{{APP_HOME}}", "\${APP_HOME}"))
windowsScript.writeText(windowsScript.readText().replace("{{APP_HOME}}", "%APP_HOME%"))
}
}
I currently use following valadoc build task to generate a api documentation for my vala application:
doc = bld.new_task_gen (
features = 'valadoc',
output_dir = '../doc/html',
package_name = bld.env['PACKAGE_NAME'],
package_version = bld.env['VERSION'],
packages = 'gtk+-3.0 gee-1.0 libxml-2.0 x11 gdk-x11-3.0 libpeas-gtk-1.0 libpeas-1.0 config xtst gdk-3.0',
vapi_dirs = '../vapi',
force = True)
path = bld.path.find_dir ('../src')
doc.files = path.ant_glob (incl='**/*.vala')
This tasks creates a directory html in the output directory including several subdirectories with html and picture files.
What I am know trying to do is to install such files to /usr/share/doc/projectname/html/. To do so I added the following to the wscript_build file (following the documentation I have found here):
output_dir = doc.bld.path.find_or_declare('../doc/html')
doc.outputs = output_dir.ant_glob (incl='**/*')
doc.bld.install_files('${PREFIX}/share/doc/projectname/html', doc.outputs)
However this leads to an error "Missing node signature". Does anyone know how to get around this error? Or is there a simple way to install a directory recursively with waf?
You can find a full-fledge sample here.
I had a similar issue with generated files and had to update the signature for the corresponding Node objects. Try creating a task:
def signature_task(task):
for x in task.generator.bld.path.find_dir('../doc/html').ant_glob('**/*', remove=False):
x.sig = Utils.h_file(x.abspath())
To the top of you build rule, try adding:
#Support running task groups serially
bld.post_mode = Build.POST_LAZY
Then at the end of your build, add:
#Previous tasks belong to a group
bld.add_group()
#This task runs last
bld(rule=signature_task, always=True, name="signature_task")
There is an easier way using relative_trick.
bld.install_files(destination,
bld.path.ant_glob('../doc/html/**'),
cwd=bld.path.find_dir('../doc/html'),
relative_trick=True)
This gets a list of files from the glob, chops off the prefix, and puts it into the destination.
I can make it work this way
Dir.chdir(basedir)
puts Dir.glob("#{filename}*").inspect
Is there any way to do so using only one command? I want to list
all files that stat with filename
the the directory basedir
Update 1
puts "#{csv_dir_name}\\#{testsuite}*"
puts Dir["#{csv_dir_name}\\#{testsuite}*"].inspect
retuns
C:\Program Files\TestPro\TestPro Automation Framework\Output Files\builds\basics\logs\basics\2011\07\07114100\login*
[]
on the other hand, this code works fine
Dir.chdir(csv_dir_name)
csv_file_name = Dir.glob("#{testsuite}*")
I think this should do what you want:
puts Dir["#{basedir}/#{filename}*"]
Or alternatively:
puts Dir["#{File.join(basedir, filename)}*"]
previous working directory is restored when the command executed:
Dir.chdir(basedir) { puts Dir.glob("#{filename}*").inspect }