0

How to generate content in yml file with this format:

required_groups:
- ["member", "cn=serviceboard-users,cn=groups,cn=accounts,dc=int,dc=dostack,dc=io"]

Instead of this format:

required_groups:
- - member
  - cn=serviceboard-users,cn=groups,cn=accounts,dc=int,dc=dostack,dc=io

Input:

[["member", "cn=serviceboard-users,cn=groups,cn=accounts,dc=int,dc=dostack,dc=io"]]

Current Code:

File.open("config/ldap/#{Rails.env}.yml", "w") do |file|
  data = {}

  formatted_groups_array = []
  Setting.ldap_users_filter_required_groups.each { |group| formatted_groups_array.push([Setting.ldap_group_membership_attribute.to_s, group.to_s])}
  data["required_groups"] = formatted_groups_array

  file.write(data.to_yaml)
end
marzapower
  • 5,531
  • 7
  • 38
  • 76
Miroslav
  • 158
  • 2
  • 22

1 Answers1

0

As stated in this answer, the default ruby library Psych does not have many options available, and you cannot customize the output the way you are trying to do.

In the meantime I've found an old possibile custom solution to your problem, but I had to rewrite a small part of it to make it compatible to the most recent ruby version (I am using 2.7.0 for this example). This is my updated script. Basically it will introduce a new way to edit the YAML output using a more concise syntax.

You can use it this way:

 File.open("config/ldap/#{Rails.env}.yml", "w") do |file|
  data = {}

  formatted_groups_array = []
  Setting.ldap_users_filter_required_groups.each { |group| formatted_groups_array.push([Setting.ldap_group_membership_attribute.to_s, group.to_s])}
  data["required_groups"] = formatted_groups_array
  
  styled_yaml = StyledYAML.inline(data)
  file.write(StyledYAML.dump(styled_yaml))
end
marzapower
  • 5,531
  • 7
  • 38
  • 76