Class: Dotenvcrypt::Manager
- Inherits:
-
Object
- Object
- Dotenvcrypt::Manager
- Defined in:
- lib/dotenvcrypt/manager.rb
Constant Summary collapse
- CONFIG_HOME =
Determine config directory following XDG Base Directory Specification
ENV['XDG_CONFIG_HOME'] || "#{ENV['HOME']}/.config"
- CONFIG_DIR =
"#{CONFIG_HOME}/dotenvcrypt".freeze
- ENCRYPTION_KEY_LOCATIONS =
Check multiple locations in order of preference
[ "#{Dir.pwd}/.dotenvcrypt.key", # Project-specific key (current directory) "#{CONFIG_DIR}/secret.key", # XDG standard location "#{ENV['HOME']}/.dotenvcrypt.key" # Legacy location (for backward compatibility) ].freeze
Instance Method Summary collapse
-
#decrypt_file(encrypted_file) ⇒ Object
Decrypt an encrypted ‘.env` file and print to stdout.
-
#edit_file(encrypted_file) ⇒ Object
Edit decrypted env, then re-encrypt.
-
#encrypt_file(unencrypted_file, encrypted_file) ⇒ Object
Encrypt an existing ‘.env` file.
-
#initialize(encryption_key = nil) ⇒ Manager
constructor
A new instance of Manager.
Constructor Details
Instance Method Details
#decrypt_file(encrypted_file) ⇒ Object
Decrypt an encrypted ‘.env` file and print to stdout
43 44 45 |
# File 'lib/dotenvcrypt/manager.rb', line 43 def decrypt_file(encrypted_file) puts encryptor.decrypt(File.read(encrypted_file)) end |
#edit_file(encrypted_file) ⇒ Object
Edit decrypted env, then re-encrypt
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/dotenvcrypt/manager.rb', line 48 def edit_file(encrypted_file) temp_file = Tempfile.new('dotenvcrypt') original_content = encryptor.decrypt(File.read(encrypted_file)) File.open(temp_file.path, 'w') do |f| f.write original_content end puts "Waiting for file to be saved. Abort with Ctrl-C." system("#{ENV['EDITOR'] || 'vim'} #{temp_file.path}") updated_content = File.read(temp_file.path) if updated_content != original_content File.open(encrypted_file, 'w') do |f| f.write encryptor.encrypt(updated_content) end end puts "🔒 Encrypted and saved #{encrypted_file}" ensure temp_file.unlink if temp_file end |
#encrypt_file(unencrypted_file, encrypted_file) ⇒ Object
Encrypt an existing ‘.env` file
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/dotenvcrypt/manager.rb', line 29 def encrypt_file(unencrypted_file, encrypted_file) unless File.exist?(unencrypted_file) puts "❌ File not found: #{unencrypted_file}" exit(1) end File.open(encrypted_file, 'w') do |f| f.write encryptor.encrypt(File.read(unencrypted_file)) end puts "🔒 Encrypted #{unencrypted_file} → #{encrypted_file}" end |