Module: PgSync::Utils

Included in:
Client, Init, Sync, TableList, TableSync
Defined in:
lib/pgsync/utils.rb

Instance Method Summary collapse

Instance Method Details

#colorize(message, color_code) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/pgsync/utils.rb', line 7

def colorize(message, color_code)
  if $stderr.tty?
    "\e[#{color_code}m#{message}\e[0m"
  else
    message
  end
end

#config_fileObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pgsync/utils.rb', line 15

def config_file
  return @config_file if instance_variable_defined?(:@config_file)

  @config_file =
    search_tree(
      if @options[:db]
        db_config_file(@options[:db])
      else
        @options[:config] || ".pgsync.yml"
      end
    )
end

#db_config_file(db) ⇒ Object



28
29
30
31
# File 'lib/pgsync/utils.rb', line 28

def db_config_file(db)
  return unless db
  ".pgsync-#{db}.yml"
end

#log(message = nil) ⇒ Object



3
4
5
# File 'lib/pgsync/utils.rb', line 3

def log(message = nil)
  $stderr.puts message
end

#search_tree(file) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pgsync/utils.rb', line 33

def search_tree(file)
  path = Dir.pwd
  # prevent infinite loop
  20.times do
    absolute_file = File.join(path, file)
    if File.exist?(absolute_file)
      break absolute_file
    end
    path = File.dirname(path)
    break if path == "/"
  end
end