class Arcanus::Command::Edit
Public Instance Methods
execute()
click to toggle source
# File lib/arcanus/command/edit.rb, line 10 def execute ensure_key_unlocked unless editor_defined? raise Arcanus::Errors::ConfigurationError, '$EDITOR environment variable is not defined' end key = Arcanus::Key.from_file(repo.unlocked_key_path) chest = Arcanus::Chest.new(key: key, chest_file_path: repo.chest_file_path) if arguments.size > 1 edit_single_key(chest, arguments[1], arguments[2]) else # Edit entire chest ::Tempfile.new(['arcanus-chest', '.yaml']).tap do |file| file.sync = true file.write(chest.to_yaml) edit_until_done(chest, file.path) end end end
Private Instance Methods
edit_single_key(chest, key_path, new_value)
click to toggle source
# File lib/arcanus/command/edit.rb, line 39 def edit_single_key(chest, key_path, new_value) new_value = arguments[2] old_value = chest.get(key_path) chest.set(key_path, new_value) chest.save ui.success "Key '#{key_path}' updated from '#{old_value}' to '#{new_value}'" end
edit_until_done(chest, tempfile_path)
click to toggle source
# File lib/arcanus/command/edit.rb, line 47 def edit_until_done(chest, tempfile_path) # Keep editing until there are no syntax errors or user decides to quit loop do unless system(ENV['EDITOR'], tempfile_path) ui.error 'Editor exited unsuccessfully; ignoring any changes made.' break end begin update_chest(chest, tempfile_path) ui.success 'Chest updated successfully' break rescue => ex ui.error "Error occurred while modifying the chest: #{ex.message}" unless ui.yes?('Do you want to try editing the same file again?') break end end end end
editor_defined?()
click to toggle source
# File lib/arcanus/command/edit.rb, line 35 def editor_defined? !ENV['EDITOR'].to_s.strip.empty? end
update_chest(chest, tempfile_path)
click to toggle source
# File lib/arcanus/command/edit.rb, line 69 def update_chest(chest, tempfile_path) changed_hash = YAML.load_file(tempfile_path).to_hash chest.update(changed_hash) chest.save # TODO: Show diff and let user accept/reject before saving end