Class: Statistrano::Remote::File

Inherits:
Object
  • Object
show all
Defined in:
lib/statistrano/remote/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, remote, permissions = 644) ⇒ File

Returns a new instance of File.



8
9
10
11
12
# File 'lib/statistrano/remote/file.rb', line 8

def initialize path, remote, permissions=644
  @path   = path
  @remote = remote
  @permissions = permissions
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/statistrano/remote/file.rb', line 6

def path
  @path
end

#permissionsObject (readonly)

Returns the value of attribute permissions.



6
7
8
# File 'lib/statistrano/remote/file.rb', line 6

def permissions
  @permissions
end

#remoteObject (readonly)

Returns the value of attribute remote.



6
7
8
# File 'lib/statistrano/remote/file.rb', line 6

def remote
  @remote
end

Instance Method Details

#append_content!(new_content) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# 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

#contentObject



14
15
16
17
18
19
20
21
# File 'lib/statistrano/remote/file.rb', line 14

def content
  resp = remote.run "cat #{path}"
  if resp.success?
    resp.stdout
  else
    ""
  end
end

#destroy!Object



47
48
49
50
51
52
53
54
55
# 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) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# 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