Skip to content

Instantly share code, notes, and snippets.

@JoshAshby
Last active August 29, 2015 14:17
Show Gist options
  • Save JoshAshby/056b3b1fe0c9fd9a1600 to your computer and use it in GitHub Desktop.
Save JoshAshby/056b3b1fe0c9fd9a1600 to your computer and use it in GitHub Desktop.
Runs the given command and sends a notification if it fails
#!/usr/bin/env ruby
require 'washbullet'
require 'thor'
class CommandCLI < Thor
desc "pushbullet COMMAND", "Runs the given COMMAND and sends a pushbullet notification if the exit status is non-zero"
long_desc <<-DESC
Runs the given command and sends a pushbullet notification if the exit status is non-zero.
The message can be customized, and there are a few values that you can use through placeholders, eg:
The command %{command} returned %{status}: %{result}
Available keys for the message:
- command
- status
- result
DESC
option :message, default: "The command %{command} returned %{status}!\n%{result}"
option :title, default: "Notifatron"
option :key, default: ENV['PUSHBULLET_KEY']
def pushbullet command
if options[:key].nil?
raise ArgumentError.new("Need to specify the pushbullet key in the environment as PUSHBULLET_KEY")
end
pushbullet_client = Washbullet::Client.new(options[:key])
res = {
command: command,
result: system(command),
status: $?.exitstatus
}
if res[:status] != 0
message = options[:message] % res
#puts "Would push notification: #{ options[:title] } - #{ message }"
pushbullet_client.push_note nil, options[:title], message
end
end
end
CommandCLI.start(ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment