aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/commands/secrets/secrets_command.rb
blob: 05e0c228e89a710c4bdcb8849fdea7abef5e749f (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
42
43
44
45
46
47
48
49
50
require "active_support"
require "rails/secrets"

module Rails
  module Command
    class SecretsCommand < Rails::Command::Base # :nodoc:
      def help
        say "Usage:\n  #{self.class.banner}"
        say ""
        say self.class.desc
      end

      def setup
        require "rails/generators"
        require "rails/generators/rails/encrypted_secrets/encrypted_secrets_generator"

        Rails::Generators::EncryptedSecretsGenerator.start
      end

      def edit
        require_application_and_environment!

        Rails::Secrets.read_for_editing do |tmp_path|
          watch tmp_path do
            puts "Waiting for secrets file to be saved. Abort with Ctrl-C."
            system("\$EDITOR #{tmp_path}")
          end
        end

        puts "New secrets encrypted and saved."
      rescue Interrupt
        puts "Aborted changing encrypted secrets: nothing saved."
      rescue Rails::Secrets::MissingKeyError => error
        say error.message
      end

      private
        def watch(tmp_path)
          mtime, start_time = File.mtime(tmp_path), Time.now

          yield

          editor_exits_after_open = $?.success? && (Time.now - start_time) < 1
          if editor_exits_after_open
            sleep 0.250 until File.mtime(tmp_path) != mtime
          end
        end
    end
  end
end