forked from openSUSE/open-build-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolve_swagger_yaml.rb
executable file
·67 lines (55 loc) · 1.83 KB
/
resolve_swagger_yaml.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
#!/usr/bin/ruby.ruby3.1
# frozen_string_literal: true
require 'optparse'
require 'logger'
require 'yaml'
require 'json'
require 'json_refs'
class ResolveSwaggerYAML
def initialize
@log = Logger.new($stderr)
parse_arguments
check_for_file_existence
resolved_yaml = resolve_swagger_yaml
output_yaml_file(yaml_content: resolved_yaml)
end
private
# rubocop:disable Metrics/MethodLength
def parse_arguments
opt_parser = OptionParser.new do |parser|
parser.banner = 'Usage: resolve_swagger_yaml.rb [options]'
parser.on('-i', '--input PATH/TO/SWAGGER.YAML', 'specify input swagger yaml file location') { |i| @input_file = i }
parser.on('-o', '--output PATH/TO/SWAGGER.YAML', 'specify output location of resolved swagger yaml file') { |o| @output_file = o }
parser.on('-f', '--force', 'allow overwrite of an existing file') { |f| @force = f }
parser.on('-h', '--help', 'Print this help') do
puts parser
exit
end
end
opt_parser.parse!(ARGV)
end
# rubocop:enable Metrics/MethodLength
def check_for_file_existence
return if File.file?(@input_file)
@log.error("The specified input file does not exist: #{@input_file}")
exit 1
end
def resolve_swagger_yaml
yaml = YAML.load_file(@input_file)
resolved_in_json_format = nil
Dir.chdir(File.dirname(@input_file)) do
# JsonRefs can handle the yaml format too, but returns a json formated version
resolved_in_json_format = JsonRefs.call(yaml)
end
# Convert output back to yaml
resolved_in_json_format.to_yaml
end
def output_yaml_file(yaml_content:)
if File.file?(@output_file) && !@force
@log.error("The file already exists, use '-f' to force overwrite: #{@output_file}")
exit 1
end
File.write(@output_file, yaml_content)
end
end
ResolveSwaggerYAML.new