-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver.cr
67 lines (55 loc) · 1.76 KB
/
resolver.cr
1
2
3
4
5
6
7
8
9
10
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
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
## This script will configure node resolver settings based on
## json file as database backend.
### Configuration Parameters
#### Move the static configuration file to yaml config file.
config_file = "/home/system/dnsvault/configs/settings.yml"
test_mode = false
### Dependencies ###
require "json"
require "yaml"
require "option_parser"
# Excutable argument settings.
# -t to run in verbose test mode.
# https://crystal-lang.org/api/0.18.7/OptionParser.html
OptionParser.parse! do |parser|
parser.banner = "Usage: #{PROGRAM_NAME} [arguments]"
parser.on("-t", "--test", "Run in test mode. Only output to stdout and will not write to file.") { test_mode = true }
parser.on("-h", "--help", "Show this help") { puts parser }
end
### Load external variable
unless File.file?(config_file)
puts "Error!! Config file not found at #{config_file}"
exit 1
else
config_yaml = YAML.parse(File.read("#{config_file}"))
end
### Functions ###
### Code Begin Here ###
json_path = config_yaml["routes"]["json_file"].as_s
resolv_file = config_yaml["routes"]["route_file"].as_s
# load json db.
if File.file?(json_path)
json_file = File.read(json_path)
resolv_settings = JSON.parse(json_file)
else
puts "Error #{json_path} not found!"
exit 1
end
# process each resolver hash then write to file.
# write parameter to configs file.
resolv_template = ""
resolver_list = resolv_settings["resolver"]?
#process resolver
if resolver_list
resolver_list.each do |resolv|
resolv_key = resolv.as_h.keys.first
resolv_value = resolv["#{resolv_key}"]
resolv_template = resolv_template + "#{resolv_key} #{resolv_value}\n"
end
end
if test_mode
puts resolv_template
else
#writing setting to file.
File.write(resolv_file, resolv_template)
end