Module: Radd::Cli

Defined in:
lib/radd/cli.rb

Constant Summary collapse

COMMANDS =
%w[help setup add delete list start].freeze

Class Method Summary collapse

Class Method Details

.addObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/radd/cli.rb', line 52

def add
  load_config
  print "Enter name: "
  name = STDIN.gets.chomp
  print "Password: "
  password = STDIN.gets.chomp
  print "Re-type password: "
  password_confirmation = STDIN.gets.chomp
  raise "Password mismatch!" unless password == password_confirmation
  record = Radd::Record.new(password: password)
  record.name = name
  record.save
  puts "Added record '#{name}'\n"
end

.deleteObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/radd/cli.rb', line 67

def delete
  load_config
  print "Enter name: "
  name = STDIN.gets.chomp
  if record = Radd::Record.where(name: name).first
    print "Do you really want to delete #{name} (y/N)?"
    confirm = STDIN.gets.chomp
    if 'y' == confirm
      record.delete
      puts "Record #{name} deleted"
    end
  else
    puts "Record #{name} not found"
  end
end

.helpObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/radd/cli.rb', line 31

def help
  puts <<~EOS
    Usage:
      radd COMMAND [--config FILE]

    You must specify one of the following commands:

      setup        Create the database
      add          Add new record
      list         List available records
      start        Run the server
  
  EOS
end

.listObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/radd/cli.rb', line 83

def list
  load_config
  puts
  records = Radd::Record.all
  tab = [records.map(&:name).map(&:size).max, 24].compact.max
  records.each do |record|
    puts "#{record.name.ljust(tab)}  #{record.ip.to_s.ljust(15)}  #{record.updated_at || 'never updated'}\n"
  end
  puts
end

.load_config(**opts) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/radd/cli.rb', line 9

def load_config(**opts)
  config = {}
  parser = OptionParser.new
  parser.banner = 'Usage: radd COMMAND [--config FILE]'
  parser.on('--config FILE', 'Config file') do |file|
    config['file'] = file
  end

  parser.parse!

  Radd.configure!(config['file'], **opts)
end

.runObject



22
23
24
25
26
27
28
29
# File 'lib/radd/cli.rb', line 22

def run
  command = ARGV.shift
  unless COMMANDS.include?(command)
    puts "Unknown command #{command}"
    exit(1)
  end
  send(command)
end

.setupObject



46
47
48
49
50
# File 'lib/radd/cli.rb', line 46

def setup
  require_relative 'schema'
  load_config(skip_db: true, skip_models: true)
  Radd::Schema.create
end

.startObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/radd/cli.rb', line 94

def start
  load_config
  puts "Starting Radd server for #{Radd.domain}"

  dns, http = Radd::Nameserver.new, Radd::Webserver.new

  Async do
    dns_task, http_task = dns.run, http.run
    watchdog = Async do
      sleep(1) while !http_task.failed? && !dns_task.failed?
      puts "Task failed!"
      exit(1)
    end
  end
end