forked from picotrading/config-encoder-macros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaml_converter.rb
executable file
·121 lines (99 loc) · 2.81 KB
/
yaml_converter.rb
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env ruby
require 'erb'
require 'optparse'
require 'ostruct'
require 'yaml'
def parse_arguments
args = OpenStruct.new
# Default values
args.format = nil
args.help = false
args.path = '.'
args.var = nil
args.yaml_file = nil
options = OptionParser.new do |opts|
opts.program_name = $PROGRAM_NAME
opts.banner = "usage: #{opts.program_name} " \
'[-h] -f FORMAT [-p PATH] [-v NAME] [-y FILE]'
opts.separator('')
opts.separator('YAML converter')
opts.separator('')
opts.separator('optional arguments:')
opts.on(
'-f', '--format FORMAT',
'output format') do |f|
args.format = f
end
opts.on(
'-h', '--help',
'show this help message and exit') do |h|
args.help = h
end
opts.on(
'-p', '--path PATH',
'path to the macros directory (default: .)') do |p|
args.path = p
end
opts.on(
'-v', '--var NAME',
'read data from certain YAML variable') do |v|
args.var = v
end
opts.on(
'-y', '--yaml FILE',
'YAML file to convert (default: -)') do |y|
args.yaml_file = y
end
opts.separator('')
opts.separator('Examples:')
opts.separator(" $ #{opts.program_name} -f apache -v apache_data -y " \
'./vars/apache_test.yaml')
opts.separator(" $ #{opts.program_name} -f json -v json_data -y " \
'./vars/json_test.yaml')
end
options.parse!
return args, options
end
def main
# Parse command line arguments
args, options = parse_arguments
# Check if help should be displayed
if args.help
print options.help
exit
end
# Check if format was specified
if args.format.nil?
print options.help
abort("\nERROR: Format not specified.")
end
# Check if specified format is supported
formats = %w(apache erlang ini json toml xml yaml)
unless formats.include?(args.format)
abort('ERROR: Unsuported format. Suported formats are: ' +
formats.join(', '))
end
if args.yaml_file.nil?
# Read data from pipeline
yaml_input = ARGF.read
yaml_load_fnc = YAML.method(:load)
else
# Check if the YAML file exists
abort('ERROR: YAML file not found.') unless File.exist?(args.yaml_file)
yaml_input = args.yaml_file
yaml_load_fnc = YAML.method(:load_file)
end
# Convert the YAML data to the Ruby data structure
if args.var.nil?
item = yaml_load_fnc.call(yaml_input)
else
item = yaml_load_fnc.call(yaml_input)[args.var]
end
# Check if the macro file exists
macro_path = "#{args.path}/macros/#{args.format}_encode_macro.erb"
abort('ERROR: Macro file not found.') unless File.exist?(macro_path)
# Convert the YAML data to the final format
binding = OpenStruct.new.send(:binding)
print ERB.new(IO.read(macro_path), nil, '-', '_erbout0').result(binding)
end
main if __FILE__ == $PROGRAM_NAME