namespace :credentials do

  desc 'Edit credentials using the encryption key from the server'
  task :edit do
    on primary(:app) do
      key_path = "#{shared_path}/config/credentials/#{fetch(:rails_env)}.key"
      key = capture(:cat, key_path).strip

      run_locally do
        edit_command = "bundle exec rails credentials:edit --environment #{fetch(:rails_env)}"
        system({ 'RAILS_MASTER_KEY' => key }, edit_command)
      end
    rescue SSHKit::Command::Failed => e
      puts e.message
      exit 1
    end
  end

end