about summary refs log tree commit homepage
path: root/lib/dtas/edit_client.rb
blob: 141fbb8add7aa0360a9b03b518c28d865103ee98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Copyright (C) 2013-2016 all contributors <dtas-all@nongnu.org>
# License: GPL-3.0+ (https://www.gnu.org/licenses/gpl-3.0.txt)
# frozen_string_literal: true
require 'tempfile'
require 'yaml'
require_relative 'unix_client'
require_relative 'disclaimer'

# common code between dtas-sourceedit and dtas-sinkedit
module DTAS::EditClient # :nodoc:
  def editor
    %w(VISUAL EDITOR).each do |key|
      v = ENV[key] or next
      v.empty? and next
      return v
    end
    'vi'.freeze
  end

  def client_socket
    DTAS::UNIXClient.new
  rescue
    e = "DTAS_PLAYER_SOCK=#{DTAS::UNIXClient.default_path}"
    abort "dtas-player not running on #{e}"
  end

  def tmpyaml
    tmp = Tempfile.new(%W(#{File.basename($0)} .yml))
    tmp.sync = true
    tmp.binmode
    tmp
  end

  def update_cmd_env(cmd, orig, updated)
    if env = updated["env"]
      env.each do |k,v|
        cmd << (v.nil? ? "env##{k}" : "env.#{k}=#{v}")
      end
    end

    # remove deleted env
    if orig_env = orig["env"]
      env ||= {}
      deleted_keys = orig_env.keys - env.keys
      deleted_keys.each { |k| cmd << "env##{k}" }
    end
  end
end