diff options
author | Kasper Timm Hansen <kaspth@gmail.com> | 2019-08-04 02:19:55 +0200 |
---|---|---|
committer | Kasper Timm Hansen <kaspth@gmail.com> | 2019-08-04 02:19:55 +0200 |
commit | f1f5024b918ecf303b9908f78d1a6136d9418730 (patch) | |
tree | 2e35bb48205daddbf6c5f02fdfbf8a39ffa23867 | |
parent | 03e44f93001db97953917e0a100c627e189e2be6 (diff) | |
download | rails-f1f5024b918ecf303b9908f78d1a6136d9418730.tar.gz rails-f1f5024b918ecf303b9908f78d1a6136d9418730.tar.bz2 rails-f1f5024b918ecf303b9908f78d1a6136d9418730.zip |
Revise flow to what was described in 03e44f9
4 files changed, 47 insertions, 38 deletions
diff --git a/railties/lib/rails/commands/credentials/USAGE b/railties/lib/rails/commands/credentials/USAGE index 0396fcb403..6b896ab02a 100644 --- a/railties/lib/rails/commands/credentials/USAGE +++ b/railties/lib/rails/commands/credentials/USAGE @@ -35,14 +35,15 @@ You could prepend that to your server's start command like this: Rails provides `rails credentials:diff --enable` to instruct Git to call `rails credentials:diff` when `git diff` is run on a credentials file. -Any credentials files are set to use the "rails_credentials" diff driver in .gitattributes. -Since Git requires the diff driver to be set up in a config file, the command uses -the project local .git/config. Since that config isn't stored in Git each team member -must enable separately. +Running the command enrolls the project such that all credentials files use the +"rails_credentials" diff driver in .gitattributes. -Or set up the "rails_credentials" diff driver globally with: +Additionally since Git requires the driver itself to be set up in a config file +that isn't tracked Rails automatically ensures it's configured when running +`credentials:edit`. - git config --global diff.rails_credentials.textconv "bin/rails credentials:diff" +Otherwise each co-worker would have to run enable manually, including on each new +repo clone. === Editing Credentials diff --git a/railties/lib/rails/commands/credentials/credentials_command.rb b/railties/lib/rails/commands/credentials/credentials_command.rb index 9b3a7dbb86..9cde44558b 100644 --- a/railties/lib/rails/commands/credentials/credentials_command.rb +++ b/railties/lib/rails/commands/credentials/credentials_command.rb @@ -32,6 +32,7 @@ module Rails ensure_encryption_key_has_been_added if credentials.key.nil? ensure_credentials_have_been_added + ensure_rails_credentials_driver_is_set catch_editing_exceptions do change_credentials_in_system_editor @@ -49,8 +50,8 @@ module Rails say credentials.read.presence || missing_credentials_message end - option :enable, type: :boolean, default: false, - desc: "Pass `--enable` to make credential files diffable with `git diff`" + option :enroll, type: :boolean, default: false, + desc: "Enrolls project in credential file diffing with `git diff`" def diff(content_path = nil) if @content_path = content_path @@ -60,7 +61,7 @@ module Rails say credentials.read.presence || credentials.content_path.read else require_application! - enable_diffing if options[:enable] + enroll_project_in_credentials_diffing if options[:enroll] end rescue ActiveSupport::MessageEncryptor::InvalidMessage say credentials.content_path.read 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 diff --git a/railties/test/commands/credentials_test.rb b/railties/test/commands/credentials_test.rb index e5397523e1..974b34836b 100644 --- a/railties/test/commands/credentials_test.rb +++ b/railties/test/commands/credentials_test.rb @@ -123,28 +123,24 @@ class Rails::Command::CredentialsCommandTest < ActiveSupport::TestCase end - test "diff enable diffing" do - run_diff_command(enable: true) + test "diff enroll diffing" do + assert_match("successfully enrolled", run_diff_command(enroll: true)) assert_equal <<~EOM, File.read(app_path(".gitattributes")) config/credentials/*.yml.enc diff=rails_credentials config/credentials.yml.enc diff=rails_credentials EOM + end + + test "running edit after enrolling in diffing sets diff driver" do + run_diff_command(enroll: true) + run_edit_command Dir.chdir(app_path) do assert_equal "bin/rails credentials:diff", `git config --get 'diff.rails_credentials.textconv'`.strip end end - test "diff won't enable again if already enabled" do - app_file(".git/config", <<~EOM) - [diff "rails_credentials"] - textconv = bin/rails credentials:diff - EOM - - assert_no_match(/Diffing enabled/, run_diff_command(enable: true)) - end - test "diff from git diff left file" do run_edit_command(environment: "development") @@ -196,8 +192,8 @@ class Rails::Command::CredentialsCommandTest < ActiveSupport::TestCase rails "credentials:show", args, **options end - def run_diff_command(path = nil, enable: nil, **options) - args = enable ? ["--enable"] : [path] + def run_diff_command(path = nil, enroll: nil, **options) + args = enroll ? ["--enroll"] : [path] rails "credentials:diff", args, **options end end |