-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml_to_s3_json_config_converter.rb
35 lines (29 loc) · 1.14 KB
/
yaml_to_s3_json_config_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
require 'yaml'
require 'json'
require 'uri'
def convert_yaml_to_s3_json_config(yaml_file, json_file)
yaml_data = YAML.load_file(yaml_file)
website_configuration = {}
if yaml_data['s3']
website_configuration['IndexDocument'] = { 'Suffix' => yaml_data['s3']['index'] } if yaml_data['s3']['index']
website_configuration['ErrorDocument'] = { 'Key' => yaml_data['s3']['error'] } if yaml_data['s3']['error']
if yaml_data['s3']['redirects']
redirect_rules = yaml_data['s3']['redirects'].map do |rule|
{
'Condition' => {
'KeyPrefixEquals' => rule['path'].delete_prefix('/')
},
'Redirect' => {
'HostName' => URI.parse(rule['destination']).host,
'HttpRedirectCode' => '301',
'Protocol' => URI.parse(rule['destination']).scheme,
'ReplaceKeyPrefixWith' => URI.parse(rule['destination']).path.delete_prefix('/')
}
}
end
website_configuration['RoutingRules'] = redirect_rules
end
end
File.write(json_file, JSON.pretty_generate(website_configuration))
end
convert_yaml_to_s3_json_config('_config.yml', 's3_config.json')