How can you check to see if a file exists (on the remote server) in Capistrano?

前端 未结 5 887
忘了有多久
忘了有多久 2020-12-24 01:14

Like many others I\'ve seen in the Googleverse, I fell victim to the File.exists? trap, which of course checks your local file system, not the server y

5条回答
  •  温柔的废话
    2020-12-24 02:03

    Inspired by @bhups response, with tests:

    def remote_file_exists?(full_path)
      'true' ==  capture("if [ -e #{full_path} ]; then echo 'true'; fi").strip
    end
    
    namespace :remote do
      namespace :file do
        desc "test existence of missing file"
        task :missing do
          if remote_file_exists?('/dev/mull')
            raise "It's there!?"
          end
        end
    
        desc "test existence of present file"
        task :exists do
          unless remote_file_exists?('/dev/null')
            raise "It's missing!?"
          end
        end
      end
    end
    

提交回复
热议问题