Class: PostDB::CLI::Domains

Inherits:
Thor
  • Object
show all
Defined in:
lib/postdb/cli/domains.rb,
lib/postdb/cli/domains/dkim.rb

Defined Under Namespace

Classes: DKIM

Instance Method Summary collapse

Instance Method Details

#add(domain_name = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/postdb/cli/domains.rb', line 26

def add(domain_name = nil)
  unless domain_name
    domain_name = prompt.ask("Domain:") do |q|
      q.required(true)
    end
  end

  domain_name = domain_name.downcase

  if PostDB::Domain.where(name: domain_name).count > 0
    exit_with_warning("The domain '#{domain_name}' has already been added.")
  end

  domain = PostDB::Domain.new
  domain.name = domain_name

  if domain.save
    prompt.ok("The domain '#{domain_name}' has been added.")
  else
    errors = domain.errors.full_messages.map { |m| " #{m}" }
    exit_with_error("The domain '#{domain_name}' couldn't be added:", *errors)
  end
end

#listObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/postdb/cli/domains.rb', line 9

def list
  domains = PostDB::Domain.all

  if domains.empty?
    exit_with_warning("There don't appear to be any domains on this system.")
  end

  domains = domains.to_a
  domains.sort! { |a, b| a.name <=> b.name }

  puts TTY::Table.new(
    header: ["Domain Name", "Total Users", "Total Aliases"].pad(' '),
    rows: domains.map { |d| [d.name, d.users.count, d.forwarding_aliases.count].pad(' ') }
  ).render(:ascii, multiline: true)
end

#remove(domain_name = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/postdb/cli/domains.rb', line 52

def remove(domain_name = nil)
  unless domain_name
    domains = PostDB::Domain.all

    if domains.empty?
      exit_with_warning("There don't appear to be any domains on this system.")
    end

    domains = domains.to_a
    domains.sort! { |a, b| a.name <=> b.name }

    domain_name = prompt.select("Domain:", domains.map(&:name))
  end

  domain_name = domain_name.downcase

  domains = PostDB::Domain.where(name: domain_name)

  if domains.empty?
    exit_with_warning("The domain '#{domain_name}' could not be found.")
  end

  unless options[:force]
    confirm_action!("Remove the domain '#{domain_name}'?", "'#{domain_name}' left untouched.")
  end

  domains.each(&:destroy)

  prompt.ok("The domain '#{domain_name}' has been removed.")
end