class Statistrano::Remote::File

Attributes

path[R]
permissions[R]
remote[R]

Public Class Methods

new(path, remote, permissions=644) click to toggle source
# File lib/statistrano/remote/file.rb, line 8
def initialize path, remote, permissions=644
  @path   = path
  @remote = remote
  @permissions = permissions
end

Public Instance Methods

append_content!(new_content) click to toggle source
# File lib/statistrano/remote/file.rb, line 35
def append_content! new_content
  create_remote_file unless remote_file_exists?
  resp = remote.run "echo '#{new_content}' >> #{path}"

  if resp.success?
    Log.info :success, "appended content to file at #{path} on #{remote.config.hostname}"
  else
    Log.error "problem appending content to file at #{path} on #{remote.config.hostname}",
              resp.stderr
  end
end
content() click to toggle source
# File lib/statistrano/remote/file.rb, line 14
def content
  resp = remote.run "cat #{path}"
  if resp.success?
    resp.stdout
  else
    ""
  end
end
destroy!() click to toggle source
# File lib/statistrano/remote/file.rb, line 47
def destroy!
  resp = remote.run "rm #{path}"
  if resp.success?
    Log.info :success, "file at #{path} on #{remote.config.hostname} removed"
  else
    Log.error "failed to remove #{path} on #{remote.config.hostname}",
              resp.stderr
  end
end
update_content!(new_content) click to toggle source
# File lib/statistrano/remote/file.rb, line 23
def update_content! new_content
  create_remote_file unless remote_file_exists?
  resp = remote.run "echo '#{new_content}' > #{path}"

  if resp.success?
    Log.info :success, "file at #{path} on #{remote.config.hostname} saved"
  else
    Log.error "problem saving the file #{path} on #{remote.config.hostname}",
              resp.stderr
  end
end

Private Instance Methods

create_remote_file() click to toggle source
# File lib/statistrano/remote/file.rb, line 64
def create_remote_file
  resp = remote.run "touch #{path} " +
                    "&& chmod #{permissions} #{path}"

  if resp.success?
    Log.info :success, "created file at #{path} on #{remote.config.hostname}"
  else
    Log.error "problem creating file at #{path} on #{remote.config.hostname}",
              resp.stderr
  end
end
remote_file_exists?() click to toggle source
# File lib/statistrano/remote/file.rb, line 59
def remote_file_exists?
  resp = remote.run "[ -f #{path} ] && echo \"exists\""
  resp.success? && resp.stdout.strip == "exists"
end