about summary refs log tree commit homepage
path: root/bin
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2013-08-26 05:59:07 +0000
committerEric Wong <normalperson@yhbt.net>2013-08-26 05:59:07 +0000
commit57bd6f96e007097219cdd31b034f8ca386927ed6 (patch)
treeef7bf91aeecae88f5bd3a2514b8c3e755b10f812 /bin
parent2907cc6dd0b0d2d80d44ae292a157651d6e05ee7 (diff)
downloaddtas-57bd6f96e007097219cdd31b034f8ca386927ed6.tar.gz
These two are similar enough that it's possible to share
some code between them and also increase user-friendliness
at the same time.
Diffstat (limited to 'bin')
-rwxr-xr-xbin/dtas-sinkedit40
-rwxr-xr-xbin/dtas-sourceedit37
2 files changed, 23 insertions, 54 deletions
diff --git a/bin/dtas-sinkedit b/bin/dtas-sinkedit
index 9d9fa25..bf97020 100755
--- a/bin/dtas-sinkedit
+++ b/bin/dtas-sinkedit
@@ -2,20 +2,16 @@
 # -*- 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"
+require 'dtas/edit_client'
+include DTAS::EditClient
+c = client_socket
+sinks = c.req('sink ls') || "(unknown)"
+usage = "Usage: #{DTAS_PROGNAME} SINKNAME\n" \
+        "available SINKNAME values: #{sinks}"
 ARGV.size == 1 or abort usage
 name = ARGV[0]
 
-tmp = Tempfile.new(%w(dtas-sinkedit .yml))
-tmp.sync = true
-tmp.binmode
-
+tmp = tmpyaml
 buf = c.req(%W(sink cat #{name}))
 abort(buf) if buf =~ /\AERR/
 orig = YAML.load(buf)
@@ -27,32 +23,20 @@ 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
+update_cmd_env(cmd, orig, sink)
 
+# both of these default to false
 %w(nonblock active).each do |field|
-  if sink.key?(field)
-    cmd << "#{field}=#{sink[field] ? 'true' : 'false'}"
-  end
+  cmd << "#{field}=#{sink[field] ? 'true' : 'false'}"
 end
 
 %w(prio pipe_size).each do |field|
   value = sink[field] and cmd << "#{field}=#{value}"
 end
 
+# nil OK
 %w(command).each do |field|
-  value = sink[field]
-  cmd << "#{field}=#{value}"
+  cmd << "#{field}=#{sink[field]}"
 end
 
 c.req_ok(cmd)
diff --git a/bin/dtas-sourceedit b/bin/dtas-sourceedit
index 7e30e6b..2a61e36 100755
--- a/bin/dtas-sourceedit
+++ b/bin/dtas-sourceedit
@@ -2,20 +2,16 @@
 # -*- 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"] || "vi"
-c = DTAS::UNIXClient.new
-usage = "#$0 <sox|av>"
+require 'dtas/edit_client'
+include DTAS::EditClient
+c = client_socket
+sources = c.req('source ls') || "(unknown)"
+usage = "Usage: #{DTAS_PROGNAME} SOURCENAME\n" \
+        "available SOURCENAME values: #{sources}"
 ARGV.size <= 1 or abort usage
 name = ARGV[0] || "sox"
 
-tmp = Tempfile.new(%w(dtas-sourceedit .yml))
-tmp.sync = true
-tmp.binmode
-
+tmp = tmpyaml
 buf = c.req(%W(source cat #{name}))
 abort(buf) if buf =~ /\AERR/
 orig = YAML.load(buf)
@@ -27,22 +23,11 @@ tmp.rewind
 source = YAML.load(tmp.read)
 
 cmd = %W(source ed #{name})
-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
+update_cmd_env(cmd, orig, source)
 
-%w(command).each do |field|
-  value = source[field]
-  cmd << "#{field}=#{value}"
+# nil OK
+%w(tryorder command).each do |field|
+  cmd << "#{field}=#{source[field]}"
 end
 
 c.req_ok(cmd)