Sha256: 6479173b70cb08caed546cd5013f99ce23880dbfc28cbe1ff7736de63718ee28

Contents?: true

Size: 1.57 KB

Versions: 4

Compression:

Stored size: 1.57 KB

Contents

require 'hiera/backend/eyaml/utils'
require 'hiera/backend/eyaml/actions/decrypt_action'
require 'hiera/backend/eyaml/actions/encrypt_action'
require 'hiera/backend/eyaml/options'

class Hiera
  module Backend
    module Eyaml
      module Actions
  
        class EditAction

          def self.execute 
  
            decrypted_input = DecryptAction.execute 
            decrypted_file = Utils.write_tempfile decrypted_input
            editor = Utils.find_editor
            system editor, decrypted_file
            status = $?
            raise StandardError, "Editor #{editor} has not exited?" unless status.exited?
            raise StandardError, "Editor did not exit successfully (exit code #{status.exitstatus}), aborting" unless status.exitstatus  #TODO: The file is left on the disk
            raise StandardError, "File was moved by editor" unless File.file? decrypted_file

            edited_file = File.read decrypted_file
            Utils.secure_file_delete :file => decrypted_file, :num_bytes => [edited_file.length, decrypted_input.length].max
            raise StandardError, "Edited file is blank" if edited_file.empty?
            raise StandardError, "No changes" if edited_file == decrypted_input

            Eyaml::Options[:input_data] = edited_file
            Eyaml::Options[:output] = "raw"

            encrypted_output = EncryptAction.execute

            filename = Eyaml::Options[:eyaml]
            File.open("#{filename}", 'w') { |file| 
              file.write encrypted_output
            }

            true
          end

        end

      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hiera-eyaml-1.3.4 lib/hiera/backend/eyaml/actions/edit_action.rb
hiera-eyaml-1.3.3 lib/hiera/backend/eyaml/actions/edit_action.rb
hiera-eyaml-1.3.2 lib/hiera/backend/eyaml/actions/edit_action.rb
hiera-eyaml-1.3.1 lib/hiera/backend/eyaml/actions/edit_action.rb