about summary refs log tree commit homepage
path: root/bin/dtas-sinkedit
blob: 9d9fa252a316c5bf310d52486cdd5090bc96bf9a (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
49
50
51
52
53
54
55
56
57
58
#!/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 'dtas/unix_client'
require 'dtas/disclaimer'
require 'tempfile'
require 'yaml'
editor = ENV["VISUAL"] || ENV["EDITOR"] || "vi"
c = DTAS::UNIXClient.new
usage = "#$0 SINKNAME"
ARGV.size == 1 or abort usage
name = ARGV[0]

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

buf = c.req(%W(sink cat #{name}))
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
sink = YAML.load(tmp.read)

cmd = %W(sink ed #{name})
if env = sink["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(nonblock active).each do |field|
  if sink.key?(field)
    cmd << "#{field}=#{sink[field] ? 'true' : 'false'}"
  end
end

%w(prio pipe_size).each do |field|
  value = sink[field] and cmd << "#{field}=#{value}"
end

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

c.req_ok(cmd)