Skip to content

Commit

Permalink
Add the observed-shell plugin for observing and reporting with shell …
Browse files Browse the repository at this point in the history
…commands
  • Loading branch information
KUOKA Yusuke committed Dec 6, 2013
1 parent 6e078dc commit aaae23e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
5 changes: 5 additions & 0 deletions plugins/observed-shell/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# A sample Gemfile
source "https://rubygems.org"

gem 'observed', :path => '../..'
gem 'mixlib-shellout'
54 changes: 54 additions & 0 deletions plugins/observed-shell/lib/observed/shell.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit aaae23e

Please sign in to comment.