/
get-servicehubot.coffee
56 lines (48 loc) · 2.22 KB
/
get-servicehubot.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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