about summary refs log tree commit homepage
path: root/bin/dtas-sourceedit
blob: 3ce7945192ca385b7a38bd12885cae49c4f65942 (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
#!/usr/bin/env ruby
# -*- encoding: binary -*-
# Copyright (C) 2013, Eric Wong <normalperson@yhbt.net>
# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
require 'tempfile'
require 'yaml'
require 'dtas/unix_client'
require 'dtas/disclaimer'
editor = ENV["VISUAL"] || ENV["EDITOR"]
c = DTAS::UNIXClient.new
usage = $0
ARGV.size == 0 or abort usage
name = ARGV[0]

tmp = Tempfile.new(%w(dtas-sourceedit .yml))
tmp.sync = true
tmp.binmode

buf = c.req(%W(source cat))
abort(buf) if buf =~ /\AERR/
orig = YAML.load(buf)

tmp.write(buf << DTAS_DISCLAIMER)
cmd = "#{editor} #{tmp.path}"
system(cmd) or abort "#{cmd} failed: #$?"
tmp.rewind
source = YAML.load(tmp.read)

cmd = %W(source ed)
if env = source["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

%w(command).each do |field|
  value = source[field]
  cmd << "#{field}=#{value}"
end

c.req_ok(cmd)