Advertisement
Guest User

Untitled

a guest
May 5th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'washbullet'
  4. require 'thor'
  5.  
  6. class CommandCLI < Thor
  7.  
  8. desc "pushbullet COMMAND", "Runs the given COMMAND and sends a pushbullet notification if the exit status is non-zero"
  9. long_desc <<-DESC
  10. Runs the given command and sends a pushbullet notification if the exit status is non-zero.
  11.  
  12. The message can be customized, and there are a few values that you can use through placeholders, eg:
  13. The command %{command} returned %{status}: %{result}
  14.  
  15. Available keys for the message:
  16. - command
  17. - status
  18. - result
  19. DESC
  20. option :message, default: "The command %{command} returned %{status}!\n%{result}"
  21. option :title, default: "Notifatron"
  22. option :key, default: ENV['PUSHBULLET_KEY']
  23. def pushbullet command
  24. if options[:key].nil?
  25. raise ArgumentError.new("Need to specify the pushbullet key in the environment as PUSHBULLET_KEY")
  26. end
  27.  
  28. pushbullet_client = Washbullet::Client.new(options[:key])
  29.  
  30. res = {
  31. command: command,
  32. result: system(command),
  33. status: $?.exitstatus
  34. }
  35.  
  36. if res[:status] != 0
  37. message = options[:message] % res
  38. #puts "Would push notification: #{ options[:title] } - #{ message }"
  39. pushbullet_client.push_note nil, options[:title], message
  40. end
  41. end
  42.  
  43. end
  44.  
  45. CommandCLI.start(ARGV)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement