Ruhi Singh
Ruhi Singh

Reputation: 186

How to copy a file in capistrano script without throwing error if source file does not exist?

I am using capistrano to deploy my php application.I have a requirement to copy a list of configuration files from previous release to new release. The list is maintained in an array. When I am looping over this array to copy from previous to current release , in case the source file is not found it throw an error and further execution stops. I want the script to ignore such case and keep executing next command printing a simple message if source file does not exist. I tried using command like following, but no luck: run "test -f /tmp/myfile && cp -p /tmp/myfile /home/admin

or even

if(File.exists?("/tmp/myfile"))

 run "cp -p /tmp/myfile /home/admin" 


else

  print " file doesnot exist"

end

Thanks in advance !!

Upvotes: 0

Views: 616

Answers (1)

Faiz
Faiz

Reputation: 16255

I'd go with this:

run <<-CMD
  if [ -f /tmp/myfile ]; then \
    cp -p /tmp/myfile /home/admin; \
  else \
    echo 'myfile does not exist'; \
  fi
CMD

Remember that all capistrano run commands are executed on the remote server, and only an exit value of 0 indicates success. The result of "test -f /tmp/myfile && cp -p /tmp/myfile /home/admin would still be 1 if /tmp/myfile does not exist. You could use || to call echo with a message that the file does not exist:

test -f /tmp/myfile && cp -p /tmp/myfile /home/admin && echo myfile does not exist

Which is the same thing.

Upvotes: 1

Related Questions