keepcosmos/terjira

View on GitHub
lib/terjira/option_support/editor.rb

Summary

Maintainability
A
0 mins
Test Coverage
module Terjira
  class Editor
    def self.editor_text(content = '')
      editor = ENV['EDITOR']
      if editor.nil? || editor.empty?
        raise 'EDITOR environment variable not found. Please set a default editor.'
      end

      tmp_file = Tempfile.new('content')
      tmp_file.write(content)
      tmp_file.close
      success = system "#{editor} #{tmp_file.path}"
      content = File.read(tmp_file.path) if success

      tmp_file.unlink

      raise 'Editor returned a non-zero exit code. Something must have gone wrong' unless success

      content
    end
  end
end