aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/commands/credentials/credentials_command/diffing.rb
blob: 1d34c6807498e5f03d1687619ac122e035c07733 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# frozen_string_literal: true

module Rails::Command::CredentialsCommand::Diffing # :nodoc:
  def enroll_project_in_credentials_diffing
    if enrolled?
      true
    else
      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
  end

  def ensure_rails_credentials_driver_is_set
    set_driver if enrolled? && !driver_configured?
  end

  private
    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 set_driver
      puts "running"
      system "git config diff.rails_credentials.textconv 'bin/rails credentials:diff'"
    end

    def gitattributes
      Rails.root.join(".gitattributes")
    end
end