securial 2.1.1 → 2.1.5

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: f9c7a709db9d2a74edcd0d7d4046d280049a2d83fe63fbb8aa5d15b209ac39aa
4
- data.tar.gz: 520a658e2797e57cdcb7ef1d365324b84ba2c57e2e1197974c7d76f516d7977e
3
+ metadata.gz: 380f3eb7c41909962f49e0d860ac4ebfb8948b4477557ca0d4fc3a3b599b78c6
4
+ data.tar.gz: fe9c3e04c95f70836d453e36f4bbaf67b428a397c05a7516ca42d152a8368f9d
5
5
  SHA512:
6
- metadata.gz: bf133510e6d9923775356f1ce0d8674289dafc8cc52e0f87faf10a156b9c24285d62d07fa5db4c32363f82bd40c714266c70c8694016d6cbf46bedcc58aab1af
7
- data.tar.gz: f150c5a04805ea5a8d92b0833dd1e926265d8fb574534e3f146b7865c9c49b38514b8edf8069be7ee5e3f92b752eff52e05191d123abb30ac69f5db4cd359c7b
6
+ metadata.gz: 2a144e1b69790b12456fb2db21218f3ae77cf72c148a92af38d7934ab21740af566b4bd2155738d90d490da4004f12531a447296c674565b54774f667c33719b
7
+ data.tar.gz: 50404735621bed58aa44b3a98a5387a85093ea9d0a68050daa39e21fcad42fab30efbcf953b63c118b5d77dec4c640c47bc0e84a1c1382225030670f9aa18612
data/lib/securial/cli.rb CHANGED
@@ -197,22 +197,55 @@ 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 if config["default"]["adapter"].blank?
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 file line-by-line to preserve anchors and formatting
212
+ lines = File.readlines(db_config_path)
213
+ inside_default = false
214
+ updated_lines = []
215
+
216
+ lines.each_with_index do |line, index|
217
+ if line =~ /^default:/
218
+ inside_default = true
219
+ updated_lines << line
220
+ next
221
+ end
222
+
223
+ if inside_default
224
+ # Break out of default block when another top-level key appears
225
+ if line =~ /^\S/ && line !~ /^\s/
226
+ inside_default = false
227
+ end
228
+
229
+ # Skip existing host/username/password lines
230
+ next if line =~ /^\s*(host|username|password):/
231
+ end
232
+
233
+ updated_lines << line
234
+ end
235
+
236
+ # Find index of `default:` line to insert after
237
+ insert_index = updated_lines.find_index { |l| l =~ /^default:/ }
238
+ return unless insert_index
239
+
240
+ injection = [
241
+ " host: <%= ENV.fetch(\"DB_HOST\", \"localhost\") %>",
242
+ " username: <%= ENV.fetch(\"DB_USERNAME\") { \"postgres\" } %>",
243
+ " password: <%= ENV.fetch(\"DB_PASSWORD\") { \"postgres\" } %>",
244
+ ]
245
+
246
+ updated_lines.insert(insert_index + 1, *injection)
212
247
 
213
- # Dump YAML manually to preserve formatting
214
- yaml = config.to_yaml.gsub(/['"](<%= .*? %>)['"]/, '\1') # Unquote the ERB
215
- File.write(db_config_path, yaml)
248
+ File.write(db_config_path, updated_lines.join)
216
249
  end
217
250
 
218
251
  # 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.1".freeze
9
+ VERSION = "2.1.5".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.1
4
+ version: 2.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aly Badawy