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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/radd/cli.rb', line 59

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/radd/cli.rb', line 74

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/radd/cli.rb', line 37

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
      delete       Delete record
      list         List available records
      start        Run the server
  
  EOS
end

.listObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/radd/cli.rb', line 90

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
30
31
32
33
34
35
# File 'lib/radd/cli.rb', line 22

def run
  command = ARGV.shift
  if COMMANDS.include?(command)
    send(command)
  elsif command.nil?
    help
    exit 1
  elsif '--help' == command
    help
  else
    puts "Unknown command #{command}"
    exit 1
  end
end

.setupObject



53
54
55
56
57
# File 'lib/radd/cli.rb', line 53

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

.startObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/radd/cli.rb', line 101

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