securial 2.1.3 → 2.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6685ddf1efdce3df66dac73131e8cb8cb38c1c02841d469eeea090a32f5bf12d
4
- data.tar.gz: 5e225dd6071383dda5b76636edf65549debabae162db4d434e67cb259fe4f8f5
3
+ metadata.gz: 914b5c4ac539dfc8c6994f001eeec7237a0fbda2030c828898864b9371ac2ac4
4
+ data.tar.gz: 3b23660aa0a8e439d0bfa46f8bfab87949634dd84a45eaedb91088058a1576d3
5
5
  SHA512:
6
- metadata.gz: 5fb0b09611232bd05c972b5e122f7afd382d2666c87fde018ef4b10878c02db60b0d4ecbc6c0fade302948b0a9969b0dd675c56e7d176f0a535c5bdafbff311c
7
- data.tar.gz: f332fd17e74bdec705ac54045a73b96ec4f4c738d37407dccb2858d2cd5cfe447c4ec34fb9910373c7c55d22c5d9cd2e5421543d3226d6f7a22257201dd63771
6
+ metadata.gz: 5f50a95757c49396f0c45ce9970e9f4333603e8499f8ae532def4f3d96252ec8dc91be4533f8f2cb190e2d905fd6cfcb4bccec4aad978ead7ed99bcedc40e183
7
+ data.tar.gz: a7e219a841c30569a68dec40b3ff2abbca4f446a60c6e0de7214a470bb188eb1bdcded2bea1c86a79ba5b31752f2b746bebde25c8b58ddce1dd4e6031d89fffb
data/lib/securial/cli.rb CHANGED
@@ -197,22 +197,45 @@ module Securial
197
197
  # @param app_name [String] name of the Rails application
198
198
  # @return [void]
199
199
  #
200
- def update_database_yml_host(app_name)
200
+ def update_database_yml_host(app_name) # rubocop:disable Metrics/MethodLength
201
201
  db_config_path = File.join(app_name, "config", "database.yml")
202
+
203
+ # Step 1: Parse to check adapter
202
204
  raw = File.read(db_config_path)
203
205
  rendered = ERB.new(raw).result
204
206
  config = YAML.safe_load(rendered, aliases: true) || {}
205
207
 
206
- config["default"] ||= {}
207
- return unless config["default"]["adapter"].is_a?(String) && !config["default"]["adapter"].empty?
208
- return unless ["mysql2", "postgresql"].include?(config["default"]["adapter"])
209
- config["default"]["host"] = "<%= ENV.fetch(\"DB_HOST\", \"localhost\") %>"
210
- config["default"]["username"] = "<%= ENV.fetch(\"DB_USERNAME\") { \"postgres\" } %>"
211
- config["default"]["password"] = "<%= ENV.fetch(\"DB_PASSWORD\") { \"postgres\" } %>"
208
+ adapter = config.dig("default", "adapter")
209
+ return unless adapter.is_a?(String) && %w[postgresql mysql2].include?(adapter)
210
+
211
+ # Step 2: Modify the raw YAML file line-by-line
212
+ lines = File.readlines(db_config_path)
213
+ updated_lines = []
214
+ inside_default = false
215
+ inserted = false
216
+
217
+ lines.each do |line|
218
+ updated_lines << line
219
+
220
+ if line =~ /^default:/
221
+ inside_default = true
222
+ next
223
+ end
224
+
225
+ if inside_default && line.strip.start_with?("adapter:")
226
+ # Insert immediately after the adapter line
227
+ updated_lines += [
228
+ " host: <%= ENV.fetch(\"DB_HOST\", \"localhost\") %>\n",
229
+ " username: <%= ENV.fetch(\"DB_USERNAME\") { \"postgres\" } %>\n",
230
+ " password: <%= ENV.fetch(\"DB_PASSWORD\") { \"postgres\" } %>\n",
231
+ ]
232
+ inserted = true
233
+ end
212
234
 
213
- # Dump YAML manually to preserve formatting
214
- yaml = config.to_yaml.gsub(/['"](<%= .*? %>)['"]/, '\1') # Unquote the ERB
215
- File.write(db_config_path, yaml)
235
+ # Exit `default:` block when another top-level key appears
236
+ inside_default = false if inside_default && line =~ /^\S/ && line !~ /^\s/
237
+ end
238
+ File.write(db_config_path, updated_lines.join)
216
239
  end
217
240
 
218
241
  # Adds the Securial gem to the application's Gemfile.
@@ -6,5 +6,5 @@ module Securial
6
6
  #
7
7
  # @see https://semver.org/ Semantic Versioning 2.0.0
8
8
  # @return [String] the current version in the format "major.minor.patch"
9
- VERSION = "2.1.3".freeze
9
+ VERSION = "2.1.6".freeze
10
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: securial
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.3
4
+ version: 2.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aly Badawy