Class: Hiera::Backend::Eyaml::Subcommands::Edit

Inherits:
Hiera::Backend::Eyaml::Subcommand show all
Defined in:
lib/hiera/backend/eyaml/subcommands/edit.rb

Class Method Summary collapse

Methods inherited from Hiera::Backend::Eyaml::Subcommand

all_options, attach_option, find, hidden?, load_config_file, parse, prettyname

Class Method Details

.descriptionObject



18
19
20
# File 'lib/hiera/backend/eyaml/subcommands/edit.rb', line 18

def self.description
  "edit an eyaml file"
end

.executeObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/hiera/backend/eyaml/subcommands/edit.rb', line 34

def self.execute
  encrypted_parser = Parser::ParserFactory.encrypted_parser
  tokens = encrypted_parser.parse Eyaml::Options[:input_data]
  decrypted_input = tokens.each_with_index.to_a.map{|(t,index)| t.to_decrypted :index => index}.join
  decrypted_file = Utils.write_tempfile decrypted_input

  editor = Utils.find_editor

  begin
    system "#{editor} #{decrypted_file}"
    status = $?

    raise StandardError, "File was moved by editor" unless File.file? decrypted_file
    edited_file = File.read decrypted_file

    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 == 0
    raise StandardError, "Edited file is blank" if edited_file.empty?

    if edited_file == decrypted_input
      Utils.info "No changes detected, exiting"
    else
      decrypted_parser = Parser::ParserFactory.decrypted_parser
      edited_tokens = decrypted_parser.parse(edited_file)

      # check that the tokens haven't been copy / pasted
      used_ids = edited_tokens.find_all{ |t| t.class.name =~ /::EncToken$/ and !t.id.nil? }.map{ |t| t.id }
      if used_ids.length != used_ids.uniq.length
          raise RecoverableError, "A duplicate DEC(ID) was found so I don't know how to proceed. This is probably because you copy and pasted a value - if you do this please delete the ID in parentheses"
      end

      # replace untouched values with the source values
      edited_denoised_tokens = edited_tokens.map{ |token|
        if token.class.name =~ /::EncToken$/ && !token.id.nil?
          old_token = tokens[token.id]
          if old_token.plain_text.eql? token.plain_text
            old_token
          else
            token
          end
        else
          token
        end
      }

      encrypted_output = edited_denoised_tokens.map{ |t| t.to_encrypted }.join

      filename = Eyaml::Options[:eyaml]
      File.open("#{filename}", 'w') { |file|
        file.write encrypted_output
      }
    end
  rescue RecoverableError => e
    Utils.info e
    if agree "Return to the editor to try again?"
      retry
    else
      raise e
    end
  ensure
    Utils.secure_file_delete :file => decrypted_file, :num_bytes => [edited_file.length, decrypted_input.length].max
  end

  nil
end

.helptextObject



22
23
24
# File 'lib/hiera/backend/eyaml/subcommands/edit.rb', line 22

def self.helptext
  "Usage: eyaml edit [options] <some-eyaml-file>"
end

.optionsObject



14
15
16
# File 'lib/hiera/backend/eyaml/subcommands/edit.rb', line 14

def self.options
  []
end

.validate(options) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/hiera/backend/eyaml/subcommands/edit.rb', line 26

def self.validate options
  Trollop::die "You must specify an eyaml file" if ARGV.empty?
  options[:source] = :eyaml
  options[:eyaml] = ARGV.shift
  options[:input_data] = File.read options[:eyaml]
  options
end