Skip to content

Instantly share code, notes, and snippets.

@MattHodge
Last active January 24, 2016 20:07
Show Gist options
  • Save MattHodge/8e3461622b7f3c1f9a53 to your computer and use it in GitHub Desktop.
Save MattHodge/8e3461622b7f3c1f9a53 to your computer and use it in GitHub Desktop.
get-servicehubot.coffee
# Description:
# Gets the status of a service on the Hubot server
#
# Commands:
# hubot get service <service name> - Finds the status of a service on the Hubot server
# Require the edge module we installed
edge = require("edge")
# Build the PowerShell that will execute
executePowerShell = edge.func('ps', -> ###
# Dot source the function
. .\scripts\Get-ServiceHubot.ps1
# Edge.js passes an object to PowerShell as a variable - $inputFromJS
# This object is built in CoffeeScript on line 28 below
Get-ServiceHubot -Name $inputFromJS.serviceName
###
)
module.exports = (robot) ->
# Capture the user message using a regex capture to find the name of the service
robot.respond /get service (.*)$/i, (msg) ->
# Set the service name to a varaible
serviceName = msg.match[1]
# Build an object to send to PowerShell
psObject = {
serviceName: serviceName
}
# Build the PowerShell callback
callPowerShell = (psObject, msg) ->
executePowerShell psObject, (error,result) ->
# If there are any errors that come from the CoffeeScript command
if error
msg.send ":fire: An error was thrown in Node.js/CoffeeScript"
msg.send error
else
# Capture the PowerShell outpout and convert the JSON that the function returned into a CoffeeScript object
result = JSON.parse result[0]
# Output the results into the Hubot log file so we can see what happened - useful for troubleshooting
console.log result
# Check in our object if the command was a success (checks the JSON returned from PowerShell)
# If there is a success, prepend a check mark emoji to the output from PowerShell.
if result.success is true
# Build a string to send back to the channel and include the output (this comes from the JSON output)
msg.send ":white_check_mark: #{result.output}"
# If there is a failure, prepend a warning emoji to the output from PowerShell.
else
# Build a string to send back to the channel and include the output (this comes from the JSON output)
msg.send ":warning: #{result.output}"
# Call PowerShell function
callPowerShell psObject, msg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment