Class: VPS::CLI::Domain

Inherits:
Thor
  • Object
show all
Defined in:
lib/vps/cli/domain.rb

Instance Method Summary collapse

Instance Method Details

#add(host_and_upstream, domain, email = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/vps/cli/domain.rb', line 6

def add(host_and_upstream, domain, email = nil)
  return unless domain.match(/^https?:\/\/([a-z0-9\-]{2,}\.)+[a-z]{2,}$/)

  host, name = host_and_upstream.split(":")
  config = VPS.read_config(host)

  if (upstream = config[:upstreams].detect{|upstream| upstream[:name] == name})
    upstream[:email] = email if email
    (upstream[:domains] ||= []).push(domain).uniq!
    VPS.write_config(host, config)
  end
end

#list(host_and_optional_upstream) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vps/cli/domain.rb', line 42

def list(host_and_optional_upstream)
  host, name = host_and_optional_upstream.split(":")
  config = VPS.read_config(host)

  domains = config[:upstreams].collect do |upstream|
    if name.nil? || upstream[:name] == name
      upstream[:domains]
        .collect{|domain| "  ~> #{domain}"}
        .unshift("* #{upstream[:name]}:")
    end
  end.flatten.compact

  puts domains
end

#remove(host_and_upstream, domain = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/vps/cli/domain.rb', line 20

def remove(host_and_upstream, domain = nil)
  host, name = host_and_upstream.split(":")
  config = VPS.read_config(host)

  unless name
    list = config[:upstreams].collect{|upstream| upstream[:name]}.sort
    name = list[Ask.list("From which upstream do you want to remove a domain?", list)]
  end

  if (upstream = config[:upstreams].detect{|upstream| upstream[:name] == name})
    unless domain
      list = upstream[:domains]
      domain = list[Ask.list("Which domain do you want to remove?", list)]
    end

    if upstream[:domains].reject!{|x| x == domain}
      VPS.write_config(host, config)
    end
  end
end