Class: OpsWorks::Commands::FileUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/opsworks/commands/file_util.rb

Class Method Summary collapse

Class Method Details

.backup(source) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/opsworks/commands/file_util.rb', line 9

def self.backup (source)
  if File.exists? source
    base_name = source + ".backup"
    if File.exists? base_name
      number = 0
      backup = "#{base_name}-#{number}"
      while base_name.class.exists? backup
        backup = "#{base_name}-#{number += 1}"
      end
    else
      backup = base_name
    end

    FileUtils.cp_r(source, backup)
  end
end

.update_file(f, new_contents) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/opsworks/commands/file_util.rb', line 26

def self.update_file (f, new_contents)
  options = Trollop::options

  old_contents = (File.exists? f) ? File.read(f) : ""

  File.open(f, "w") do |file|
    unless old_contents == ""
      file.puts old_contents.gsub(
        /\n?\n?#{SSH_PREFIX}.*#{SSH_POSTFIX}\n?\n?/m,
        ''
      )
    end
    file.puts new_contents
  end
end