Class: PostDB::CLI::Aliases

Inherits:
Thor
  • Object
show all
Defined in:
lib/postdb/cli/aliases.rb

Constant Summary collapse

SOURCE_REGEX =
/^([A-Za-z0-9\-\_\.\+]+)?@[A-Za-z0-9\-]+\.[A-Za-z0-9\-\.]+$/

Instance Method Summary collapse

Instance Method Details

#add(source = nil, destination = nil) ⇒ Object



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
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/postdb/cli/aliases.rb', line 45

def add(source = nil, destination = nil)
  unless source
    source = prompt.ask("Source:") do |q|
      q.required(true)
      q.validate(SOURCE_REGEX)
    end
  end

  unless destination
    destination = prompt.ask("Destination:") do |q|
      q.required(true)
      q.validate(:email)
    end
  end

  source = source.downcase
  destination = destination.downcase

  if source == destination
    exit_with_error("The alias '#{source} -> #{destination}' is managed automatically by PostDB.")
  end

  if PostDB::Alias.where(source: source, destination: destination).count > 0
    exit_with_warning("The alias '#{source} -> #{destination}' has already been added.")
  end

  domain_name = source.split('@')[1]

  unless domain = PostDB::Domain.where(name: domain_name).first
    exit_with_error("The domain '#{domain_name}' is not available.")
  end

  PostDB::Alias.create(domain: domain, source: source, destination: destination)

  prompt.ok("The alias '#{source} -> #{destination}' has been added.")
end

#list(domain = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/postdb/cli/aliases.rb', line 11

def list(domain = nil)
  domains = PostDB::Domain.where(**(domain ? { name: domain } : {}))

  if domains.empty?
    if domain
      exit_with_warning("The domain '#{domain}' could not be found.")
    else
      exit_with_warning("There don't appear to be any domains on this system.")
    end
  end

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

  domains.each_with_index do |domain, index|
    aliases = domain.forwarding_aliases.sort { |a, b| a.source <=> b.source }

    if aliases.empty?
      puts TTY::Table.new(
        header: [domain.name].pad(' '),
        rows: [['No Aliases'].pad(' ')]
      ).render(:ascii)
    else
      puts TTY::Table.new(
        header: [" " + domain.name + " \n Source: ", "\n Destination: "],
        rows: aliases.map { |a| [a.source, a.destination].pad(' ') }
      ).render(:ascii, multiline: true)
    end

    new_line unless (index + 1) == domains.count
  end
end

#remove(source = nil, destination = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/postdb/cli/aliases.rb', line 84

def remove(source = nil, destination = nil)
  unless source && destination
    aliases = PostDB::Alias.all.select { |a| a.source != a.destination }

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

    a = prompt.select("Alias:", aliases.map { |a| ["#{a.source} -> #{a.destination}", a] }.to_h)

    source = a.source
    destination = a.destination
  end

  source = source.downcase
  destination = destination.downcase

  if source == destination
    exit_with_error("The alias '#{source} -> #{destination}' is managed automatically by PostDB.")
  end

  aliases = PostDB::Alias.where(source: source, destination: destination)

  if aliases.empty?
    exit_with_warning("The alias '#{source} -> #{destination}' could not be found.")
  end

  unless options[:force]
    confirm_action!("Remove the alias '#{source} -> #{destination}'?", "'#{source} -> #{destination}' left untouched.")
  end

  aliases.each(&:destroy)

  prompt.ok("The alias '#{source} -> #{destination}' has been removed.")
end