From aaae23ea64940e5c14119c03b9f11d71d46c1c00 Mon Sep 17 00:00:00 2001 From: KUOKA Yusuke Date: Fri, 6 Dec 2013 17:42:28 +0900 Subject: [PATCH] Add the observed-shell plugin for observing and reporting with shell commands --- plugins/observed-shell/Gemfile | 5 ++ plugins/observed-shell/lib/observed/shell.rb | 54 ++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 plugins/observed-shell/Gemfile create mode 100644 plugins/observed-shell/lib/observed/shell.rb diff --git a/plugins/observed-shell/Gemfile b/plugins/observed-shell/Gemfile new file mode 100644 index 0000000..d45b691 --- /dev/null +++ b/plugins/observed-shell/Gemfile @@ -0,0 +1,5 @@ +# A sample Gemfile +source "https://rubygems.org" + +gem 'observed', :path => '../..' +gem 'mixlib-shellout' diff --git a/plugins/observed-shell/lib/observed/shell.rb b/plugins/observed-shell/lib/observed/shell.rb new file mode 100644 index 0000000..b8ad131 --- /dev/null +++ b/plugins/observed-shell/lib/observed/shell.rb @@ -0,0 +1,54 @@ +require 'mixlib/shellout' + +require 'observed/observer' +require 'observed/reporter' + +module Observed + module Plugins + end +end + +class Observed::Plugins::ShellObserver < Observed::Observer + plugin_name 'shell' + + attribute :command + + def observe + c = Mixlib::ShellOut.new(*command) + c.run_command + { command: command, stdout: c.stdout, stderr: c.stderr } + end +end + +class Observed::Plugins::ShellReporter < Observed::Reporter + plugin_name 'shell' + + attribute :command + attribute :input_key + + def report(data, options) + if command.is_a? Proc + num_params = command.parameters.size + args = [data, options].take(num_params) + result = command.call *args + command_line = result + else + command_line = command + end + c = Mixlib::ShellOut.new(*command_line, input: data[get_attribute_value(:input_key)]) + c.run_command + #logger.debug %Q|[observed-shell] ShellReporter executed the command "#{command_line}", captured stdout is "#{c.stdout}", captured stderr is #{c.stderr}"| + data + end +end + +if __FILE__ == $0 + require 'observed' + + include Observed + + test = (observe via: 'shell', with: { command: 'echo foo' } ) + .then(report via: 'shell', with: { command: -> d { "growlnotify -m #{d[:stdout]}" } } ) + + test.now +end