Commit 21236fc

mo <mo.khan@gmail.com>
2018-05-10 04:00:43
add execute shell command tag: v0.1.16
1 parent a7608d4
Changed files (3)
lib/del/message.rb
@@ -1,5 +1,6 @@
 module Del
   class Message
+    PREFIX = "/code"
     attr_reader :text, :robot, :source
 
     def initialize(text, robot:, source:)
@@ -12,6 +13,24 @@ module Del
       source.reply(robot, message)
     end
 
+    def execute_shell(command)
+      command = Array(command).flatten.join(' ')
+      reply("Okay, I will run #{command}.")
+      success = false
+      Open3.popen3(command) do |stdin, stdout, stderr, wait_thr|
+        stdout.each_line do |line|
+          yield line if block_given?
+          reply("#{PREFIX} #{line}")
+        end
+        stderr.each_line do |line|
+          yield line if block_given?
+          reply("#{PREFIX} #{line}")
+        end
+        success = wait_thr.value.success?
+      end
+      success
+    end
+
     def to_s
       "#{source}: #{text}"
     end
lib/del/version.rb
@@ -1,3 +1,3 @@
 module Del
-  VERSION = "0.1.15"
+  VERSION = "0.1.16"
 end
lib/del.rb
@@ -1,5 +1,6 @@
 require "json"
 require "logger"
+require "open3"
 require "socket"
 require "xmpp4r"
 require "xmpp4r/muc/helper/mucbrowser"