Class: Posgra::CLI::Database

Inherits:
Thor
  • Object
show all
Includes:
Helper, Logger::Helper
Defined in:
lib/posgra/cli/database.rb

Constant Summary collapse

DEFAULT_FILENAME =
'pg_dbgrants.rb'

Constants included from Helper

Helper::REGEXP_OPTIONS

Instance Method Summary collapse

Methods included from Logger::Helper

#log

Methods included from Helper

#check_fileanem, #client

Instance Method Details

#apply(file) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/posgra/cli/database.rb', line 14

def apply(file)
  check_fileanem(file)
  updated = client.apply_databases(file)

  unless updated
    Posgra::Logger.instance.info('No change'.intense_blue)
  end
end

#export(file = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/posgra/cli/database.rb', line 25

def export(file = nil)
  check_fileanem(file)
  dsl = client.export_databases

  if options[:split]
    file = DEFAULT_FILENAME unless file

    log(:info, 'Export Database Grants')
    requires = []

    dsl.each do |user, content|
      user = user.gsub(/\s+/, '_')
      user = '_' if user.empty?
      grant_file = "#{user}.rb"
      requires << grant_file
      log(:info, "  write `#{grant_file}`")

      open(grant_file, 'wb') do |f|
        f.puts Posgra::CLI::MAGIC_COMMENT
        f.puts content
      end
    end

    log(:info, "  write `#{file}`")

    open(file, 'wb') do |f|
      f.puts Posgra::CLI::MAGIC_COMMENT

      requires.each do |grant_file|
        f.puts "require '#{File.basename grant_file}'"
      end
    end
  else
    if file.nil? or file == '-'
      puts dsl
    else
      log(:info, "Export Database Grants to `#{file}`")

      open(file, 'wb') do |f|
        f.puts Posgra::CLI::MAGIC_COMMENT
        f.puts dsl
      end
    end
  end
end