Class: Domrobot::Cli

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

Instance Method Summary collapse

Instance Method Details

#configure(file = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/domrobot/cli.rb', line 11

def configure file=nil
  config   = file and Config.load file 
  config ||= Config
  if options["uri"]
    uri       ||= URI URI.escape options["uri"]
    @username ||= URI.unescape uri.user     if uri.user
    @password ||= URI.unescape uri.password if uri.password
    @server   ||= URI.unescape uri.host     if uri.host
  else
    @username ||= options["username"]
    @password ||= options["password"]
    @server   ||= options["server"]
  end
  config.general.username = @username if @username
  config.general.password = @password if @password
  config.general.server   = @server   if @server
  if file
    config.save(file)
  else
    config.save
  end
end

#updateRecord(name) ⇒ Object

Raises:

  • (Exceptions)


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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/domrobot/cli.rb', line 42

def updateRecord name
  success_callbacks = []
  if options["touch"]
    require "fileutils"
    success_callbacks << Proc.new do |message,data|
      FileUtils.mkdir_p File.dirname options["touch"]
      File.write options["touch"], [message,data].join("\n")
    end
  end
  if options["default_address"]
    require "domrobot/address"
    address = Domrobot::Address.default
  else
    address = options["address"]
  end
  raise Exceptions unless address
  subdomain = Subdomain.construct options["subdomain"]
  domain    = options["domain"]
  name      = [name,subdomain,domain].join(".")
  type      = options["type"]
  record    = Record.find_by domain:domain, name:name
  
  unless record
    puts "NEW"
    record = Record.new domain:domain,name:name,type:type,content:address
    result = record.save
    if result["code"] = 1000
      m,d = "OK NOOP","#{address} A #{name}."
      puts m
      success_callbacks.map{|cb| cb.call(m,d)} and exit 0
    end
    exit 1
  end
  if record.content == address
    m,d = "OK NOOP","#{address} A #{name}."
    puts m
    success_callbacks.map{|cb| cb.call(m,d) } and exit 0
  end
  record.content = address
  result = record.update
  if result["code"] = 1000
    m,d = "OK CREATE","#{address} A #{name}."
    puts m
    success_callbacks.map{|cb| cb.call(m,d)} and exit 0
  end
  exit 1
end