-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.rb
More file actions
executable file
·59 lines (50 loc) · 1.45 KB
/
main.rb
File metadata and controls
executable file
·59 lines (50 loc) · 1.45 KB
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
#!/usr/bin/env ruby
require "slop"
require "dry/monads"
require "yaml"
require_relative "util/file_helpers"
require_relative "util/open_api_helpers"
include Dry::Monads[:result]
# Simple CLI Parsing
opts = Slop.parse { |o|
o.string "-f", "--file", "routes file to read in"
o.on "--version", "print the version" do
puts Slop::VERSION
exit
end
}
# ! This hack sucks - what is better? hash.transform_keys(&:to_s) didn't help me on Ruby 2.6.6.
class ::Hash
# via https://stackoverflow.com/a/25835016/2257038
def stringify_keys
h = map { |k, v|
v_str = if v.instance_of? Hash
v.stringify_keys
else
v
end
[k.to_s, v_str]
}
Hash[h]
end
end
# Define handlers for Either Monad
handle_failure = lambda do |failure|
# just write the failure to stdout for now
puts failure
exit 1
end
handle_success = lambda do |success|
# ! I really detest this hack. There needs to be a better way.
res = success.to_hash.stringify_keys
# Write OpenAPI output to YAML file
# puts success
File.open("temp.yml", "w") { |file| file.write(res.to_yaml) }
exit 0
end
# Main
# ! This looks synchronous. How do you write file streams in Ruby instead of creating large memory buffers?
file_data = Util::FileHelpers.call(opts.to_hash[:file]).value_or(handle_failure)
yaml_format = Util::OpenAPIHelpers.call(file_data)
# Fold either handlers based on success or failure
yaml_format.either(handle_success, handle_failure)