diff options
Diffstat (limited to 'railties/lib/rails/commands/credentials/credentials_command/diffing.rb')
-rw-r--r-- | railties/lib/rails/commands/credentials/credentials_command/diffing.rb | 43 |
1 files changed, 27 insertions, 16 deletions
diff --git a/railties/lib/rails/commands/credentials/credentials_command/diffing.rb b/railties/lib/rails/commands/credentials/credentials_command/diffing.rb index b7330178a3..1d34c68074 100644 --- a/railties/lib/rails/commands/credentials/credentials_command/diffing.rb +++ b/railties/lib/rails/commands/credentials/credentials_command/diffing.rb @@ -1,30 +1,41 @@ # frozen_string_literal: true module Rails::Command::CredentialsCommand::Diffing # :nodoc: - class Error < StandardError; end - - def enable_diffing - if enabled? - say "Already enabled!" + def enroll_project_in_credentials_diffing + if enrolled? + true else - enable - say "Diffing enabled! Editing a credentials file will display a diff of what actually changed." + gitattributes.write(<<~end_of_template, mode: "a") + config/credentials/*.yml.enc diff=rails_credentials + config/credentials.yml.enc diff=rails_credentials + end_of_template + + say "Project successfully enrolled!" + say "Rails ensures the rails_credentials diff driver is set when running `credentials:edit`. See `credentials:help` for more." end - rescue Error - say "Couldn't setup Git to enable credentials diffing." + end + + def ensure_rails_credentials_driver_is_set + set_driver if enrolled? && !driver_configured? end private - def enabled? + def enrolled? + gitattributes.read.match?(/config\/credentials(\/\*)?\.yml\.enc diff=rails_credentials/) + rescue Errno::ENOENT + false + end + + def driver_configured? system "git config --get diff.rails_credentials.textconv", out: File::NULL end - def enable - raise Error unless system("git config diff.rails_credentials.textconv 'bin/rails credentials:diff'") + def set_driver + puts "running" + system "git config diff.rails_credentials.textconv 'bin/rails credentials:diff'" + end - Rails.root.join(".gitattributes").write(<<~end_of_template, mode: "a") - config/credentials/*.yml.enc diff=rails_credentials - config/credentials.yml.enc diff=rails_credentials - end_of_template + def gitattributes + Rails.root.join(".gitattributes") end end |